An open API service indexing awesome lists of open source software.

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.

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

[![Travis build status](https://travis-ci.org/saurfang/h3js.svg?branch=master)](https://travis-ci.org/saurfang/h3js)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/h3js)](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_boundary

hex_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)
)
```