Ankit Patel
WritingPapersLabsAboutContact
Distributed Systems & Data··Updated 2024-10·3 min read·802 views

Geohash, S2 or H3?

A square cell has eight neighbors at two center-to-center distances, while a hexagon has six at one

'In the same parent cell' can describe a relationship between IDs or a relationship between shapes. With H3, those are two different promises.

Every fine-resolution H3 hexagon maps to exactly one parent ID in the index hierarchy, making integer rollups trivial. Geometrically, however, child hexagons extend across the edges of their coarser parent hexagon. Indexing GPS points directly at resolution 7 therefore assigns border points to different coarse cells than indexing at resolution 9 and rolling child IDs up to resolution 7.

H3's indexing documentation calls this out directly: hierarchical containment is exact, while geometric containment across resolutions is approximate.

When cells drive surge zones, tax boundaries or enterprise visit reports, that discrepancy changes real numbers. Microsecond differences in coordinate-to-cell encoding speed matter far less than picking the right geometric invariant.

Geohash, S2 and H3 at the boundary

Index Core strength Boundary behavior to watch
Geohash Base32 string prefixes work natively in key-value and B-tree indexes Adjacent points across a meridian or cell seam can share zero prefix characters
S2 Spherical quadtree on a projected cube with exact child-cell containment Variable-size cell coverings still approximate arbitrary legal or business polygons
H3 Hexagonal cells with uniform six-neighbor adjacency for movement smoothing Parent IDs and parent polygon outlines follow different containment rules

Geohash shines when you only have standard string prefix scans in an existing store. Truncating characters zooms out cleanly, yet a single prefix scan never suffices for radius search because two cafes twenty meters apart across a major grid seam can have completely different leading characters. Latitude distortion also stretches cell width-to-height ratios far from the equator.

Google's S2 library projects the six faces of a cube onto the unit sphere and subdivides each quadrilateral into four exact children along a Hilbert space-filling curve. Because every child cell sits strictly inside its parent's spherical bounds, multi-resolution region coverings preserve exact containment predicates for polygon pruning in spatial joins before running exact boundary geometry.

Uber's H3 grid excels at spatial smoothing, flow modeling and k-ring neighborhood features because every hexagon shares edges at uniform center-to-center distances with six neighbors, avoiding the corner-versus-edge asymmetry of square grids. Even H3 carries twelve pentagons per resolution worldwide and modest area variation across icosahedron faces, so area-normalized density calculations still require calling exact cell-area functions rather than assuming identical acreage everywhere.

import h3

def nearby_venues(lat: float, lng: float, index_by_h3: dict[str, list[str]]) -> list[str]:
    cell = h3.latlng_to_cell(lat, lng, 9)
    ring = h3.grid_disk(cell, k=1)  # 7 cells: origin + 6 equidistant neighbors
    venues: list[str] = []
    for neighbor in ring:
        venues.extend(index_by_h3.get(neighbor, ()))
    return venues

Matching the grid to the operation

No single discrete global grid wins every workload inside a geospatial stack. Marketplace heatmaps and ML neighborhood features benefit from H3's six-way k-rings paired with one canonical base resolution for rollups. Exact polygon containment and multi-resolution region coverings fit S2 naturally. Simple proximity lookups inside Postgres often need nothing beyond native PostGIS GIST indexes.

When I review a spatial grid proposal, I ask to see test points along cell seams and real service-area polygons, then compare radius lookup, direct coarse binning and fine-to-coarse ID rollups on the exact map customers see. Everybody reading 'same area' in a metric spec should know whether that means a shared parent ID, a rendered hexagon or the legal polygon promised in the contract.

Continue in Distributed Systems & Data