Skip to contents

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

Usage

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 class '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.

Value

See details.

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: 0.7605052 ymin: 15.68115 xmax: 54.05352 ymax: 78.72561
#> Geodetic CRS:  WGS 84
#> POINT (54.05352 26.14981)
#> POINT (14.19107 65.93194)
#> POINT (0.7605052 69.49418)
#> POINT (42.23951 78.72561)
#> POINT (44.75921 15.68115)

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