R/geo-boundary-to-sf.R

Defines functions geo_boundary_to_sf.list geo_boundary_to_sf.matrix geo_boundary_to_sf features_to_sf

Documented in geo_boundary_to_sf geo_boundary_to_sf.list geo_boundary_to_sf.matrix

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

#' Parse geo boundaries to object of class \code{sf}
#'
#' @param obj geo boundary matrix or list of matrices with [lng, lat] pairs
#' as returned from \link{h3_to_geo_boundary} or \link{h3_set_to_multi_polygon}
#'
#' @return object of class \code{sf} (geometry type: \code{POLYGON})
#'
#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf <- function(obj) {
  if (!any(class(obj) == "lng_lat_closed")) {
    stop("only closed loops of the form [lng, lat] are supported")
  }

  UseMethod("geo_boundary_to_sf")
}

#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf.matrix <- function(obj) {
  sf::st_polygon(list(obj)) %>% features_to_sf()
}

#' @name geo_boundary_to_sf
#' @export
geo_boundary_to_sf.list <- function(obj) {
  lapply(obj, function(item) {
    sf::st_polygon(list(item))
  }) %>%
    features_to_sf()
}
crazycapivara/h3forr documentation built on Dec. 6, 2020, 5:21 a.m.