https://github.com/saurfang/h3js
R bindings to H3, a hexagon-based geographic grid system via h3-js.
https://github.com/saurfang/h3js
geospatial h3 spatial-indexing
Last synced: 6 months ago
JSON representation
R bindings to H3, a hexagon-based geographic grid system via h3-js.
- Host: GitHub
- URL: https://github.com/saurfang/h3js
- Owner: saurfang
- License: other
- Created: 2018-09-26T04:47:49.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-27T03:18:11.000Z (about 7 years ago)
- Last Synced: 2025-03-27T07:35:54.774Z (6 months ago)
- Topics: geospatial, h3, spatial-indexing
- Language: R
- Homepage: https://saurfang.github.io/h3js/
- Size: 294 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# h3js[](https://travis-ci.org/saurfang/h3js)
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://cran.r-project.org/package=h3js)R bindings to [H3](https://github.com/uber/h3), a hexagon-based geographic grid system via [h3-js](https://github.com/uber/h3-js).
R bindings via native H3 C library can be found at [h3r](https://github.com/scottmmjackson/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.
## Installation
You can install the released version of h3js from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("h3js")
```Development version can be installed with:
```r
devtools::install_github("saurfang/h3js")
```## Core functions
```{r core, message=FALSE}
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_boundaryhex_boundary %>%
# close polygon
rbind(.[1,]) %>%
# swap columns
.[, c(2, 1)] %>%
# convert to simple feature collection
list() %>%
st_polygon() %>%
st_sfc() %>%
plot()
```## Useful algorithms
```{r algo, message=FALSE}
# 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()
```## Benchmark
```{r}
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)
)
```