Skip to main content

DuckDB Quickstart

Get started with A5 in Duckdb by installing the extension and running a simple example.

Installation

Install and load the A5 extension with this SQL:

INSTALL a5 from community;
LOAD a5;

-- Optional: Install spatial extension for geometry operations
INSTALL spatial;
LOAD spatial;

Example: Translate Lat/Lon to A5 Cell and back

-- Get the A5 Cell for London
SELECT a5_lonlat_to_cell(-0.1276, 51.50735, 10) as cell;
┌─────────────────────┐
│ cell │
│ uint64 │
├─────────────────────┤
7161034019553935360
└─────────────────────┘

-- Get the center of the A5 cell previously returned
-- since cells cover a greater area, the returned lon/lat
-- will be different.
SELECT a5_cell_to_lonlat(7161034019553935360) AS lonlat;
┌───────────────────────────────────────────┐
│ lonlat │
double[2]
├───────────────────────────────────────────┤
[-0.15971839880376137, 51.511842921513406]
└───────────────────────────────────────────┘

Example: Generate GeoJSON for Cell

To generate a GeoJSON polygon for the A5 cell above use this SQL along with DuckDB's spatial extension:

SELECT
ST_AsGeoJSON(
ST_MakePolygon(
ST_MakeLine(
list_transform(
a5_cell_to_boundary(7161034019553935360),
x -> ST_Point(x[1], x[2])
)
)
)
) as g

This produces:

{
"type":"Polygon",
"coordinates":[
[
[-0.19250141916919006, 51.51946462334752],
[-0.20078615946991363, 51.47558191837641],
[-0.13985206278343298, 51.48472345047163],
[-0.11104984728311251, 51.52438151810816],
[-0.15436577597120050, 51.55503847320052],
[-0.19250141916919006, 51.51946462334752]
]
]
}

Visualizing that A5 cell shows:

Example: Generate A5 Cells

Here's a complete example that generates A5 cells at a specified resolution and creates a Polygon using DuckDB's spatial extension.


SELECT unnest(a5_cell_to_children(7161034019553935360, 13)) AS cell_id;
┌─────────────────────┐
│ cell_id │
│ uint64 │
├─────────────────────┤
7161033478388056064
7161033495567925248
7161033512747794432
7161033529927663616
7161033547107532800
7161033564287401984
...
├─────────────────────┤
64 rows
└─────────────────────┘

Example: Compare Cell Areas

This example show how to obtain the cell area, cross-checking against the value from ST_Area_Spheroid().

Note that all cells at the same resolution have the exact same area. In general a5_cell_area() should always be used rather than manually computing the area

-- Compare the exact area (from a5_cell_area) with the estimated area
-- calculated from the cell boundary using ST_Area_Spheroid
WITH cells AS (
SELECT
unnest(generate_series(0, 10))::INTEGER AS resolution
),
areas AS (
SELECT
resolution,
a5_lonlat_to_cell(-0.1276, 51.50735, resolution) AS cell,
a5_cell_area(resolution) AS exact_area, -- Area constant within resolution level
ST_Area_Spheroid(
ST_MakePolygon(
ST_MakeLine(
list_transform(
a5_cell_to_boundary(a5_lonlat_to_cell(-0.1276, 51.50735, resolution)),
x-> ST_Point(x[2], x[1]) -- Swap to [lat, lon] for ST_Area_Spheroid
)
)
)
) as estimated_area -- Quantized boundary will yield only estimate of area
FROM cells
)
SELECT
resolution,
cell,
exact_area,
estimated_area,
printf('%.4g%%', 100 * (estimated_area - exact_area) / exact_area) as area_error
FROM areas;
┌────────────┬─────────────────────┬────────────────────┬────────────────────┬─────────────┐
│ resolution │ cell │ exact_area │ estimated_area │ area_error │
│ int32 │ uint64 │ doubledoublevarchar
├────────────┼─────────────────────┼────────────────────┼────────────────────┼─────────────┤
0129703669268270284842505468731619.9342505469418157.651.615e-06%
169895866216790097928501093746323.9858501093267808.199-5.629e-06%
271697306067738296322125273436580.99632125259982148.427-0.0006331%
37160723407519088640531318359145.2491531312733699.80804-0.001059%
47162975207332773888132829589786.31229132841421743.743960.008908%
5715565685793829683233207397446.57806833204356296.555916-0.009158%
671560790704033628168301849361.6445178300133697.437225-0.02067%
771610400668678881282075462340.41112922075718617.14949040.01235%
87161031270774865920518865585.1027823518895962.349887850.005855%
97161033469798121472129716396.27569558129712619.13629723-0.002912%
10716103401955393536032429099.06892389432428626.05546379-0.001459%
├────────────┴─────────────────────┴────────────────────┴────────────────────┴─────────────┤
11 rows 5 columns
└──────────────────────────────────────────────────────────────────────────────────────────┘

Next Steps