Skip to content

Commit

Permalink
bugfixes in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
baarthur committed Dec 5, 2023
1 parent d003e8e commit e9d1533
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 45 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Imports:
colorspace,
dplyr,
ggplot2,
h3jsr,
magrittr,
osmdata,
purrr,
Expand Down
44 changes: 24 additions & 20 deletions R/get_h3_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#' 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/].
#' 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;
#' 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
Expand All @@ -28,39 +28,43 @@
#' @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
#' 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) {


# setting local variables that will be created later
h3_addresses <- NULL
geometry <- NULL

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) %>%

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 .} %>%
cell_to_polygon(simple = FALSE) %>%
relocate(geometry, .after = everything()) %>%
{if(crop) st_intersection(., shp) else {
crossing(., st_drop_geometry(shp)) %>% st_as_sf()
}} %>%
{if(keep_crs) st_transform(., crs = crs_old) else .}
}

3 changes: 3 additions & 0 deletions inst/examples/geom_bbox.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
library(ggplot2)
library(dplyr)

data("fortaleza")

# plot the whole city, fill neighborhood "aldeota" in red, and zoom in to region "SER II"
Expand Down
9 changes: 7 additions & 2 deletions inst/examples/geom_sf_toscale.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(ggplot2)

data("fortaleza")

# entire data
Expand All @@ -9,6 +11,9 @@ ggplot() +
geom_sf_toscale(fortaleza, 3000, name_neigh, c("centro", "aldeota"))

# filtering a few neighborhoods, one per plot
library(dplyr)
library(purrr)

p <- fortaleza[1:2,] %>%
pull(name_neigh) %>%
map(
Expand All @@ -18,5 +23,5 @@ p <- fortaleza[1:2,] %>%
labs(title = x)
)

## plotting them together
cowplot::plot_grid(p[[1]], p[[2]])
## plotting them together (requires cowplot)
# cowplot::plot_grid(p[[1]], p[[2]])
17 changes: 7 additions & 10 deletions inst/examples/get_h3_grid.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
library(dplyr)

data("fortaleza")

# grid for only one polygon

## cutting grid edges (default)
fortaleza %>%
filter(name_neigh == "centro") %>%
get_h3_grid() %>%
ggplot() +
geom_sf()
get_h3_grid()

## letting grid cross borders
fortaleza %>%
filter(name_neigh == "centro") %>%
get_h3_grid(crop = FALSE) %>%
ggplot() +
geom_sf()
get_h3_grid(crop = FALSE)


# grid for multiple polygons using purrr::map()

library(purrr)

## cutting edges
fortaleza %>%
filter(name_region %in% c("SER III", "SER IV")) %>%
Expand All @@ -28,7 +28,4 @@ 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()
bind_rows()
3 changes: 3 additions & 0 deletions man/geom_bbox.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions man/geom_sf_toscale.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions man/get_h3_grid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9d1533

Please sign in to comment.