R/h3_indexing.R

Defines functions h3_to_geo_boundary_sf h3_to_geo_boundary h3_to_geo_sf h3_to_geo geo_to_h3.sf geo_to_h3.data.frame geo_to_h3.matrix geo_to_h3.numeric geo_to_h3

Documented in geo_to_h3 geo_to_h3.data.frame geo_to_h3.matrix geo_to_h3.numeric geo_to_h3.sf h3_to_geo h3_to_geo_boundary h3_to_geo_boundary_sf h3_to_geo_sf

#' Index geo-coordinates at the given resolution into H3 addresses.
#' @param latlng geo-coordinates as lat/lng pairs or object of class \code{sf}
#' @param res resolution between 0 and 15
#' @return character vector
#' @example inst/examples/api-reference/geo-to-h3.R
#' @name geo_to_h3
#' @export
geo_to_h3 <- function(latlng, res) {
  UseMethod("geo_to_h3")
}

#' @name geo_to_h3
#' @export
geo_to_h3.numeric <- function(latlng, res = 7) {
  matrix(latlng, ncol = 2) %>% rcpp_geo_to_h3(res)
}

#' @name geo_to_h3
#' @export
geo_to_h3.matrix <- function(latlng, res = 7) {
  rcpp_geo_to_h3(latlng, res)
}

#' @name geo_to_h3
#' @export
geo_to_h3.data.frame <- function(latlng, res = 7) {
  as.matrix(latlng) %>% rcpp_geo_to_h3(res)
}

#' @name geo_to_h3
#' @export
geo_to_h3.sf <- function(latlng, res = 7) {
  sf::st_coordinates(latlng)[, 2:1] %>% geo_to_h3(res) # %>% rcpp_geo_to_h3(res)
}

#' Get the centers of the given H3 indexes.
#' @param h3_index character vector of H3 indexes
#' @example inst/examples/api-reference/h3-to-geo.R
#' @export
h3_to_geo <- function(h3_index) {
  rcpp_h3_to_geo(h3_index)
}

#' Get the centers of the given H3 indexes as \code{sf} object.
#' @inheritParams h3_to_geo
#' @example inst/examples/api-reference/h3-to-geo-sf.R
#' @export
h3_to_geo_sf <- function(h3_index) {
  rcpp_h3_to_geo(h3_index) %>%
    geo_to_sf() %>%
    cbind(h3_index = h3_index)
}

#' Get the vertices of the given H3 indexes.
#' @inheritParams h3_to_geo
#' @return list of matrices with geo-coordinates as lat/lng pairs for each H3 index
#' @export
h3_to_geo_boundary <- function(h3_index) {
  rcpp_h3_to_geo_boundary(h3_index)
}

#' Get the polygons of the given H3 indexes as \code{sf} object.
#' @param h3_index character vector of H3 indexes
#' @return object of class \code{sf}
#' @example inst/examples/api-reference/h3-to-geo-boundary-sf.R
#' @export
h3_to_geo_boundary_sf <- function(h3_index) { # nocov start
  rcpp_h3_to_geo_boundary(h3_index) %>%
    geo_boundary_to_sf() %>%
    cbind(h3_index = h3_index)
} # nocov end
crazycapivara/h3-r documentation built on Aug. 10, 2022, 1:17 p.m.