Create H3 indices from sfc objects, vectors of x and y coordinates, or H3 string IDs.

h3_from_xy(x, y, resolution)

h3_from_points(x, resolution)

h3_from_strings(x)

h3_to_points(x)

h3_to_vertexes(x)

# S3 method for H3
as.character(x, ...)

flatten_h3(x)

is_h3(x)

Arguments

x

for h3_from_points() an object of class sfc_POINT. For h3_from_strings() a character vector of H3 index IDs. For h3_from_xy() a numeric vector of longitudes.

y

a numeric vector of latitudes.

resolution

an integer indicating the H3 cell resolution. Must be between 0 and 15 inclusive.

...

unused.

Details

  • h3_from_points(): takes an sfc_POINT object and creates a vector of H3 cells

  • h3_from_strings(): converts a character vector of cell indexes to an H3 vector

  • h3_from_xy(): converts vectors of x and y coordinates to an H3 vector

  • h3_to_points(): converts an H3 vector to a either an sfc_POINT object or a list of sfg POINT objects.

  • h3_to_vertexes(): converts an H3 vector to an sfc_MULTIPOINT object or a list of MULTIPOINT objects.

Examples

h3_from_xy(-90, 120, 5)
#> <H3[1]>
#> [1] 85f29383fffffff

h3_from_strings("85f29383fffffff")
#> <H3[1]>
#> [1] 85f29383fffffff

if (requireNamespace("sf")) {
  # create random points
  pnts <- sf::st_cast(
    sf::st_sfc(
      sf::st_multipoint(matrix(runif(10, max = 90), ncol = 2)),
      crs = 4326
    ),
    "POINT"
  )

  # convert to H3 objects
  h3s <- h3_from_points(pnts, 5)

  h3_to_vertexes(h3s)

  h3_to_points(h3s)
}
#> Geometry set for 5 features 
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 31.96947 ymin: 5.121401 xmax: 89.71598 ymax: 55.42679
#> Geodetic CRS:  WGS 84
#> POINT (87.44469 50.92527)
#> POINT (31.96947 5.121401)
#> POINT (64.7449 55.42679)
#> POINT (89.71598 23.31884)
#> POINT (58.02395 33.78329)

h3_ids <- c("831f91fffffffff", "831fb5fffffffff", "831f94fffffffff")

flatten_h3(
  list(
    NULL,
    h3_from_strings(h3_ids),
    h3_from_strings(h3_ids[1])
  )
)
#> <H3[4]>
#> [1] 831f91fffffffff 831fb5fffffffff 831f94fffffffff 831f91fffffffff