Package {duckh3}


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 ORCID iD [aut, cre], Egor Kotov ORCID iD [aut], Rafael H. M. Pereira ORCID iD [aut]
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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

child_pos

Character. Name of the column containing the child position (a whole number, as returned by ddbh3_get_child_pos()).

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 NA (or empty).

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 NULL (default), all the cells of x are compacted together.

new_column

Name of the new column to create on the input data. Defaults to "h3compact" for ddbh3_compact() and "h3uncompact" for ddbh3_uncompact().

nested

Logical. If TRUE, the cells are returned as a nested list column (one row per group in ddbh3_compact(), one row per input row in ddbh3_uncompact()). If FALSE (default), the result is unnested so each cell occupies its own row.

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

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:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

Details

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_lazy

If x is not spatial. mode is ignored in this case.

duckspatial_df

If x is spatial (e.g. an sf or duckspatial_df object) and mode = "duckspatial" (default).

sf

If x is spatial and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is 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 NULL (default), the setting is not changed, and DuckDB engine will use all available cores it detects (warning, on some shared HPC nodes the detected number of cores might be total number of cores on the node, not the per-job allocation).

memory_limit_gb

Numeric. Memory limit in GB. If NULL (default), the setting is not changed, and DuckDB engine will use 80% of available operating system memory it detects (warning, on some shared HPC nodes the detected memory might be the full node memory, not the per-job allocation).

bigint

String. How to handle 64-bit integers. One of "integer64" (default) or "numeric". Use "integer64": with "numeric", H3 indexes stored as UBIGINT are silently rounded.

...

Other parameters passed to duckspatial::ddbs_create_conn()

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 duckspatial::ddbs_create_conn() (e.g. threads). If a default connection already exists, threads and memory_limit_gb are applied to it, and the rest are ignored with a warning. bigint is always ignored (with a warning).

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

base_cell

The name of a column in x containing the base cell numbers (between 0 and 121)

digits

The name of a list column in x containing the digits of the cells (integers between 0 and 6, one per resolution)

new_column

Name of the new column to create on the input data. Defaults to "h3string". 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: "string" (default) or "bigint"

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

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_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3start

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). The path starts on this cell

h3end

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). It's the destination cell of the path

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

nested

Logical. If TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

h3edge

Name of the column containing H3 directed edge indexes, as strings or unsigned 64-bit integers (UBIGINT). Defaults to "h3edge"

unit

Character. Unit of the edge length. One of "m" (default), "km" or "rads". Only used in ddbh3_get_edge_length().

Details

The functions cover the full directed edge workflow:

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_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

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 NA (or empty).

containment

Character. Polyfill containment mode: "overlap" (default), "full", "center", or "overlap_bbox". See the DuckDB H3 extension documentation for details on each mode.

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

buffer_scale

Numeric. Line-buffer size as a multiple of one cell footprint (default 1). Only used in ddbh3_lines_to_h3() and ddbh3_lines_to_spatial(), since a line has zero width and therefore no native polyfill: candidate cells are found by buffering the line, and the result is trimmed back to the exact intersecting set. Larger values are safer (slower) but never change correctness.

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 new_column. Either "string" (default) or "bigint". Only used in ddbh3_polygons_to_h3() and ddbh3_lines_to_h3().

Details

The four functions differ in the input geometry family and output format:

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_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

unit

Character. Unit of the area. One of "m^2" (default), "km^2" or "rads^2".

new_column

Name of the new column to create on the input data. Defaults to "h3area".

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

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_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

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 NA (or empty).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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 ddbh3_get_hexagon_area_avg(), one of "m^2" (default) or "km^2". For ddbh3_get_hexagon_edge_length_avg(), one of "m" (default) or "km".

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

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 NA (or empty).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

nested

Logical. If TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

Details

The four functions differ in the type of related cell they retrieve:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

lon_start, lat_start

The names of the columns in x containing the longitude and latitude of the start points

lon_end, lat_end

The names of the columns in x containing the longitude and latitude of the end points

unit

Character. Unit of the distance. One of "m" (default), "km" or "rads".

new_column

Name of the new column to create on the input data. Defaults to "distance".

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

k

Integer. The grid distance (number of neighbor hops), between 0 and 1500. Defaults to 1.

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 ddbh3_grid_disk_distances(), two names: one for the cells and one for their grid distance to the origin (defaults to c("h3neighbors", "h3distance")).

nested

Logical. If TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

safe

Logical. If TRUE (default), uses the safe algorithm, which correctly handles pentagons. If FALSE, uses the faster unsafe algorithm, which returns no cells when it hits a pentagon (so, with nested = FALSE, those rows are dropped).

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Details

These functions differ in how they traverse and select neighborhood cells:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3start

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). The path starts on this cell

