Course map

Topic 210 / Advanced

Geospatial indexing: geohash, quadtree, H3

Chapter 13.2 · Geospatial Indexing

What and why

A B-tree index (→ 64) orders data along one dimension — it answers "give me everything between X and Y" efficiently, but has no good answer to "give me everything within 2 km of this point," because two points close in physical space can be arbitrarily far apart in a one-dimensional sort order. Geospatial indexing maps two-dimensional coordinates onto structures that preserve locality, so nearby points land in nearby keys or the same cell, turning a radius query into a small set of index lookups. Builds on: → 64 (indexing — where one-dimensional ordering stops working).

Diagram

Geohash (prefix-matchable string)      Quadtree (adaptive)          H3 (hexagonal cells)
────────────────────────────────       ──────────────────           ────────────────────
  "9q8yy" ⊂ "9q8y" ⊂ "9q8"               ┌───┬───┐                     ⬡ ⬡ ⬡
  shared prefix ≈ nearby                 │ NW│ NE│ split only           ⬡ ⬡ ⬡
  (edge cases jump)                      ├───┼───┤ where dense           ⬡ ⬡ ⬡
                                         │ SW│ SE│                   no corner-vs-edge
                                         └───┴───┘                   distance artifact

How it works

  1. Geohash encodes latitude/longitude into a string by recursively bisecting the bounding box and interleaving bits; each added character narrows the cell further, and a shared prefix usually means nearby cells, so an index on the geohash string lets a prefix match approximate a bounding-box query.
  2. Geohash's weakness: cells near the edge of a bisection can be geographically adjacent to a cell with a completely different prefix, so a naive prefix query misses some genuine neighbors — a radius query must also check neighboring prefixes.
  3. Quadtrees subdivide space adaptively: a region starts as one cell and splits into four only once it holds more points than a threshold, so dense areas get fine cells and sparse areas stay coarse — better than geohash's fixed grid at uneven density.
  4. H3 tiles the sphere with hexagonal cells at multiple resolutions (coarse cells subdivide into finer hexagons); hexagons are chosen because every neighboring cell is equally close, avoiding the corner-versus-edge distance distortion a square grid has.
  5. For a "near me" query under any of these structures: compute the query point's cell(s) at an appropriate resolution, fetch that cell and its ring of neighbors, then filter the candidates by exact distance — the index narrows a large candidate set to a handful before any precise distance math runs.
  6. Cell-size trade-off: coarse cells mean fewer cells to check but more candidates per cell to filter (more false positives); fine cells mean fewer false positives but more cells to union for a given radius, and more index entries to store.
  7. Hot cells: a cell covering a dense venue (a stadium, an airport) can receive far more reads and writes than an average cell — the same hot-partition problem partitioning faces (→ 73), solved the same way: split further, or scale that cell's data separately.

Practical example

(illustrative). A search for nearby drivers within 2 km, instead of computing distance from the rider to every active driver citywide, resolves the rider's location to one cell plus its immediate ring of neighbors — a handful of cells — and only drivers indexed in those cells are distance-checked. If each cell holds a few dozen drivers on average, the candidate set drops from thousands citywide to a few hundred before any precise distance math runs at all.

When to use it

  • Use when: proximity queries ("near me", "within radius") are a core access pattern and a one-dimensional index can't express "close in space."
  • Avoid when: location is only ever filtered by exact match (a stored city or postal code) or by boundary lookups a database's native geometry type already handles.
  • Failure scenarios: hot cells at dense venues overloading one partition while the rest of the index sits idle; a cell resolution chosen too coarse (too many false positives to filter) or too fine (index bloat, more cells to union per query); geohash's edge-adjacency gap silently missing a genuine neighbor.
  • Simpler alternative: a database's built-in geometry/geography type with its own spatial index, for moderate scale where a cell scheme's operational complexity isn't justified.

Practice

(Mechanism kind — trace by hand; 20–30 min, no lab services). Plot 12 points on a hand-drawn grid representing a city block. Build a geohash-style prefix encoding by hand for 4 levels of bisection, and separately build a quadtree by splitting only cells holding more than 2 points. Query "points near the center" on both, and compare which structure needed fewer cells examined and which missed a neighbor sitting just across a cell boundary. Done when you can point to the exact case where prefix matching misses a true neighbor.

Key takeaway

One-dimensional indexes can't express proximity; geohash, quadtrees, and H3 each trade a specific precision/density/boundary-artifact profile for turning "near me" into a handful of cell lookups. Done when you can: pick a cell scheme for a given density pattern and explain its specific failure mode. Used next in Projects 14 and 15.

References

Uber, H3 — see references.md.

Source directory

Search the guide