Skip to content

DuckDB Extension

A5 Geospatial Index

Group geographic points into A5 pentagonal cells in DuckDB. Use cell IDs to count points by area, join datasets, or roll up to a coarser grid.

494,475
extension loads Β· last 90 days
On this page
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;

Group geographic points into equal-area cells

A5 divides the globe into a hierarchy of pentagonal cells. The extension converts longitude and latitude into a cell ID, so locations can be grouped and joined using an integer instead of a pair of coordinates.

Use it to count events by area, compare geographic densities, or summarize a detailed grid at a coarser resolution. Cells at the same resolution have equal area. You can turn a cell back into a polygon when you need to draw it on a map.

Example

Put two nearby points in grid cells

The sample includes two New York coordinates. A5 takes longitude first, then latitude; resolution controls cell size.

Tested with DuckDB 1.5.6. Browser examples use Haybarn-Wasm 2.0.0-rc12.

SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
  WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL a5 FROM query_farm;
LOAD a5 FROM query_farm;

WITH points(longitude, latitude) AS (
  VALUES (-74.0060, 40.7128), (-74.0061, 40.7129)
)
SELECT a5_lonlat_to_cell(longitude, latitude, 10) AS cell_id,
       count(*) AS point_count
FROM points
GROUP BY cell_id
ORDER BY cell_id;

These points fall in one cell, giving a point count of 2. The numeric cell ID differs between A5 builds: check the migration notes before combining stored IDs. Cell membership is a grid approximation; use an exact geometry predicate when a boundary matters.

Common tasks

Deep Dive

Technical Details

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Cell Properties
a5_cell_area() Object type: Scalar function Returns the area in square meters of an A5 cell at the specified resolution level (-1 gives the world cell, i.e.
a5_cell_edge_length_avg() Object type: Scalar function Returns the average edge length in meters of an A5 cell at the specified resolution level; individual edges vary from the average by roughly +/-10%
a5_cell_to_boundary() Object type: Scalar function Returns the boundary vertices of an A5 cell as a closed ring of [lon, lat] points
a5_get_resolution() Object type: Scalar function Returns the resolution level (0-30) of an A5 cell
a5_is_valid_cell() Object type: Scalar function Returns true if the value is a valid A5 cell ID (a canonically-encoded cell).
Cell Sets
a5_area() Object type: Scalar function Returns the total area in square meters of a list of A5 cells (compacted or not).
a5_compact() Object type: Scalar function Compacts a list of A5 cells by merging complete sets of sibling cells into parent cells.
a5_contains() Object type: Scalar function Returns true if the cell is in the list of A5 cells (compacted or not).
a5_count() Object type: Scalar function Returns the number of cells in a list of A5 cells at its resolution, i.e.
a5_covering_resolution() Object type: Scalar function Returns the resolution of a list of A5 cells: the resolution of its compaction marker, or of its finest cell when it has none.
a5_difference() Object type: Scalar function Returns the difference of two lists of A5 cells: the cells in a but not in b.
a5_intersect() Object type: Scalar function Returns the intersection of two lists of A5 cells: the cells in both.
a5_is_compaction_marker() Object type: Scalar function 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_overlaps() Object type: Scalar function Returns true if two lists of A5 cells (compacted or not) share any cell.
a5_uncompact() Object type: Scalar function 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() Object type: Scalar function Returns the union of two lists of A5 cells: the cells in either.
Coordinate Conversion
a5_cell_to_lonlat() Object type: Scalar function Returns the center point [longitude, latitude] of an A5 cell
a5_hex_to_u64() Object type: Scalar function Converts an A5 hex string representation to a UBIGINT cell ID
a5_lonlat_to_cell() Object type: Scalar function Converts a longitude/latitude coordinate to an A5 cell at the specified resolution
a5_u64_to_hex() Object type: Scalar function Converts a UBIGINT A5 cell ID to its hex string representation
Geometry Integration
a5_cell_to_geometry() Object type: Scalar function Returns an A5 cell as a POLYGON geometry of its boundary
a5_cell_to_point() Object type: Scalar function Returns the center of an A5 cell as a POINT geometry
a5_geometry_to_cells() Object type: Scalar function Returns the A5 cells covering an arbitrary geometry at the given resolution.
Hierarchy
a5_cell_to_children() Object type: Scalar function Returns the immediate child A5 cells (one resolution finer)
a5_cell_to_parent() Object type: Scalar function Returns the immediate parent A5 cell (one resolution coarser)
a5_cell_to_subcells() Object type: Scalar function Returns the A5 cells at the specified finer resolution whose centers lie in the cell: the spatial counterpart of a5_cell_to_children and the inverse of a5_cell_to_supercell.
a5_cell_to_supercell() Object type: Scalar function Returns the A5 cell at the specified coarser resolution that contains the center of the cell: the spatial counterpart of a5_cell_to_parent, whose result need not contain the cell (-1 gives the world cell)
a5_get_num_children() Object type: Scalar function Returns the number of child cells at child_resolution that fit within a cell at parent_resolution (-1 for the world cell)
Traversal
a5_grid_disk() Object type: Scalar function Returns all A5 cells within k edge-steps of the given cell (edge adjacency), as a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
a5_grid_disk_vertex() Object type: Scalar function Returns all A5 cells within k vertex-steps of the given cell (vertex adjacency), as a covering: compacted cells sorted in curve order, followed by a compaction marker recording the resolution - use a5_uncompact to expand it
a5_spherical_cap() Object type: Scalar function 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
Utilities
a5_get_num_cells() Object type: Scalar function Returns the total number of A5 cells at the specified resolution level (0-30)
a5_get_res0_cells() Object type: Scalar function Returns all 12 resolution 0 (root) A5 cells covering the entire globe
a5_max_resolution() Object type: Scalar function Returns the finest A5 resolution level (30).
a5_migrate() Object type: Scalar function 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_world_cell() Object type: Scalar function Returns the A5 world cell, the root cell that covers the entire globe and is the ancestor of all resolution-0 cells

API Reference

Function Reference

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In Rust
Source Available Yes
View on GitHub
Usage
494,475
loads Β· last 90 days

Platforms

DuckDB 2.0.0

  • Linux x86_64aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows x86_64x86_64 (MinGW)
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 5.32 MB
Linux aarch64 4.62 MB
macOS Apple Silicon 3.21 MB
Windows x86_64 10.52 MB
Windows x86_64 (MinGW) 19.36 MB

Compressed download sizes for the release shown above.

DuckDB & Haybarn

Release calendar
  • DuckDB v2.0.0 Query.Farm repository Supported
  • Haybarn v1.5.6 Built-in community repository Supported

Browser examples use Haybarn-Wasm 2.0.0-rc12.

Checked .