R/geo-boundary-to-sf.R

Defines functions geo_boundary_to_sf.list geo_boundary_to_sf.matrix geo_boundary_to_sf make_sf make_polygon

Documented in geo_boundary_to_sf geo_boundary_to_sf.list geo_boundary_to_sf.matrix

make_polygon <- function(latlng) {
  lnglat_closed <- rbind(latlng, latlng[1, ])[, 2:1]
  sf::st_polygon(list(lnglat_closed))
}

make_sf <- function(polygons) {
  sf::st_sfc(polygons, crs = 4326) %>%
    sf::st_sf()
}

#' Parse geo-boundaries to \code{sf} object.
#' @param latlng geo-coordinates
#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf <- function(latlng) {
  UseMethod("geo_boundary_to_sf")
}

#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf.matrix <- function(latlng) {
  make_polygon(latlng) %>% make_sf()
}

#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf.list <- function(latlng) {
  lapply(latlng, make_polygon) %>% make_sf()
}
crazycapivara/h3-r documentation built on Aug. 10, 2022, 1:17 p.m.