Geospatial Indexing
"What's near me?" — map 2-D points to something an ordinary index can search.
| Index | Idea | Notes |
|---|---|---|
| Geohash | interleave latitude/longitude bits → base-32 string; shared prefix ≈ nearby | 5 chars ≈ 4.9 × 4.9 km, 6 ≈ 1.2 × 0.6 km, 7 ≈ 153 × 153 m; query the cell + its 8 neighbours (edge effects); works in any B-tree/KV store and Redis GEOSEARCH |
| Quadtree | split a cell into 4 once it holds more than K points | adapts to density (cities vs oceans); usually an in-memory structure |
| S2 / H3 | hierarchical cells on the sphere (S2: Hilbert curve on a cube; H3: hexagons) | region coverings; H3's equidistant neighbours suit ride-hailing and surge pricing |
| R-tree (PostGIS GiST) | nested bounding rectangles | polygons and "within shape" queries |
Rules of thumb: static places (Yelp) → precompute an in-memory index and replicate it; moving objects (drivers, friends) → keep only the latest position in memory (Redis GEO, sharded by region) and stream the history elsewhere.