| Type: | Package |
| Title: | H3 Extension of 'DuckDB' |
| Version: | 0.3.0 |
| Description: | Fast & memory-efficient functions to work with the 'H3' hierarchical hexagonal grid system on large data sets. It leverages the fast analytical capabilities of 'DuckDB' and its community 'h3' extension (see https://duckdb.org/community_extensions/extensions/h3), while maintaining compatibility with R's spatial data ecosystem. |
| URL: | https://cidree.github.io/duckh3/, https://github.com/Cidree/duckh3 |
| BugReports: | https://github.com/Cidree/duckh3/issues |
| Encoding: | UTF-8 |
| License: | GPL (≥ 3) |
| Imports: | cli, bit64, DBI, dbplyr, duckdb (≥ 2.0.0), duckspatial (≥ 1.4.0), glue |
| Config/testthat/edition: | 3 |
| Suggests: | dplyr, knitr, quarto, sf, testthat (≥ 3.0.0) |
| VignetteBuilder: | quarto |
| Config/roxygen2/version: | 8.1.1 |
| NeedsCompilation: | no |
| Packaged: | 2026-11-30 21:04:17 UTC; cidre |
| Author: | Adrián Cidre González
|
| Maintainer: | Adrián Cidre González <adrian.cidre@gmail.com> |
| Depends: | R (≥ 4.1.0) |
| Repository: | CRAN |
| Date/Publication: | 2026-11-30 23:50:02 UTC |
Get an H3 child cell from a parent and child position
Description
Get the child H3 cell at a given position within a parent cell at a specified child resolution.
Usage
ddbh3_child_pos_to_cell(
x,
child_pos = "h3child_pos",
parent = "h3string",
resolution,
new_column = "h3child",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
child_pos |
Character. Name of the column containing the child position
(a whole number, as returned by |
parent |
Character. Name of the column containing the parent H3 cell. |
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Position of a resolution 8 cell within its resolution 6 parent
cells_tbl <- data.frame(h3string = "88283470a1fffff") |>
ddbh3_get_child_pos(resolution = 6) |>
ddbh3_get_parent(resolution = 6)
## Back to the resolution 8 cell from the parent and the position
ddbh3_child_pos_to_cell(cells_tbl, parent = "h3parent", resolution = 8)
Compact and uncompact sets of H3 cells
Description
Compact a set of H3 cells into the smallest set of (possibly coarser) cells covering the same area, or uncompact a set of cells to a single resolution
Usage
ddbh3_compact(
x,
h3 = "h3string",
by = NULL,
new_column = "h3compact",
nested = FALSE,
conn = NULL,
name = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_uncompact(
x,
h3 = "h3string",
resolution,
new_column = "h3uncompact",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
by |
Character vector with the names of the columns used to group the
cells before compacting them (e.g. one compacted set per region). If
|
new_column |
Name of the new column to create on the input data.
Defaults to |
nested |
Logical. If |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
resolution |
A number specifying the resolution of the uncompacted cells (between 0 and 15). It must be equal to or finer than the resolution of every input cell. |
mode |
Character. Controls the return type. Options:
Can be set globally via |
Details
-
ddbh3_compact()is an aggregation: it replaces every complete set of children by their parent, recursively. The output only keeps thebycolumns and the compacted cells, and it is never spatial (useddbh3_h3_to_spatial()to get the hexagons). Duplicated cells are removed before compacting. If a group contains invalid cells, its result isNULL(withnested = TRUE) or the group is dropped (withnested = FALSE). Theh3column can also be a list column of cells. -
ddbh3_uncompact()expands each cell (or each list of cells, ifh3is a list column) to all its descendants atresolution. Cells already atresolutionare kept. It keeps the rest of the columns. If a cell is finer thanresolution, the result for that row isNULL(withnested = TRUE) or the row is dropped (withnested = FALSE).
Inside dplyr, ddbh3_compact(h3) works as an aggregate function in
summarise(), and ddbh3_uncompact(h3, resolution) works in mutate() on
a list column of cells.
Value
For ddbh3_compact(), a tbl_lazy (or TRUE invisibly if name
is provided). For ddbh3_uncompact(), the same outputs as the rest of
the package:
tbl_lazyIf
xis not spatial.modeis ignored in this case.duckspatial_dfIf
xis spatial (e.g. ansforduckspatial_dfobject) andmode = "duckspatial"(default).sfIf
xis spatial andmode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.
In vectorized mode (x is a vector of cells and conn = NULL), both
functions return a vector with the compacted or uncompacted set of cells.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## All the children of two cells, plus one extra cell
cells_tbl <- data.frame(
region = c("A", "B", "B"),
h3string = c("87283472bffffff", "872834729ffffff", "88283470a1fffff")
) |>
ddbh3_get_children(resolution = 8, h3 = "h3string", new_column = "h3children")
## COMPACT ------------------
## Compact all the cells together
ddbh3_compact(cells_tbl, h3 = "h3children")
## Compact the cells of each region
compact_tbl <- ddbh3_compact(cells_tbl, h3 = "h3children", by = "region")
compact_tbl
## One row per region with a list of cells
ddbh3_compact(cells_tbl, h3 = "h3children", by = "region", nested = TRUE)
## Inside summarise
cells_tbl |>
summarise(h3compact = ddbh3_compact(h3children), .by = region)
## UNCOMPACT ----------------
## Back to resolution 8
ddbh3_uncompact(compact_tbl, h3 = "h3compact", resolution = 8)
## Vectorized mode
ddbh3_uncompact("87283472bffffff", resolution = 8) |>
ddbh3_compact()
Create a DuckDB connection with spatial and h3 extensions
Description
It creates a DuckDB connection, and then it installs and loads the spatial and h3 extensions
Usage
ddbh3_create_conn(
dbdir = "memory",
threads = NULL,
memory_limit_gb = NULL,
bigint = "integer64",
...
)
Arguments
dbdir |
String. Either "tempdir", "memory", or file path with .duckdb or .db extension. Defaults to "memory". |
threads |
Integer. Number of threads to use. If |
memory_limit_gb |
Numeric. Memory limit in GB. If |
bigint |
String. How to handle 64-bit integers. One of |
... |
Other parameters passed to |
Value
A duckdb_connection
Examples
# load packages
library(duckspatial)
library(duckh3)
# create a duckdb database in memory
conn <- ddbh3_create_conn(dbdir = "memory", threads = 1)
# create an in-memory connection with 1 thread and 2GB memory limit
conn <- ddbh3_create_conn(threads = 1, memory_limit_gb = 2)
# Create a persistent database in disk
# conn <- ddbh3_create_conn(dbdir = "my_database.duckdb")
ddbs_stop_conn(conn)
Get or create default DuckDB connection
Description
Setup the default connection with h3 and spatial extensions installed and loaded. It will be used internally by the package functions if no other connection is provided
Usage
ddbh3_default_conn(create = TRUE, upgrade_h3 = FALSE, ...)
Arguments
create |
Logical. If TRUE and no connection exists, create one. Default is TRUE. |
upgrade_h3 |
Logical. If TRUE, will attempt to upgrade the h3 extension |
... |
Additional parameters to pass to |
Details
The default connection is shared with duckspatial (it is stored in
options("duckspatial_conn")). It always uses bigint = "integer64", so
H3 indexes stored as UBIGINT are not rounded: if duckspatial already
created a default connection with bigint = "numeric", it is replaced with
a warning. The old connection is not closed, so lazy tables that use it
keep working.
Value
Invisibly, the default duckdb_connection
Examples
## Get or create the default connection
conn <- ddbh3_default_conn(threads = 1)
## Calling it again returns the same connection
identical(conn, ddbh3_default_conn())
Build H3 cells from a base cell and digits
Description
Build H3 cell indexes from a base cell number and a set of indexing digits
Usage
ddbh3_digits_to_h3(
x,
base_cell = "base_cell",
digits = "digits",
new_column = "h3string",
h3_format = "string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
base_cell |
The name of a column in |
digits |
The name of a list column in |
new_column |
Name of the new column to create on the input data.
Defaults to |
h3_format |
Character. The format of the H3 cell index: |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Details
An H3 cell index is made of a base cell (one of the 122 cells of resolution
0, see ddbh3_get_base_cell_number()) and one digit (between 0 and 6) per
resolution, from resolution 1 to the resolution of the cell. Each digit is
the position of the cell among the 7 children of its parent (pentagons have
no digit 1 child).
The resolution of the built cell is the number of digits, so an empty list
returns the base cell itself (resolution 0). The function returns NA when
the base cell or any digit is not valid.
It also works inside dplyr::mutate(), where it always returns H3 strings.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
See Also
ddbh3_get_base_cell_number(), ddbh3_get_child_pos()
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Base cells and digits (the first row is the cell 88283470a1fffff)
digits_df <- data.frame(base_cell = c(20, 20, 0))
digits_df$digits <- list(c(0, 6, 4, 3, 4, 1, 2, 0), c(0, 6, 4), numeric(0))
## Build the cells
ddbh3_digits_to_h3(digits_df)
## As UBIGINT
ddbh3_digits_to_h3(digits_df, h3_format = "bigint")
## Inside mutate
ddbh3_digits_to_h3(digits_df, new_column = "h3cell") |>
mutate(
h3string = ddbh3_digits_to_h3(base_cell, digits),
resolution = ddbh3_get_resolution(h3string)
)
Work with H3 directed edges
Description
Create H3 directed edges from H3 cells, and convert H3 directed edges to cells, lengths and spatial lines. A directed edge goes from an origin cell to one of its neighbors (the destination cell)
Usage
ddbh3_h3_to_edge(
x,
h3start = "h3string",
h3end,
new_column = "h3edge",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_h3_to_edges(
x,
h3 = "h3string",
new_column = "h3edge",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_edge_to_origin(
x,
h3edge = "h3edge",
new_column = "h3origin",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_edge_to_destination(
x,
h3edge = "h3edge",
new_column = "h3destination",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_edge_to_h3(
x,
h3edge = "h3edge",
new_column = "h3cells",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_reverse_edge(
x,
h3edge = "h3edge",
new_column = "h3edge_reversed",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_get_edge_length(
x,
h3edge = "h3edge",
unit = "m",
new_column = "edge_length",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_edge_to_spatial(
x,
h3edge = "h3edge",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3start |
The name of a column in |
h3end |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
h3 |
The name of a column in |
nested |
Logical. If |
h3edge |
Name of the column containing H3 directed edge indexes, as
strings or unsigned 64-bit integers ( |
unit |
Character. Unit of the edge length. One of |
Details
The functions cover the full directed edge workflow:
-
ddbh3_h3_to_edge()returns the directed edge going from the cell inh3startto the cell inh3end. It returnsNAwhen the cells are not neighbors -
ddbh3_h3_to_edges()returns all directed edges going out of an H3 cell (6 for hexagons, 5 for pentagons) — either nested (one row per cell) or unnested (one row per edge) depending onnested -
ddbh3_edge_to_origin()returns the origin cell of a directed edge -
ddbh3_edge_to_destination()returns the destination cell of a directed edge -
ddbh3_edge_to_h3()returns both the origin and the destination cells of a directed edge — either nested (one row per edge) or unnested (one row per cell) depending onnested -
ddbh3_reverse_edge()returns the directed edge going in the opposite direction (from destination to origin) -
ddbh3_get_edge_length()returns the length of a directed edge inunit -
ddbh3_edge_to_spatial()converts directed edges toLINESTRINGgeometries (EPSG:4326) shared by the origin and destination cells
Directed edges are returned in the same format as the input cells: strings
for H3 strings and UBIGINT for H3 unsigned 64-bit integers.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 8)
## CELLS TO EDGES ---------
## All edges going out of each cell (one row per edge)
edges_tbl <- ddbh3_h3_to_edges(points_tbl)
## All edges going out of each cell (one row per cell)
ddbh3_h3_to_edges(points_tbl, nested = TRUE)
## Edge between two neighboring cells
pair_tbl <- data.frame(
h3start = "8a1fb46622dffff",
h3end = "8a1fb46622d7fff"
)
ddbh3_h3_to_edge(pair_tbl, h3start = "h3start", h3end = "h3end")
## EDGES TO CELLS ---------
## Origin and destination cells
edges_tbl |>
ddbh3_edge_to_origin() |>
ddbh3_edge_to_destination()
## Both cells in a list column
ddbh3_edge_to_h3(edges_tbl, nested = TRUE)
## Reverse the direction of the edges
ddbh3_reverse_edge(edges_tbl)
## EDGE PROPERTIES --------
## Length of the edges in kilometers
ddbh3_get_edge_length(edges_tbl, unit = "km")
## Inside mutate
edges_tbl |>
mutate(
origin = ddbh3_edge_to_origin(h3edge),
length = ddbh3_get_edge_length(h3edge, 'm')
)
## EDGES TO SPATIAL -------
## Convert edges to lines
ddbh3_edge_to_spatial(edges_tbl)
Convert polygon or linestring geometries to H3 cell representations
Description
Convert polygon or linestring geometries into the H3 cells that
intersect them, at a specified resolution. Input must be a
duckspatial_df, sf, or table with POLYGON/MULTIPOLYGON or
LINESTRING/MULTILINESTRING geometries
Usage
ddbh3_polygons_to_spatial(
x,
resolution,
containment = "overlap",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_lines_to_spatial(
x,
resolution,
containment = "overlap",
buffer_scale = 1,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_polygons_to_h3(
x,
resolution,
new_column = "h3string",
h3_format = "string",
containment = "overlap",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_lines_to_h3(
x,
resolution,
new_column = "h3string",
h3_format = "string",
containment = "overlap",
buffer_scale = 1,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
containment |
Character. Polyfill containment mode: |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
buffer_scale |
Numeric. Line-buffer size as a multiple of one cell
footprint (default |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
h3_format |
Character. Output format for the H3 cell indexes stored
in |
Details
The four functions differ in the input geometry family and output format:
-
ddbh3_polygons_to_h3()/ddbh3_lines_to_h3()add a list column with the H3 cells intersecting each feature, as strings orUBIGINT -
ddbh3_polygons_to_spatial()/ddbh3_lines_to_spatial()return one row per (feature, intersecting cell), with the geometry replaced by the H3 cell hexagon boundary. Features without any cell (e.g. a polygon smaller than a cell withcontainment = "center", or an empty geometry) are dropped, while*_to_h3()keeps them withNULLinnew_column
Polygons are polyfilled directly with DuckDB's experimental polygon
polyfill using the overlap containment mode by default, so boundary
cells are included. Lines have no native polyfill: a buffer is built
around the line (sized from one cell's footprint in degrees, so it stays
latitude-correct), the buffer is polyfilled, and the candidate cells are
trimmed back to the exact intersecting set with ST_Intersects against
the original line. For lines, containment is therefore applied to the
buffer, not to the line itself: "full" or "center" only keep cells
fully inside, or centred in, the buffer, and still intersecting the line.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(duckspatial)
library(sf)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Build a small polygon and a small line in EPSG:4326
poly_sf <- st_as_sf(
st_sfc(
st_polygon(list(rbind(
c(-0.5, -0.5), c(0.5, -0.5), c(0.5, 0.5), c(-0.5, 0.5), c(-0.5, -0.5)
))),
crs = 4326
)
)
line_sf <- st_as_sf(
st_sfc(
st_linestring(rbind(c(-0.5, -0.5), c(0.5, 0.5))),
crs = 4326
)
)
## POLYGONS TO H3 -------------
## Add a list column with the intersecting h3 strings
poly_h3_tbl <- ddbh3_polygons_to_h3(poly_sf, resolution = 4)
## POLYGONS TO SPATIAL --------
## One row per intersecting cell, geometry = cell hexagon
poly_cells_ddbs <- ddbh3_polygons_to_spatial(poly_sf, resolution = 4)
## LINES TO H3 ----------------
line_h3_tbl <- ddbh3_lines_to_h3(line_sf, resolution = 4)
## LINES TO SPATIAL -----------
line_cells_ddbs <- ddbh3_lines_to_spatial(line_sf, resolution = 4)
Get the area of H3 cells
Description
Get the exact area of H3 cell indexes stored as strings or unsigned 64-bit integers
Usage
ddbh3_get_area(
x,
h3 = "h3string",
unit = "m^2",
new_column = "h3area",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
unit |
Character. Unit of the area. One of |
new_column |
Name of the new column to create on the input data.
Defaults to |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Details
The area is computed for each individual cell, so it differs from the
average cell area at a resolution returned by
ddbh3_get_hexagon_area_avg(). Cells of the same resolution have different
areas depending on their location on the globe.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
See Also
ddbh3_get_hexagon_area_avg(), ddbh3_get_edge_length()
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 8)
## Area of the cells in square meters
ddbh3_get_area(points_tbl)
## Area in square kilometers
ddbh3_get_area(points_tbl, unit = "km^2")
## Inside mutate
points_tbl |>
mutate(area = ddbh3_get_area(h3string, 'km^2'))
## Vectorized mode
ddbh3_get_area("8a1fb46622dffff")
Get the base cell number of an H3 cell
Description
Get the base cell number (ranging from 0 to 121) of H3 cell indexes stored
as strings or unsigned 64-bit integers (UBIGINT). Every H3 cell descends
from one of these foundational global base cells.
Usage
ddbh3_get_base_cell_number(
x,
h3 = "h3string",
new_column = "base_cell",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add H3 string column
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 6)
## Get the base cell number
ddbh3_get_base_cell_number(points_tbl)
Get the position of an H3 cell within its parent
Description
Get the position of H3 cell indexes stored as strings or unsigned 64-bit
integers (UBIGINT) within their parent cell at a specified resolution.
The position is a zero-based index among all children of the parent cell.
Usage
ddbh3_get_child_pos(
x,
resolution,
h3 = "h3string",
new_column = "h3child_pos",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add H3 string column
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 6)
## Get position relative to resolution 4
ddbh3_get_child_pos(points_tbl, resolution = 4)
Get the average area and edge length of H3 hexagons
Description
Get the average area or the average edge length of H3 hexagons at one or more resolutions
Usage
ddbh3_get_hexagon_area_avg(resolution, unit = "m^2", conn = NULL)
ddbh3_get_hexagon_edge_length_avg(resolution, unit = "m", conn = NULL)
Arguments
resolution |
A vector of integers between 0 and 15 with the H3 resolutions |
unit |
Character. Unit of the result. For
|
conn |
A connection object to a DuckDB database. Required when |
Details
These functions do not take H3 cells as input; they return a constant per
resolution. To get the exact area of a cell or the length of a directed
edge, use ddbh3_get_area() or ddbh3_get_edge_length().
They also work inside dplyr::mutate() with a column of resolutions.
Value
A numeric vector with one value per element of resolution
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Average hexagon area at resolution 8, in km2
ddbh3_get_hexagon_area_avg(8, unit = "km^2")
## Average edge length at all resolutions
ddbh3_get_hexagon_edge_length_avg(0:15)
## Inside mutate
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
) |>
ddbh3_lonlat_to_h3(resolution = 8)
points_tbl |>
mutate(
area = ddbh3_get_area(h3string),
area_avg = ddbh3_get_hexagon_area_avg(ddbh3_get_resolution(h3string))
)
Get parent and children H3 cells
Description
Get the parent or children H3 cells of H3 cell indexes stored as strings or
unsigned 64-bit integers (UBIGINT) at a specified resolution:
ddbh3_get_parent(), ddbh3_get_center_child(), ddbh3_get_children(),
and ddbh3_get_n_children().
Usage
ddbh3_get_parent(
x,
resolution,
h3 = "h3string",
new_column = "h3parent",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_get_children(
x,
resolution,
h3 = "h3string",
new_column = "h3children",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_get_n_children(
x,
resolution,
h3 = "h3string",
new_column = "h3n_children",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_get_center_child(
x,
resolution,
h3 = "h3string",
new_column = "h3center_child",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
nested |
Logical. If |
Details
The four functions differ in the type of related cell they retrieve:
-
ddbh3_get_parent()returns the parent cell at a coarser resolution -
ddbh3_get_center_child()returns the center child cell at a finer resolution -
ddbh3_get_children()returns all children cells at a finer resolution -
ddbh3_get_n_children()returns the number of children cells at a finer resolution, without computing them
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 8)
## GET PARENTS ----------
## Get resolution-7 parent
points_parent_tbl <- ddbh3_get_parent(points_tbl, resolution = 7)
## Check the resolution
ddbh3_get_resolution(
points_parent_tbl,
h3 = "h3parent"
)
## Add with mutate
points_tbl |>
mutate(parent4 = ddbh3_get_parent(h3string, 4))
## GET CHILDREN ----------
## Get level 9 children
children_9_tbl <- ddbh3_get_children(points_tbl, resolution = 9)
## Get level 9 children in a nested list
children_9_nested_tbl <- ddbh3_get_children(points_tbl, resolution = 9, nested = TRUE)
## Add with mutate (nested)
points_tbl |>
mutate(children9 = ddbh3_get_children(h3string, 9))
## Add with mutate (unnested)
points_tbl |>
mutate(children9 = ddbh3_get_children(h3string, 9)) |>
mutate(children9 = unnest(children9))
## GET CENTER CHILD ------
## Get the center child of res 10 (1 child per row)
center_child_10_tbl <- ddbh3_get_center_child(points_tbl, resolution = 10)
## Add with mutate
points_tbl |>
mutate(center = ddbh3_get_center_child(h3string, 9))
## NUMBER OF CHILDREN -----
## How many children of level 10 does each level 8 have?
n_children_tbl <- ddbh3_get_n_children(points_tbl, resolution = 10)
## Add with mutate
points_tbl |>
mutate(n_children = ddbh3_get_n_children(h3string, 15))
Get the icosahedron faces of H3 cell indexes
Description
Get the icosahedron faces intersected by H3 cell indexes stored as strings
or unsigned 64-bit integers (UBIGINT). Each H3 cell maps onto one or more
of the 20 faces of the underlying icosahedron used to construct the H3 grid.
Usage
ddbh3_get_icosahedron_faces(
x,
h3 = "h3string",
new_column = "h3faces",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
nested |
Logical. If |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add H3 string column
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 6)
## Get faces (unnested)
faces_tbl <- ddbh3_get_icosahedron_faces(points_tbl)
## Get faces (nested)
faces_nested_tbl <- ddbh3_get_icosahedron_faces(points_tbl, nested = TRUE)
## Add using mutate (nested)
points_tbl |>
mutate(faces = ddbh3_get_icosahedron_faces(h3string))
## Add using mutate (unnested)
points_tbl |>
mutate(faces = ddbh3_get_icosahedron_faces(h3string)) |>
mutate(faces_unnested = unnest(faces))
Get the resolution of H3 cell indexes
Description
Get the resolution of H3 cell indexes stored as strings or unsigned 64-bit
integers (UBIGINT) from an existing column.
Usage
ddbh3_get_resolution(
x,
h3 = "h3string",
new_column = "h3resolution",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(duckspatial)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 10)
## Convert to duckspatial_df
points_ddbs <- ddbs_as_points(points_tbl)
## Get resolution of the h3 strings
ddbh3_get_resolution(points_tbl)
ddbh3_get_resolution(points_ddbs, new_column = "res")
## Add using mutate
points_tbl |>
mutate(res = ddbh3_get_resolution(h3string))
Get the great circle distance between two points
Description
Get the great circle (haversine) distance between two points given by their longitude and latitude
Usage
ddbh3_great_circle_distance(
x,
lon_start,
lat_start,
lon_end,
lat_end,
unit = "m",
new_column = "distance",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
lon_start, lat_start |
The names of the columns in |
lon_end, lat_end |
The names of the columns in |
unit |
Character. Unit of the distance. One of |
new_column |
Name of the new column to create on the input data.
Defaults to |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Two pairs of points
pairs_tbl <- data.frame(
lon1 = c(-3.70, -8.54),
lat1 = c(40.42, 42.88),
lon2 = c(2.17, -8.41),
lat2 = c(41.39, 43.36)
)
## Distance in kilometers
ddbh3_great_circle_distance(
pairs_tbl,
lon_start = "lon1",
lat_start = "lat1",
lon_end = "lon2",
lat_end = "lat2",
unit = "km"
)
## Inside mutate
pairs_tbl |>
ddbh3_great_circle_distance("lon1", "lat1", "lon2", "lat2") |>
mutate(dist_km = ddbh3_great_circle_distance(lon1, lat1, lon2, lat2, 'km'))
Get H3 grid neighborhoods (disks, rings, and distances)
Description
Functions to generate surrounding H3 neighborhood cells based on step-by-step grid distances (k-rings and hollow rings).
Usage
ddbh3_grid_disk(
x,
k = 1,
h3 = "h3string",
new_column = "h3neighbors",
nested = FALSE,
safe = TRUE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_grid_disk_distances(
x,
k = 1,
h3 = "h3string",
new_column = c("h3neighbors", "h3distance"),
nested = FALSE,
safe = TRUE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_grid_ring(
x,
k = 1,
h3 = "h3string",
new_column = "h3neighbors",
nested = FALSE,
safe = TRUE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
k |
Integer. The grid distance (number of neighbor hops), between 0 and
1500. Defaults to |
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If
the input already has a column with this name, it is replaced by the
result. In |
nested |
Logical. If |
safe |
Logical. If |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Details
These functions differ in how they traverse and select neighborhood cells:
-
ddbh3_grid_disk(): Returns all H3 cells within a grid distancekfrom the origin cell (inclusive of all rings from distance 0 up tok). -
ddbh3_grid_disk_distances(): Returns the same cells asddbh3_grid_disk(), ordered by distance to the origin, plus a second column with the grid distance (0 tok) of each cell. Withnested = TRUE, both columns are lists. In vectorized mode, it returns one list per input cell withk + 1elements: the cells at distance 0, 1, ...,k. -
ddbh3_grid_ring(): Returns only the exact hollow ring of H3 cells located at the specific distancekfrom the origin cell, excluding all inner cells.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
library(duckh3)
library(dplyr)
ddbh3_default_conn(threads = 1)
points_tbl <- read.csv(system.file("extdata/example_pts.csv", package = "duckh3"))
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 6)
# Get grid disk (k = 1)
ddbh3_grid_disk(points_tbl, k = 1)
# Same cells, with their grid distance to the origin
ddbh3_grid_disk_distances(points_tbl, k = 1)
# Vectorized mode: one list of cells per distance
ddbh3_grid_disk_distances("8928308280fffff", k = 1)
# Only the cells at exactly k = 2 steps (hollow ring), nested
ddbh3_grid_ring(points_tbl, k = 2, nested = TRUE)
Get the grid distance between two H3 cells
Description
Computes the minimum number of step-by-step neighbor hops required to travel from a starting H3 cell to a destination H3 cell.
Usage
ddbh3_grid_distance(
x,
h3start = "h3string",
h3end,
new_column = "grid_distance",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3start |
The name of a column in |
h3end |
The name of a column in |
new_column |
Character. Name of the new column to be created.
Defaults to |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load packages
library(duckh3)
library(duckspatial)
ddbh3_default_conn(threads = 1)
## Create a data frame with the start and end of the path
h3_tbl <- data.frame(
h3start = "883923059bfffff",
h3end = "883923d04dfffff"
)
## Get the number of cells distance between them
ddbh3_grid_distance(
h3_tbl,
h3start = "h3start",
h3end = "h3end"
)
Find a grid path connecting two H3 cells
Description
Given a starting H3 cell and a destination H3 cell, returns a minimal-length contiguous path of cells between them (inclusive).
Usage
ddbh3_grid_path_cells(
x,
h3start = "h3string",
h3end,
new_column = "h3path",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3start |
The name of a column in |
h3end |
The name of a column in |
new_column |
Character. Name of the new column to be created.
Defaults to |
nested |
Logical. If |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load packages
library(duckh3)
library(duckspatial)
ddbh3_default_conn(threads = 1)
## Create a data frame with the start and end of the path
h3_tbl <- data.frame(
h3start = "883923059bfffff",
h3end = "883923d04dfffff"
)
## Get the cells of the path between them
h3path_tbl <- ddbh3_grid_path_cells(
h3_tbl,
h3start = "h3start",
h3end = "h3end"
)
## Convert path to spatial
h3path_sf <- ddbh3_h3_to_spatial(h3path_tbl, "h3path") |>
ddbs_collect()
## Convert start and end to spatial
start_sf <- ddbh3_h3_to_spatial(h3_tbl, "h3start") |>
ddbs_collect()
end_sf <- ddbh3_h3_to_spatial(h3_tbl, "h3end") |>
ddbs_collect()
## Visualize the path
if (requireNamespace("sf", quietly = TRUE)) {
plot(sf::st_geometry(h3path_sf))
plot(sf::st_geometry(start_sf), add = TRUE, col = "red")
plot(sf::st_geometry(end_sf), add = TRUE, col = "blue")
}
Convert H3 string or UBIGINT indexes to other representations
Description
Convert H3 cell indexes stored as strings or UBIGINT into other
representations (e.g. lon, lat, spatial)
Usage
ddbh3_h3_to_lon(
x,
h3 = "h3string",
new_column = "lon",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_h3_to_lat(
x,
h3 = "h3string",
new_column = "lat",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_h3_to_spatial(
x,
h3 = "h3string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_strings_to_bigint(
x,
h3 = "h3string",
new_column = "h3bigint",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_bigint_to_strings(
x,
h3 = "h3bigint",
new_column = "h3string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_h3_to_points(
x,
h3 = "h3string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Details
The six functions differ only in the output format:
-
ddbh3_h3_to_spatial()converts H3 indexes to spatial hexagon polygons -
ddbh3_h3_to_lon()extracts the longitude of the H3 cell centroid -
ddbh3_h3_to_lat()extracts the latitude of the H3 cell centroid -
ddbh3_strings_to_bigint()converts H3 indexes to unsigned 64-bit integers (UBIGINT) -
ddbh3_bigint_to_strings()converts H3 indexes to strings (e.g."8928308280fffff") -
ddbh3_h3_to_points()converts H3 indexes to spatial points located at the centroid of the H3 cell
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(duckspatial)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add H3 string column
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 6) |>
select(-lon, -lat)
## TO LON/LAT ------------
## Add longitude and latitude of the H3 string
points_coords_tbl <- points_tbl |>
ddbh3_h3_to_lat() |>
ddbh3_h3_to_lon()
## Add lon/lat with other names
points_coords_2_tbl <- points_tbl |>
ddbh3_h3_to_lat(new_column = "latitude") |>
ddbh3_h3_to_lon(new_column = "longitude")
## Add using mutate
points_tbl |>
mutate(
lon = ddbh3_h3_to_lon(h3string),
lat = ddbh3_h3_to_lat(h3string)
)
## TO SPATIAL -----------------
## Convert h3 strings to spatial polygons
points_ddbs <- ddbh3_h3_to_spatial(points_tbl)
## Collect as sf
points_sf <- ddbs_collect(points_ddbs)
## FROM STRING TO UBIGINT -----
## Add ubigint, and remove strings
points_bigint_tbl <- ddbh3_strings_to_bigint(
points_tbl,
new_column = "h3_integers"
) |>
select(-h3string)
## Add using mutate
points_tbl |>
mutate(h3int = ddbh3_strings_to_bigint(h3string))
## FROM UBIGINT TO STRING -----
## Add column with strings
points_strings_tbl <- ddbh3_bigint_to_strings(
points_bigint_tbl,
h3 = "h3_integers"
)
## Add using mutate
points_bigint_tbl |>
mutate(h3string = ddbh3_bigint_to_strings(h3_integers))
Check properties of H3 cell indexes
Description
Check properties of H3 cell indexes stored as strings or unsigned 64-bit integers
Usage
ddbh3_is_pentagon(
x,
h3 = "h3string",
new_column = "ispentagon",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_is_h3(
x,
h3 = "h3string",
new_column = "ish3",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_is_res_class_iii(
x,
h3 = "h3string",
new_column = "isclassiii",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_is_vertex(
x,
h3vertex = "h3vertex",
new_column = "isvertex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_is_edge(
x,
h3edge = "h3edge",
new_column = "isedge",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_is_index(
x,
h3 = "h3string",
new_column = "isindex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
h3vertex |
Name of the column containing H3 vertex strings. Defaults
to |
h3edge |
Name of the column containing H3 directed edge indexes, as
strings or unsigned 64-bit integers ( |
Details
The functions check different properties of H3 cell indexes, all returning a logical column:
-
ddbh3_is_pentagon(): returnsTRUEif the H3 cell is one of the 12 pentagonal cells that exist at each H3 resolution -
ddbh3_is_h3(): returnsTRUEif the H3 cell index is a valid H3 cell -
ddbh3_is_res_class_iii(): returnsTRUEif the H3 cell belongs to a Class III resolution (odd resolutions: 1, 3, 5, 7, 9, 11, 13, 15) -
ddbh3_is_vertex(): returnsTRUEif the index is a valid H3 vertex -
ddbh3_is_edge(): returnsTRUEif the index is a valid H3 directed edge -
ddbh3_is_index(): returnsTRUEif the index is any valid H3 index (cell, directed edge or vertex)
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 8)
## IS VALID H3 -------------
## Check if h3 indexes are valid
ddbh3_is_h3(points_tbl)
## Check in mutate
points_tbl |>
mutate(valid = ddbh3_is_h3(h3string))
## IS PENTAGON -------------
## Check if h3 indexes are pentagons
ddbh3_is_pentagon(points_tbl)
## Check in mutate
points_tbl |>
mutate(is_pent = ddbh3_is_pentagon(h3string))
## IS CLASS III ------------
## Check if h3 indexes belong to a Class III resolution
ddbh3_is_res_class_iii(points_tbl)
## Check across multiple resolutions
ddbh3_lonlat_to_h3(points_tbl, resolution = 7) |>
ddbh3_is_res_class_iii()
## IS VERTEX ---------------
## Get vertexes first
vertex_tbl <- ddbh3_h3_to_vertex(points_tbl, n = 1)
## Check if indexes are valid vertexes
ddbh3_is_vertex(vertex_tbl)
## Check in mutate (mix of h3 cells and vertexes)
vertex_tbl |>
mutate(
cell_valid = ddbh3_is_h3(h3string),
vertex_valid = ddbh3_is_vertex(h3vertex)
)
## IS EDGE -----------------
## Get directed edges first
edges_tbl <- ddbh3_h3_to_edges(points_tbl)
## Check if indexes are valid directed edges
ddbh3_is_edge(edges_tbl)
## Check in mutate
edges_tbl |>
mutate(edge_valid = ddbh3_is_edge(h3edge))
## IS INDEX ----------------
## Cells and edges are both valid indexes
edges_tbl |>
ddbh3_is_index(new_column = "cell_index") |>
ddbh3_is_index(h3 = "h3edge", new_column = "edge_index")
## Check in mutate
edges_tbl |>
mutate(valid = ddbh3_is_index(h3edge))
Check if two H3 cells are neighbors
Description
Checks whether two H3 cells share an edge (i.e. they are directly adjacent). Cells at different resolutions are never neighbors.
Usage
ddbh3_is_neighbor(
x,
h3start = "h3string",
h3end,
new_column = "isneighbor",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
h3start |
The name of a column in |
h3end |
The name of a column in |
new_column |
Character. Name of the new column to be created.
Defaults to |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load packages
library(duckh3)
library(dplyr)
ddbh3_default_conn(threads = 1)
## Create a data frame with pairs of cells
h3_tbl <- data.frame(
h3start = c("8a1fb46622dffff", "883923059bfffff"),
h3end = c("8a1fb46622d7fff", "883923d04dfffff")
)
## Check if they are neighbors
ddbh3_is_neighbor(h3_tbl, h3start = "h3start", h3end = "h3end")
## Check in mutate
h3_tbl |>
ddbh3_is_neighbor(h3start = "h3start", h3end = "h3end") |>
mutate(edge = ddbh3_h3_to_edge(h3start, h3end))
Work with H3 local IJ coordinates
Description
Convert H3 cells to local IJ coordinates relative to an origin cell, and local IJ coordinates back to H3 cells
Usage
ddbh3_h3_to_local_ij(
x,
origin,
h3 = "h3string",
new_column = c("i", "j"),
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_local_ij_to_h3(
x,
origin,
i = "i",
j = "j",
new_column = "h3cell",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
origin |
The name of a column in |
h3 |
The name of a column in |
new_column |
Name of the new column(s) to create on the input data. For
|
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
i, j |
The names of the columns in |
Details
Local IJ coordinates are integer coordinates of a cell in a 2D grid anchored at an origin cell. They are useful to work with cells as a regular grid (e.g. offsets between cells or image-like operations):
-
ddbh3_h3_to_local_ij()adds two integer columns with the I and J coordinates of the cell inh3relative to the cell inorigin -
ddbh3_local_ij_to_h3()returns the cell at the I and J coordinates relative to the cell inorigin. The cell is returned in the same format asorigin(string orUBIGINT)
Local IJ coordinates are only defined near the origin: the origin and the
cell must have the same resolution and be close enough to each other (and
not cross pentagon distortion). Otherwise, the result is NA. The origin
is not placed at (0, 0), and the coordinates are only comparable between
cells that share the same origin.
Both functions work inside dplyr::mutate(). There,
ddbh3_h3_to_local_ij(origin, h3) returns a list column with the [i, j]
pair; use ij[[1]] and ij[[2]] to extract each coordinate.
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Cells within 2 steps of an origin cell
cells_tbl <- data.frame(origin = "88283470a1fffff") |>
ddbh3_grid_disk(k = 2, h3 = "origin", new_column = "h3string")
## CELLS TO LOCAL IJ ------
## Local IJ coordinates relative to the origin
ij_tbl <- ddbh3_h3_to_local_ij(cells_tbl, origin = "origin")
ij_tbl
## Custom column names
ddbh3_h3_to_local_ij(cells_tbl, origin = "origin", new_column = c("col", "row"))
## LOCAL IJ TO CELLS ------
## Back to the original cells
ddbh3_local_ij_to_h3(ij_tbl, origin = "origin")
## Shift the cells one step along the I axis
ij_tbl |>
mutate(i = i + 1L) |>
ddbh3_local_ij_to_h3(origin = "origin", new_column = "h3shifted")
## Inside mutate (`ij[[1]]` extracts the I coordinate)
ij_tbl |>
mutate(
ij = ddbh3_h3_to_local_ij(origin, h3string),
i_mutate = ij[[1]],
h3cell = ddbh3_local_ij_to_h3(origin, i, j)
)
Convert longitude and latitude to H3 cell representations
Description
Convert geographic coordinates (longitude and latitude) into H3 cell representations at a specified resolution, with different output formats
Usage
ddbh3_lonlat_to_spatial(
x,
lon = "lon",
lat = "lat",
resolution,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_lonlat_to_h3(
x,
lon = "lon",
lat = "lat",
resolution,
new_column = "h3string",
h3_format = "string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
lon |
The name of a column in |
lat |
The name of a column in |
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
h3_format |
Character. The format of the H3 cell index: |
Details
The two functions differ only in the output format of the H3 cell index:
-
ddbh3_lonlat_to_h3()returns H3 cell indexes as strings (e.g."8928308280fffff") or as unsigned 64-bit integers (UBIGINT) -
ddbh3_lonlat_to_spatial()returns H3 cells as spatial hexagon polygons
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckdb)
library(duckh3)
library(duckspatial)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Create a connection with spatial and h3 extensions
conn <- ddbh3_create_conn(threads = 1)
## TO H3 ------------
## Add h3 strings as a new column (res 5)
points_strings_5_tbl <- ddbh3_lonlat_to_h3(
points_tbl,
resolution = 5
)
## Add h3 UBIGINT as a new column (res 8)
points_bigint_8_tbl <- ddbh3_lonlat_to_h3(
points_tbl,
resolution = 8,
new_column = "h3bigint",
h3_format = "bigint"
)
## If column names are different from lon/lat:
points_renamed <- rename(points_tbl, long = lon, lati = lat)
ddbh3_lonlat_to_h3(
points_renamed,
lon = "long",
lat = "lati",
resolution = 10
)
## Create a new table in the connection
ddbh3_lonlat_to_h3(
points_tbl,
resolution = 8,
conn = conn,
name = "points_strings_8"
)
## Open the created table lazily
points_lazy <- dplyr::tbl(conn, "points_strings_8")
## Read it in memory
points_eager <- dbReadTable(conn, "points_strings_8")
## TO SPATIAL -----------
## Add h3 strings as a new column (res 5)
points_5_ddbs <- ddbh3_lonlat_to_spatial(
points_tbl,
resolution = 5
)
## Create a new table in the connection
ddbh3_lonlat_to_spatial(
points_tbl,
resolution = 8,
conn = conn,
name = "points_strings_spatial"
)
## Open the created table lazily
as_duckspatial_df("points_strings_spatial", conn)
## Read it in memory as an sf object
ddbs_read_table(conn, "points_strings_spatial")
Get the maximum grid disk size for a given distance k
Description
Computes the theoretical maximum number of cells contained within a
grid disk of a specified grid distance k.
Usage
ddbh3_max_grid_disk_size(k = 1, conn = NULL)
Arguments
k |
Integer. The maximum grid distance (k-ring radius). |
conn |
A DuckDB connection object. If |
Value
An integer scalar representing the maximum number of cells.
Examples
library(duckh3)
ddbh3_max_grid_disk_size(k = 2)
Convert spatial points to H3 cell representations
Description
Convert spatial point geometries into H3 cell representations at a specified
resolution. Input must be an duckspatial_df, sf, or table with point
geometries
Usage
ddbh3_points_to_spatial(
x,
resolution,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_points_to_h3(
x,
resolution,
new_column = "h3string",
h3_format = "string",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
resolution |
A number specifying the resolution level of the H3
cells (between 0 and 15). For hierarchy functions it is relative to the
input cells: it must be coarser than (for parents) or finer than (for
children) the resolution of each cell, otherwise the result is |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
h3_format |
Character. Output format for the H3 cell index. Either
|
Details
The two functions differ in the output format:
-
ddbh3_points_to_h3()returns the H3 cell index containing each point, either as a string orUBIGINTdepending onh3_format -
ddbh3_points_to_spatial()returns the H3 cell hexagon polygon containing each point as a spatial geometry
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(duckspatial)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Convert to duckspatial_df
points_ddbs <- ddbs_as_points(points_tbl)
## TO H3 strings/ubigint ------------
## Add column with h3 strings at resolution 8
points_strings_ddbs <- ddbh3_points_to_h3(points_ddbs, 8)
## Add column with h3 ubigint at resolution 8
points_bigint_ddbs <- ddbh3_points_to_h3(
points_ddbs,
resolution = 8,
new_column = "h3bigint",
h3_format = "bigint"
)
## TO SPATIAL -----------------
## Convert from POINTS to H3 POLYGONS of res 8
polygons_8_ddbs <- ddbh3_points_to_spatial(points_ddbs, 8)
## Collect as sf
polygons_8_sf <- ddbs_collect(polygons_8_ddbs)
Convert H3 cell indexes to vertex representations
Description
Convert H3 cell indexes stored as strings or unsigned 64-bit integers
(UBIGINT) to their vertex representations
Usage
ddbh3_h3_to_vertex(
x,
n = 0,
h3 = "h3string",
new_column = "h3vertex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_vertex_to_lon(
x,
h3vertex = "h3vertex",
new_column = "lon_vertex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_vertex_to_lat(
x,
h3vertex = "h3vertex",
new_column = "lat_vertex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_h3_to_vertexes(
x,
h3 = "h3string",
new_column = "h3vertex",
nested = FALSE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
ddbh3_vertex_to_spatial(
x,
h3vertex = "h3vertex",
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE
)
Arguments
x |
Input data. One of:
|
n |
Integer. Vertex number to retrieve. Must be in the range 0–5 for
hexagons and 0–4 for pentagons. Only used in |
h3 |
The name of a column in |
new_column |
Name of the new column to create on the input data. If the input already has a column with this name, it is replaced by the result |
conn |
A connection object to a DuckDB database. Required when |
name |
A character string of length one specifying the name of the table
to create in |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
h3vertex |
Name of the column containing H3 vertex strings. Defaults
to |
nested |
Logical. If |
Details
The functions cover the full vertex workflow:
-
ddbh3_h3_to_vertex()returns a single vertex of an H3 cell, identified by its vertex numbern(0–5 for hexagons, 0–4 for pentagons), as an H3 vertex string -
ddbh3_h3_to_vertexes()returns all vertices of an H3 cell as H3 vertex strings — either nested (one row per cell) or unnested (one row per vertex) depending onnested -
ddbh3_vertex_to_lat()returns the latitude of an H3 vertex string -
ddbh3_vertex_to_lon()returns the longitude of an H3 vertex string -
ddbh3_vertex_to_spatial()converts H3 vertex strings to spatial point geometries. If the input column is nested, vertices are automatically unnested and aggregated into aMULTIPOINTgeometry per cell
Value
One of the following, depending on the inputs:
tbl_lazyIf the output has no geometry, i.e.
xis not spatial and the function adds a column.modeis ignored in this case.duckspatial_dfIf the output has geometry (
xis spatial, e.g. ansforduckspatial_dfobject, or the function builds geometries, as*_to_spatial()andddbh3_h3_to_points()do) andmode = "duckspatial"(default).sfIf the output has geometry and
mode = "sf".TRUE(invisibly)If
nameis provided, a table is created in the connection andTRUEis returned invisibly.- vector
If
xis a vector of indexes (character, orbit64::integer64) andconn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length asx. Not every function has a vectorized mode. The*_to_spatial()functions andddbh3_h3_to_points()followmodeinstead: aduckspatial_df(mode = "duckspatial") or ansf(mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element ofx.
Examples
## Load needed packages
library(duckh3)
library(duckspatial)
library(dplyr)
## Optional: set up the default connection (e.g. to limit the threads)
ddbh3_default_conn(threads = 1)
## Load example data
points_tbl <- read.csv(
system.file("extdata/example_pts.csv", package = "duckh3")
)
## Add h3 strings
points_tbl <- ddbh3_lonlat_to_h3(points_tbl, resolution = 8)
## TO VERTEX ---------------
## Add second vertex
vertex_2_tbl <- ddbh3_h3_to_vertex(points_tbl, n = 2)
## Add all vertexes (unnested)
vertexes_tbl <- ddbh3_h3_to_vertexes(points_tbl)
## Add all vertexes (nested)
vertexes_nested_tbl <- ddbh3_h3_to_vertexes(points_tbl, nested = TRUE)
## Add some vertexes with mutate
points_tbl |>
mutate(
v1 = ddbh3_h3_to_vertex(h3string, 1),
v3 = ddbh3_h3_to_vertex(h3string, 3)
)
## VERTEX TO LON/LAT ------
## Add coords
coords_vertex_tbl <- vertex_2_tbl |>
ddbh3_vertex_to_lon(new_column = "lon_v2") |>
ddbh3_vertex_to_lat(new_column = "lat_v2")
## Add coords in mutate
vertex_2_tbl |>
mutate(
lon_v2 = ddbh3_vertex_to_lon(h3vertex),
lat_v2 = ddbh3_vertex_to_lat(h3vertex)
)
## VERTEX TO SPATIAL ------
## Convert unnested vertexes (returns POINTS)
ddbh3_h3_to_vertexes(points_tbl) |>
ddbh3_vertex_to_spatial()
## Convert nested vertexes (returns MULTIPOINTS)
ddbh3_h3_to_vertexes(points_tbl, nested = TRUE) |>
ddbh3_vertex_to_spatial()