knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
R bindings to H3, a hexagon-based geographic grid system via h3-js.
R bindings via native H3 C library can be found at h3r. h3r
is much faster but requires installing h3
separately.
WARNING Only functions that take non-array input support vectorized operations currently. The return value of other functions when applying vectorized input may be unspecified. You should map over those input instead.
You can install the released version of h3js from CRAN with:
install.packages("h3js")
Development version can be installed with:
devtools::install_github("saurfang/h3js")
library(h3js)
library(sf)
library(tidyverse)
# Convert a lat/lng point to a hexagon index at resolution 7
h3_index <- h3_geo_to_h3(37.3615593, -122.0553238, 7)
h3_index
# Get the center of the hexagon
hex_center_coordinates <- h3_to_geo(h3_index)
hex_center_coordinates
# Get the vertices of the hexagon
hex_boundary <- h3_to_geo_boundary(h3_index)
hex_boundary
hex_boundary %>%
# close polygon
rbind(.[1,]) %>%
# swap columns
.[, c(2, 1)] %>%
# convert to simple feature collection
list() %>%
st_polygon() %>%
st_sfc() %>%
plot()
# Get all neighbors within 1 step of the hexagon
h3_k_ring(h3_index, 1)
# Get the set of hexagons within a polygon
polygon <- list(
c(37.813318999983238, -122.4089866999972145),
c(37.7198061999978478, -122.3544736999993603),
c(37.8151571999998453, -122.4798767000009008)
)
hexagons <- h3_polyfill(polygon, 7)
hexagons
# Get the outline of a set of hexagons, as a GeoJSON-style MultiPolygon
coordinates <- h3_set_to_multi_polygon(hexagons, TRUE)
c(
coordinates %>%
map(~ matrix(.x, ncol = 2)) %>%
map(list) %>%
st_multipolygon() %>%
st_sfc(),
do.call(rbind, polygon) %>%
# close polygon
rbind(.[1,]) %>%
# swap columns
.[, c(2, 1)] %>%
list %>%
st_polygon() %>%
st_sfc()
) %>%
st_sf() %>%
plot()
Houston <- list(lat = 29.7632836, lon = -95.3632715)
# single arguments
microbenchmark::microbenchmark(
h3r::getIndexFromCoords(Houston$lat, Houston$lon, resolution = 5),
h3_geo_to_h3(Houston$lat, Houston$lon, res = 5),
h3r::getBoundingHexFromCoords(Houston$lat, Houston$lon, resolution = 5),
h3_to_geo_boundary(h3_geo_to_h3(Houston$lat, Houston$lon, res = 5))
)
# vectorized input
coordinates <- list(lat = runif(100, -90, 90), lon = runif(100, -180, 180))
microbenchmark::microbenchmark(
pmap(coordinates, ~h3r::getIndexFromCoords(.x, .y, resolution = 5)),
h3_geo_to_h3(coordinates$lat, coordinates$lon, res = 5)
)