Geospatial Indexing
By Ankur NayakReviewed
A geospatial index turns a latitude and longitude into a cell ID in a grid, so 'what is near me?' becomes a fast lookup of a few cells instead of a scan of every point. Geohash, S2 and Uber's hexagonal H3 are common grids.
How it works
Finding the nearest drivers by computing the distance to every driver does not scale. Instead, the map is divided into cells, each with an ID, and every location is stored under its cell. A nearby search looks up the cell containing the rider plus its neighbours, then ranks the handful of candidates by real distance or ETA.
Geohash encodes rectangular cells as strings where a shared prefix means nearby. Google's S2 projects the sphere onto a cube and uses a space-filling curve. Uber's H3 uses hexagons at 16 resolutions; each finer resolution has cells about 1/7 the area, and every neighbour of a hexagon is the same distance away, which makes it convenient for measuring supply and demand per cell, as Uber does for surge pricing.
For moving objects such as drivers, the index lives in memory and is updated every few seconds, typically sharded by cell so each server owns a region of the map.
Key points
- lat/lng → cell ID turns proximity into key lookups
- Search the cell plus its neighbours, then rank by distance
- Hexagons have uniform neighbour distance
- Resolution trades precision against cells to search
When to use it, and pitfalls
Use it for
- Ride matching and delivery dispatch
- Nearby search (restaurants, friends)
- Aggregating metrics by area (surge, heatmaps)
Watch out for
- Points near cell edges: always include neighbour cells
- Dense cities vs. empty areas need different resolutions
- High-frequency location writes need an in-memory store
Used in
Related building blocks
Frequently asked questions
Why does Uber use hexagons?
In a hexagonal grid every neighbouring cell's centre is the same distance away, unlike squares where diagonal neighbours are farther. That makes neighbourhood searches and smoothing of supply and demand between cells simpler and more uniform.
Sources
- H3: Uber's Hexagonal Hierarchical Spatial Index · Uber Engineering
- H3 documentation · H3 project
- Geohash · Wikipedia