Cell Sets functions in the A5 Geospatial Index DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Test fixtures for a5 cookbook / quickstart / technical-details snippets.
-- Provides small deterministic lat/lng tables so the snippets that
-- aggregate or join real geographic data can parse and execute.
--
-- Idempotent: every CREATE uses OR REPLACE so re-running the harness on
-- the same connection is safe.
-- ---------------------------------------------------------------------
-- Base point set: 20 well-known city coordinates. Used as a building
-- block for every other table so all fixtures are mutually consistent.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TEMP TABLE _a5_cities AS
SELECT * FROM (VALUES
(-74.0060, 40.7128, 'New York'),
(-73.9352, 40.7306, 'Brooklyn'),
(-73.9857, 40.7484, 'Midtown'),
(-74.0445, 40.6892, 'Liberty'),
(-73.7781, 40.6413, 'JFK'),
(-118.2437, 34.0522, 'Los Angeles'),
(-118.4085, 33.9416, 'LAX'),
(-122.4194, 37.7749, 'San Francisco'),
(-122.2711, 37.8044, 'Oakland'),
(-122.0838, 37.3861, 'Mountain View'),
( -87.6298, 41.8781, 'Chicago'),
( -71.0589, 42.3601, 'Boston'),
( -77.0369, 38.9072, 'Washington DC'),
( -80.1918, 25.7617, 'Miami'),
( -95.3698, 29.7604, 'Houston'),
( -3.7037, 40.4168, 'Madrid'),
( 2.3522, 48.8566, 'Paris'),
( -0.1276, 51.5074, 'London'),
( 139.6503, 35.6762, 'Tokyo'),
( 151.2093,-33.8688, 'Sydney')
) AS t(lon, lat, name);
-- ---------------------------------------------------------------------
-- `points` — referenced by metadata.json:quickStart.
-- Columns: longitude, latitude.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE points AS
SELECT lon AS longitude, lat AS latitude
FROM _a5_cities;
-- ---------------------------------------------------------------------
-- `pickups` — referenced by technical-details.mdx "What you can do".
-- Columns: longitude, latitude. 50 deterministic rows generated by
-- duplicating the city set with small lat/lng perturbations so GROUP BY
-- yields multiple rows per cell at typical resolutions.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE pickups AS
SELECT
lon + (i * 0.0003) AS longitude,
lat + (i * 0.0002) AS latitude
FROM _a5_cities,
generate_series(0, 4) AS g(i);
-- ---------------------------------------------------------------------
-- `transactions` — referenced by cookbook.mdx aggregate + spatial-join.
-- Columns: longitude, latitude, amount, cell_15.
-- cell_15 is pre-computed at resolution 15 so the join example can
-- match the `events` CTE directly.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE transactions AS
SELECT
lon + (i * 0.0001) AS longitude,
lat + (i * 0.0001) AS latitude,
(10.0 + i * 1.5) AS amount,
a5_lonlat_to_cell(lon + (i * 0.0001),
lat + (i * 0.0001), 15) AS cell_15
FROM _a5_cities,
generate_series(0, 2) AS g(i);
-- ---------------------------------------------------------------------
-- `raw_points` — referenced by cookbook.mdx + technical-details.mdx
-- "roll up to a coarser resolution" examples.
-- Columns: lon, lat.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE raw_points AS
SELECT lon, lat FROM _a5_cities;
-- ---------------------------------------------------------------------
-- `indexed` — referenced by cookbook.mdx + technical-details.mdx
-- "roll up" snippets. Examples that rebuild this table use CREATE OR REPLACE.
-- Columns: lon, lat, cell_15.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE indexed AS
SELECT lon, lat, a5_lonlat_to_cell(lon, lat, 15) AS cell_15
FROM _a5_cities;
-- ---------------------------------------------------------------------
-- `event_points` — referenced by cookbook.mdx spatial-join example.
-- Columns: lon, lat.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE event_points AS
SELECT lon, lat FROM _a5_cities;
-- ---------------------------------------------------------------------
-- `zone_centroids` — referenced by cookbook.mdx spatial-join example.
-- Columns: lon, lat, zone_name.
-- ---------------------------------------------------------------------
CREATE OR REPLACE TABLE zone_centroids AS
SELECT lon, lat, name AS zone_name FROM _a5_cities;
Function category
Cell Sets
11 functionsWork with regions expressed as lists of cells. Compact a list into a covering (with its trailing compaction marker) and expand it again, count cells and measure area, and combine regions with union, intersection, difference, overlap, and membership tests — all without unnesting.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the total area in square meters of a list of A5 cells (compacted or not). Overlapping cells each add their area - use a5_union to merge them first
SELECT a5_area(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_area(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| 32429099.068923894 |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Compacts a list of A5 cells by merging complete sets of sibling cells into parent cells. The result is a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
SELECT a5_compact(a5_cell_to_children(a5_lonlat_to_cell(-122.4, 37.8, 5)));
Output
| a5_compact(a5_cell_to_children(a5_lonlat_to_cell(-122.4, 37.8, 5))) |
|---|
| [1937110789722734592, 17295511418962968640] |
Related functions
- a5_uncompact() — Expands a list of A5 cells to all their cells at the resolution of the list: the resolution of its compaction marker, or of its finest cell when it has none
- a5_is_compaction_marker() — Returns true if the value is a compaction marker: the last element of a covering (as returned by a5_compact, a5_grid_disk, a5_spherical_cap, a5_cell_to_subcells, polygon coverings and the set operations), recording the resolution its cells stand for
- a5_union() — Returns the union of two lists of A5 cells: the cells in either
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Argument
cell
|
Type
UBIGINT
|
Mode Positional | Description |
Description
Returns true if the cell is in the list of A5 cells (compacted or not). The cell must be at the resolution of the list, and the list must be sorted in curve order, as coverings are
SELECT a5_contains(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0), a5_lonlat_to_cell(-122.4, 37.8, 10));
Output
| a5_contains(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0), a5_lonlat_to_cell(-122.4, 37.8, 10)) |
|---|
| true |
Related functions
- a5_overlaps() — Returns true if two lists of A5 cells (compacted or not) share any cell
- a5_spherical_cap() — Returns all A5 cells within the specified radius (in meters) of the given cell, as a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
- a5_geometry_to_cells() — Returns the A5 cells covering an arbitrary geometry at the given resolution
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the number of cells in a list of A5 cells at its resolution, i.e. the length of the uncompacted list. Overlapping cells are counted more than once - use a5_union to merge them first
SELECT a5_count(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_count(a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| 1 |
Related functions
- a5_area() — Returns the total area in square meters of a list of A5 cells (compacted or not)
- a5_uncompact() — Expands a list of A5 cells to all their cells at the resolution of the list: the resolution of its compaction marker, or of its finest cell when it has none
- a5_union() — Returns the union of two lists of A5 cells: the cells in either
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the resolution of a list of A5 cells: the resolution of its compaction marker, or of its finest cell when it has none. Returns -1 for an empty list
SELECT a5_covering_resolution(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2));
Output
| a5_covering_resolution(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2)) |
|---|
| 10 |
Related functions
- a5_is_compaction_marker() — Returns true if the value is a compaction marker: the last element of a covering (as returned by a5_compact, a5_grid_disk, a5_spherical_cap, a5_cell_to_subcells, polygon coverings and the set operations), recording the resolution its cells stand for
- a5_get_resolution() — Returns the resolution level (0-30) of an A5 cell
- a5_count() — Returns the number of cells in a list of A5 cells at its resolution, i
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
a
|
Type
UBIGINT[]
|
Mode Positional | Description |
Argument
b
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the difference of two lists of A5 cells: the cells in a but not in b. Both lists must be at the same resolution. The result is a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
SELECT a5_difference(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_difference(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| [1936164659967033344, 1936167408746102784, 1936170157525172224, 1936171257036800000, 1937270768664576000, 1937276266222714880, 1937277365734342656, 1937279564757598208, 1937280664269225984, 1937281763780853760, 1937282863292481536, 1937285062315737088, 1937286161827364864, 17296637318869811264] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
a
|
Type
UBIGINT[]
|
Mode Positional | Description |
Argument
b
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the intersection of two lists of A5 cells: the cells in both. Both lists must be at the same resolution. The result is a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
SELECT a5_intersect(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_intersect(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| [1937278465245970432, 17296637318869811264] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
value
|
Type
UBIGINT
|
Mode Positional | Description |
Description
Returns true if the value is a compaction marker: the last element of a covering (as returned by a5_compact, a5_grid_disk, a5_spherical_cap, a5_cell_to_subcells, polygon coverings and the set operations), recording the resolution its cells stand for. A compaction marker is not a cell
SELECT list_filter(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 1), lambda x: NOT a5_is_compaction_marker(x));
Related functions
- a5_compact() — Compacts a list of A5 cells by merging complete sets of sibling cells into parent cells
- a5_uncompact() — Expands a list of A5 cells to all their cells at the resolution of the list: the resolution of its compaction marker, or of its finest cell when it has none
- a5_covering_resolution() — Returns the resolution of a list of A5 cells: the resolution of its compaction marker, or of its finest cell when it has none
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
a
|
Type
UBIGINT[]
|
Mode Positional | Description |
Argument
b
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns true if two lists of A5 cells (compacted or not) share any cell. Both lists must be at the same resolution
SELECT a5_overlaps(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_overlaps(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| true |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cells
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Expands a list of A5 cells to all their cells at the resolution of the list: the resolution of its compaction marker, or of its finest cell when it has none. The compaction marker is dropped from the result
SELECT a5_uncompact(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2));
Output
| a5_uncompact(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2)) |
|---|
| [1936164659967033344, 1936165759478661120, 1936166858990288896, 1936167958501916672, 1936169058013544448, 1936170157525172224, 1936171257036800000, 1937270768664576000, 1937276266222714880, 1937277365734342656, 1937278465245970432, 1937279564757598208, 1937280664269225984, 1937281763780853760, 1937282863292481536, 1937285062315737088, 1937286161827364864] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
a
|
Type
UBIGINT[]
|
Mode Positional | Description |
Argument
b
|
Type
UBIGINT[]
|
Mode Positional | Description |
Description
Returns the union of two lists of A5 cells: the cells in either. Both lists must be at the same resolution. The result is a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
SELECT a5_union(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0));
Output
| a5_union(a5_grid_disk(a5_lonlat_to_cell(-122.4, 37.8, 10), 2), a5_spherical_cap(a5_lonlat_to_cell(-122.4, 37.8, 10), 5000.0)) |
|---|
| [1936164659967033344, 1936167408746102784, 1936170157525172224, 1936171257036800000, 1937270768664576000, 1937276266222714880, 1937277365734342656, 1937280114513412096, 1937282863292481536, 1937285062315737088, 1937286161827364864, 17296637318869811264] |