Hexagon Discretization
In this page, we will see how we can use AISdb to discretize AIS tracks to hexagons.
Introduction
H3 is Uber's hierarchical hexagonal geospatial indexing system. It partitions the Earth into a multi-resolution hexagonal grid, and its key advantage over square grids is the "one-distance rule," where all neighbors of a hexagon lie at comparable step distances.

As illustrated in the figure above, this uniformity removes the diagonal-versus-edge ambiguity present in square lattices. For maritime work, hexagons are great because they reduce directional bias and make neighborhood queries and aggregation intuitive.
H3 indexes are 64-bit IDs typically shown as hex strings, such as
860e4d31fffffff.
Discretize AIS lat/lon points to hexagons using AISdb
AISdb's aisdb.discretize.h3.Discretizer class wraps the h3-py bindings so you can go from a stream of AIS tracks straight to H3 cell IDs, without hand-rolling the lat/lon-to-cell conversion yourself. The code below connects to a PostgreSQL database, queries a bounding box and time window in the Gulf of St. Lawrence, and tags each point in the resulting tracks with its H3 index.
The Discretizer also exposes a couple of methods worth knowing about beyond the streaming path above. get_h3_index(lat, lon) converts a single coordinate pair to its H3 cell ID, useful when you already have a point and don't need the full track pipeline. get_polygon_from_cells(cells, tight=True) takes a list of H3 cell IDs and returns their combined boundary as a Shapely geometry, handy for drawing the hexagon footprint of a set of cells on a map. And describe() prints and plots how hexagon area changes with latitude at the chosen resolution, plus a table of edge lengths across all 16 H3 resolutions, which is a quick way to sanity-check that the resolution you picked matches the scale of the analysis.
describe() relies on matplotlib and geopandas, both of which install automatically as core AISdb dependencies, so there's nothing extra to add.
Refer to the example notebook for the full walkthrough, including the describe() output. https://github.com/MAPS-Lab/AISdb/blob/master/examples/discretize.ipynb
References
Last updated