-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new functions get_h3_grid, geom_bbox, and geom_sf_toscale
- Loading branch information
Showing
15 changed files
with
391 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#' Limit a ggplot map | ||
#' | ||
#' @description | ||
#' This function creates a bounding box from a `sf` object and apply its limits to `coord_sf`. | ||
#' | ||
#' @param data An object with classes `sf` and `data.frame`. | ||
#' | ||
#' @importFrom sf st_bbox | ||
#' @importFrom ggplot2 coord_sf | ||
#' | ||
#' @export | ||
#' | ||
#' @returns A `ggplot` object | ||
#' | ||
#' @example inst/examples/geom_bbox.R | ||
|
||
|
||
geom_bbox <- function(data) { | ||
bbox <- data %>% st_bbox | ||
coord_sf(xlim = c(bbox[1], bbox[3]), ylim = c(bbox[2], bbox[4])) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' Make a geom_sf layer with a fixed scale | ||
#' | ||
#' @description | ||
#' A wrapper around `ggplot2::geom_sf`, this function generates fixed scale maps, which enhances | ||
#' comparison between different plots. | ||
#' | ||
#' @param data An object with classes `sf` and `data.frame`. Won't inherit data from previous layer | ||
#' since to make the scale, it is necessary to calculate a buffer around the centroid. | ||
#' @param dist Buffer distance, see `sf::st_buffer` | ||
#' @param col_ref `<data-masking>` Column in `data` with the filtering reference. | ||
#' Optional, provide only if desired output is a subset of original data. | ||
#' @param ref Filtering reference to be passed on `col_ref`. Either a single value or a vector. | ||
#' @param ... Other arguments to pass to \code{ggplot2::geom_sf()}. | ||
#' | ||
#' @import ggplot2 | ||
#' @importFrom dplyr filter | ||
#' @importFrom rlang quo_is_null enquo | ||
#' @importFrom sf st_union st_centroid st_buffer st_bbox | ||
#' | ||
#' @export | ||
#' | ||
#' @returns A `ggplot` object | ||
#' | ||
#' @example inst/examples/geom_sf_toscale.R | ||
|
||
geom_sf_toscale <- function(data, dist, col_ref = NULL, ref = NULL, ...) { | ||
|
||
data <- if(!quo_is_null(enquo(col_ref)) & !is.null(ref)) { | ||
data %>% filter({{col_ref}} %in% ref) | ||
} else {data} | ||
|
||
bbox <- data %>% st_union() %>% st_centroid() %>% st_buffer(dist) %>% st_bbox() | ||
|
||
list( | ||
geom_sf(data = data, ...), | ||
coord_sf(xlim = c(bbox[1], bbox[3]), ylim = c(bbox[2], bbox[4])) | ||
) | ||
} | ||
|
||
# tester <- function(data, col_ref = NULL, ref = NULL) { | ||
# | ||
# if(!rlang::quo_is_null(enquo(col_ref)) & !is.null(ref)) { | ||
# data %>% filter({{col_ref}} %in% ref) | ||
# } else print("null") | ||
# | ||
# } | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#' Get a tidy H3 grid in a sf dataframe | ||
#' @description | ||
#' More than a wrapper around `h3jsr::polygon_to_cells()`, this function automates the process | ||
#' of getting an hexagonal grid for polygons, e.g. cities. It is particularly useful when retrieving | ||
#' a grid for intersecting polygons, as it automates the cropping process (optional) to avoid | ||
#' duplicates. Based on {aopdata}[https://github.com/ipeaGIT/acesso_oport/]. | ||
#' | ||
#' @param shp A `sf` object of type `POLYGON` or `MULTIPOLYGON` | ||
#' @param res Desired H3 resolution, defaults to 9. | ||
#' @param crop Should the polygons be cropped to the original polygon? If yes, `sf::st_intersection` | ||
#' will be used. Defaults to `TRUE` | ||
#' @param buffer In some polygons, border areas may not be included if they do not cover an hexagon's | ||
#' centroid. Creating a border increases the probability of all borders being selected; | ||
#' defaults to `TRUE` | ||
#' @param buffer_size Allows selecting a custom buffer distance (in meters), when `buffer = TRUE`. | ||
#' If left empty, defaults to 300 when `crop = TRUE` and 15 when `crop = FALSE`. To use a unit other | ||
#' than metrics, pass as `buffer_size = units::as_units(x, "unit)`. | ||
#' @param keep_crs Should the original coordinate reference system (`crs`) be preserved? Defaults to | ||
#' `TRUE`; otherwise, will return an object with `crs = 4326` (WGS84). | ||
#' | ||
#' @import sf | ||
#' @importFrom dplyr pull relocate | ||
#' @importFrom h3jsr polygon_to_cells cell_to_polygon | ||
#' @importFrom magrittr %>% | ||
#' @importFrom tidyr crossing | ||
#' @importFrom units as_units | ||
#' | ||
#' @export | ||
#' | ||
#' @returns An object with classes `sf`, `tbl_df`, `tbl`, and `data.frame` | ||
#' | ||
#' @details | ||
#' `h3jsr` recommends passing polygons in WGS84 coordinates, `shp` is automatically converted | ||
#' to that format if not already in WGS84. Since WGS84's Buffer size is passed in meters since | ||
#' it is the default unit fot WGS84. | ||
#' | ||
#' @example inst/examples/get_h3_grid.R | ||
|
||
|
||
|
||
# function ------------------------------------------------------------------------------------ | ||
|
||
get_h3_grid <- function(shp, res = 9, crop = TRUE, buffer = TRUE, buffer_size = NULL, keep_crs = TRUE) { | ||
|
||
buffer_size <- if(is.null(buffer_size)) { | ||
if(crop) units::as_units(300,"m") else units::as_units(15,"m") | ||
} else buffer_size | ||
|
||
crs_old <- if(keep_crs) st_crs(shp) | ||
|
||
shp <- shp %>% | ||
{if(st_crs(.) != 4326) st_transform(., crs = 4326)} | ||
|
||
shp %>% | ||
{if(buffer) st_buffer(., buffer_size) else .} %>% | ||
polygon_to_cells(res = res, simple = FALSE) %>% | ||
pull(h3_addresses) %>% | ||
unlist() %>% | ||
cell_to_polygon(simple = FALSE) %>% | ||
crossing(st_drop_geometry(shp)) %>% | ||
st_as_sf() %>% | ||
relocate(geometry, .after = everything()) %>% | ||
{if(crop) st_intersection(., shp) else .} %>% | ||
{if(keep_crs) st_transform(., crs = crs_old) else .} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
data("fortaleza") | ||
|
||
# plot the whole city, fill neighborhood "aldeota" in red, and zoom in to region "SER II" | ||
fortaleza %>% | ||
ggplot() + | ||
geom_sf(fill = NA) + | ||
geom_sf(data = . %>% filter(name_neigh == "aldeota"), fill = "red") + | ||
geom_bbox(fortaleza %>% filter(name_region == "SER II")) + | ||
theme_void() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
data("fortaleza") | ||
|
||
# entire data | ||
ggplot() + | ||
geom_sf_toscale(fortaleza, 15000) | ||
|
||
# filtering a few neighborhoods, all at once | ||
ggplot() + | ||
geom_sf_toscale(fortaleza, 3000, name_neigh, c("centro", "aldeota")) | ||
|
||
# filtering a few neighborhoods, one per plot | ||
p <- fortaleza[1:2,] %>% | ||
pull(name_neigh) %>% | ||
map( | ||
\(x) ggplot() + | ||
geom_sf(data = fortaleza, fill = "white") + | ||
geom_sf_toscale(fortaleza, 2000, name_neigh, x) + | ||
labs(title = x) | ||
) | ||
|
||
## plotting them together | ||
cowplot::plot_grid(p[[1]], p[[2]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
data("fortaleza") | ||
|
||
# grid for only one polygon | ||
|
||
## cutting grid edges (default) | ||
fortaleza %>% | ||
filter(name_neigh == "centro") %>% | ||
get_h3_grid() %>% | ||
ggplot() + | ||
geom_sf() | ||
|
||
## letting grid cross borders | ||
fortaleza %>% | ||
filter(name_neigh == "centro") %>% | ||
get_h3_grid(crop = FALSE) %>% | ||
ggplot() + | ||
geom_sf() | ||
|
||
|
||
# grid for multiple polygons using purrr::map() | ||
|
||
## cutting edges | ||
fortaleza %>% | ||
filter(name_region %in% c("SER III", "SER IV")) %>% | ||
pull(name_neigh) %>% | ||
map( | ||
\(x) fortaleza %>% | ||
filter(name_neigh == x) %>% | ||
get_h3_grid() | ||
) %>% | ||
bind_rows() %>% | ||
ggplot() + | ||
geom_sf(aes(fill = name_region), alpha = 0.25, linewidth = 0.125) + | ||
theme_void() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.