knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "400px"
)

H3-R

R build status Project Status: Active – The project has reached a stable, usable state and is being actively developed. H3 Version CRAN status

Provides R bindings for H3, a hexagonal hierarchical spatial indexing system.

Documentation

Notes

Succesfully built on

Since v3.7.1 {h3} comes with a bundled version of the H3 C library, so that you no longer have to build it yourself before installing the R package.

Installation

Once on CRAN you can install {h3} with:

install.packages("h3")

You can install the latest version of {h3} from github with:

# install.packages("remotes")
remotes::install_github("crazycapivara/h3-r")

Usage

Core functions:

library(h3)

coords <- c(37.3615593, -122.0553238)
resolution <- 7

# Convert a lat/lng point to a hexagon index at resolution 7
(h3_index <- geo_to_h3(coords, resolution)) 

# Get the center of the hexagon
h3_to_geo_sf(h3_index)

# Get the vertices of the hexagon
h3_to_geo_boundary(h3_index)

# Get the polygon of the hexagon
h3_to_geo_boundary_sf(h3_index)

Useful algorithms:

# Get all neighbors within 1 step of the hexagon
radius <- 1
(neighbors <- k_ring(h3_index, radius))

h3_to_geo_boundary_sf(neighbors) %>%
  sf::st_geometry() %>% plot(col = "blue")

h3_set_to_multi_polygon(neighbors) %>%
  sf::st_geometry() %>% plot(col = "green")

Run tests

devtools::test(reporter = "minimal")


crazycapivara/h3-r documentation built on Aug. 10, 2022, 1:17 p.m.