Utilities 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
Utilities
5 functionsGeneral-purpose helpers — total cell count at a resolution, the 12 base cells covering the globe at resolution 0 (and the single root world cell above them), the finest resolution, and migration of cell IDs stored by pre-1.0 versions of A5.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
resolution
|
Type
INTEGER
|
Mode Positional | Description |
Description
Returns the total number of A5 cells at the specified resolution level (0-30)
SELECT a5_get_num_cells(5);
Output
| a5_get_num_cells(5) |
|---|
| 15360 |
Related functions
- a5_cell_area() — Returns the area in square meters of an A5 cell at the specified resolution level (-1 gives the world cell, i
- a5_get_num_children() — Returns the number of child cells at child_resolution that fit within a cell at parent_resolution (-1 for the world cell)
- a5_get_res0_cells() — Returns all 12 resolution 0 (root) A5 cells covering the entire globe
Signature
Description
Returns all 12 resolution 0 (root) A5 cells covering the entire globe
SELECT a5_get_res0_cells();
Output
| a5_get_res0_cells() |
|---|
| [144115188075855872, 432345564227567616, 720575940379279360, 1008806316530991104, 1297036692682702848, 1585267068834414592, 1873497444986126336, 2161727821137838080, 2449958197289549824, 2738188573441261568, 3026418949592973312, 3314649325744685056] |
Related functions
- a5_get_num_cells() — Returns the total number of A5 cells at the specified resolution level (0-30)
- a5_world_cell() — Returns the A5 world cell, the root cell that covers the entire globe and is the ancestor of all resolution-0 cells
- a5_cell_to_children() — Returns the immediate child A5 cells (one resolution finer)
Signature
Description
Returns the finest A5 resolution level (30). Resolutions run from 0 to this value
SELECT a5_max_resolution();
Output
| a5_max_resolution() |
|---|
| 30 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
cell
|
Type
UBIGINT
|
Mode Positional | Description |
Description
Converts a cell ID created by a pre-1.0 version of A5 (this extension built on the a5 crate 0.10 or earlier) to the ID of the same cell in the A5 1.0 index. A5 1.0 orders cells along a new curve, so stored IDs at resolution 2 and finer still look valid but silently name different cells until migrated. Apply it exactly once, and only to pre-1.0 IDs — nothing in an ID says which index it belongs to. Resolution-1 IDs pass through unchanged, but the resolution-1 cells themselves were redefined, so re-index resolution-1 data from its source coordinates instead.
SELECT a5_migrate(2742821848331845632);
Output
| a5_migrate(2742821848331845632) |
|---|
| 2760559170984869888 |
Signature
Description
Returns the A5 world cell, the root cell that covers the entire globe and is the ancestor of all resolution-0 cells
SELECT a5_world_cell();
Output
| a5_world_cell() |
|---|
| 0 |