h3end

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). It's the destination cell of the path

new_column

Character. Name of the new column to be created. Defaults to "grid_distance".

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3start

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). The path starts on this cell

h3end

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). It's the destination cell of the path

new_column

Character. Name of the new column to be created. Defaults to "h3path".

nested

Logical. If TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Details

The six functions differ only in the output format:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

h3vertex

Name of the column containing H3 vertex strings. Defaults to "h3vertex"

h3edge

Name of the column containing H3 directed edge indexes, as strings or unsigned 64-bit integers (UBIGINT). Defaults to "h3edge"

Details

The functions check different properties of H3 cell indexes, all returning a logical column:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

h3start

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). The path starts on this cell

h3end

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT). It's the destination cell of the path

new_column

Character. Name of the new column to be created. Defaults to "isneighbor".

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

origin

The name of a column in x containing the H3 cells used as origin of the local IJ coordinate system

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

new_column

Name of the new column(s) to create on the input data. For ddbh3_h3_to_local_ij(), a character vector of length 2 with the names of the I and J columns (defaults to c("i", "j")). For ddbh3_local_ij_to_h3(), the name of the cell column (defaults to "h3cell"). If the input already has a column with any of these names, it is replaced by the result

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

i, j

The names of the columns in x containing the local I and J coordinates

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):

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_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

lon

The name of a column in x containing the longitude

lat

The name of a column in x containing the latitude

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 NA (or empty).

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

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: string or bigint

Details

The two functions differ only in the output format of the H3 cell index:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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 NULL, uses (or creates) the default connection from ddbh3_default_conn().

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

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 NA (or empty).

conn

A connection object to a DuckDB database. Required when x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

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 "string" (default) or "bigint". Only used in ddbh3_points_to_h3().

Details

The two functions differ in the output format:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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:

duckspatial_df

A lazy spatial data frame via dbplyr.

sf

A spatial data frame.

tbl_lazy

A lazy data frame from dbplyr.

data.frame

A standard R data frame.

character string

A table or view name in conn.

character vector

A vector of values to operate on in vectorized mode (requires conn = NULL).

n

Integer. Vertex number to retrieve. Must be in the range 0–5 for hexagons and 0–4 for pentagons. Only used in ddbh3_h3_to_vertex().

h3

The name of a column in x containing the H3 strings or H3 unsigned 64-bit integers (UBIGINT)

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 x is a table name. If NULL (the default), the connection of a lazy input (tbl_lazy or duckspatial_df) is used when there is one; otherwise the default connection from ddbh3_default_conn() is used.

name

A character string of length one specifying the name of the table to create in conn. Use "schema.table" to create it in a schema other than main. If NULL (the default), no table is written and the result is returned as described in the Value section.

mode

Character. Controls the return type. Options:

  • "duckspatial" (default): Lazy spatial data frame backed by dbplyr/DuckDB

  • "sf": Eagerly collected sf object (uses memory)

Can be set globally via ddbs_options(mode = "...") or per-function via this argument. Per-function overrides global setting. It only applies when the output has a geometry column, and is ignored otherwise. In vectorized mode, ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() return a duckspatial_df ("duckspatial") or an sf ("sf") with the indexes and their geometry.

overwrite

Boolean. whether to overwrite the existing table if it exists. Defaults to FALSE. This argument is ignored when name is NULL.

quiet

A logical value. If TRUE, suppresses any informational messages. Defaults to FALSE.

h3vertex

Name of the column containing H3 vertex strings. Defaults to "h3vertex"

nested

Logical. If TRUE, the result is returned as a list column (one row per input row). If FALSE (default), the list is unnested so each element occupies its own row (rows with an empty result are dropped). Ignored in vectorized mode, which always returns a list with one element per input index.

Details

The functions cover the full vertex workflow:

Value

One of the following, depending on the inputs:

tbl_lazy

If the output has no geometry, i.e. x is not spatial and the function adds a column. mode is ignored in this case.

duckspatial_df

If the output has geometry (x is spatial, e.g. an sf or duckspatial_df object, or the function builds geometries, as ⁠*_to_spatial()⁠ and ddbh3_h3_to_points() do) and mode = "duckspatial" (default).

sf

If the output has geometry and mode = "sf".

TRUE (invisibly)

If name is provided, a table is created in the connection and TRUE is returned invisibly.

vector

If x is a vector of indexes (character, or bit64::integer64) and conn = NULL, the function operates in vectorized mode, returning a vector (or a list, for functions returning several cells per index) of the same length as x. Not every function has a vectorized mode. The ⁠*_to_spatial()⁠ functions and ddbh3_h3_to_points() follow mode instead: a duckspatial_df (mode = "duckspatial") or an sf (mode = "sf"), both with the indexes and their geometry in EPSG:4326, one row per element of x.

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()