Skip to content
Veltoryx Technologies
ProLesson 9.3Advanced8 min readUpdated 31 October 2026

Geospatial Indexing: Quadtrees and Geohashes

Understand how ridesharing, mapping, and food delivery platforms index 2D locations to query nearby drivers and restaurants in milliseconds.

Traditional database indexes (such as standard B-Trees) operate on one-dimensional data. You can easily query numbers greater than 10 or names starting with the letter "S" because 1D data has a natural linear sort order.

However, geographical locations exist in two dimensions: Latitude (North-South) and Longitude (East-West).

When building location-based systems like Uber, Lyft, Yelp, or DoorDash, the core query is: "Find the nearest 10 available drivers within a 3-kilometer radius of this customer's GPS coordinates."

If you try to query this using a standard relational database with independent B-Trees on latitude and longitude:

sql
SELECT * FROM drivers
WHERE latitude BETWEEN 37.77 AND 37.80
  AND longitude BETWEEN -122.45 AND -122.40;

The database index can only optimize one dimension at a time. The query engine scans thousands of rows along one coordinate and then manually filters them along the other, causing high CPU load, full table scans, and multi-second latencies at scale.

To solve this problem, distributed systems use Geospatial Indexing: transforming 2D spatial coordinates into structures that can be searched in sub-millisecond time.

01Core concepts and intuition

There are two dominant spatial indexing techniques used across industry: Geohashes (grid-based string interleaving) and Quadtrees (hierarchical tree partitioning).

Geohashing: Interleaving 2D space into 1D strings

Invented by Gustavo Niemeyer in 2008, Geohashing is a hierarchical spatial indexing system that converts a 2D (latitude, longitude) coordinate into a compact base-32 string (e.g., 9q8yy).

How Geohashing works:

  1. Divide the entire globe into 32 equal bounding boxes, each assigned an alphanumeric character (0-9, b-z).
  2. Recursively divide each box into 32 smaller sub-boxes.
  3. Bits representing latitude and longitude are interleaved: alternating bits narrow down the East-West and North-South boundaries.
  4. The longer the geohash string, the smaller and more precise the bounding box:
    • 1 character: ~5,000 km x 5,000 km (continental scale)
    • 4 characters: ~39 km x 19 km (city scale)
    • 6 characters: ~1.2 km x 0.6 km (neighborhood block)
    • 8 characters: ~38 meters x 19 meters (street corner)

Because Geohashes are simple alphanumeric strings, they can be indexed using standard B-Trees in PostgreSQL, stored in Redis Sorted Sets, or used as partition keys in Cassandra.

The Border Edge Problem: Because space is flattened onto a grid, two coordinates that are physically 10 meters apart might straddle the boundary between two different large bounding boxes (e.g., one is 9q8yy and the other is 9q8yz). To reliably find all nearby points, spatial queries must search the target geohash box plus its 8 adjacent neighboring boxes.

Quadtrees: Recursive 2D spatial trees

A Quadtree is a tree data structure in which each internal node has exactly four children. In geospatial indexing, these four children represent the four quadrants of a 2D bounding box:

  • Northwest (NW)
  • Northeast (NE)
  • Southwest (SW)
  • Southeast (SE)

A Quadtree begins with a root node covering an entire city or region. As points of interest (restaurants, hotels, stores) are inserted:

  • A node holds a maximum capacity (e.g., 100 points).
  • Once a quadrant exceeds 100 points, it dynamically splits into four smaller sub-quadrants.
  • In dense urban centers (like downtown Manhattan), the tree subdivides many levels deep into tiny blocks. In rural countryside, a single node covers hundreds of square kilometers without splitting.

To search for items within a radius, the algorithm traverses the tree, pruning any quadrant branches that do not intersect the search bounding circle.

Loading the rest of the lesson…