AlgoViz

Find every charger across a cell boundary

medium 6 min

A driver near the edge of a grid cell requests chargers within 800 metres.

Video lesson · Chapter 1 of 5

The nearest shop is across a boundary

0:00 / 3:25

The closest charger is across the grid line

1:09Reel

Theory

Read or listen first. About 1 min.

DriverA · 200 mB · 40 mC · 950 m800 m

An 800 m search

Two matches. A and B are within 800 m. C is in a covering cell, but its distance is 950 m.

What it is

Coordinates live on a surface. A sorted key lives on a line. A spatial index bridges the two by finding a manageable set of locations that could intersect a query. The final distance calculation tells you which candidates actually belong in the answer. Keeping those responsibilities separate makes boundary bugs easier to diagnose.

Why it exists

A geohash prefix describes a rectangle. A quadtree divides selected regions into four smaller regions. H3 gives locations hierarchical cell IDs and S2 orders cells along a space-filling curve. All can reduce search work, but no single cell ID means “everything within 800 metres of me.”

Geohash

A longer prefix selects a smaller rectangle.

Quadtree

Split a crowded region into four equal quadrants.

H3

Bin points into cells. Most have six neighbors.

S2

A Hilbert curve orders cells on each projected cube face.

Words to know

Covering
A set of regions whose union contains the query area.
Precision
The fraction of returned candidates that are true matches.

How it works

Step through the idea, or press Play.

Step 1 of 3

Cover the search area. Geohash encodes hierarchical rectangles. Scanning one prefix misses border neighbors. A quadtree splits busy regions recursively. H3 offers hierarchical cells for bucketing movement and aggregating demand.

Understand it

Start here

Across the street, across the cell

Stand on the blue side of the street. Charger B is only a short walk away on the yellow side. Follow the expanding search area, then watch which pins survive the distance check.

Across the street, across the cell
Chargers on both sides of the streetABCOne cell misses charger B

Fetch intersecting cells and check candidates.

Structure

Radius lookup

Radius lookup

Driver → Cell covering (radius); Cell covering → Cell index (cell IDs); Cell index → Distance check (candidates); Distance check → Results (within radius)
The index protects recall; exact filtering removes false positives.

A boundary test fixture

Place two chargers five metres apart on opposite sides of a cell edge, plus a third just beyond the requested distance but inside a covering cell. The first two must be returned and the third rejected. Repeat at high latitude and near the antimeridian.

Two spatial filters

An illustrative radius query

CandidateWhy it was fetchedFinal decision
A, 200 metres awaySame covering cellInclude
B, 40 metres awayAdjacent covering cellInclude
C, 950 metres awayCorner of a covering cellExclude
D, in a disjoint cellOutside a sound coveringSkip distance check

Pick a resolution from the workload

A coarse cell might contain an entire dense neighborhood. Reads touch few keys yet measure many distances. Smaller cells reduce each bucket but expand the covering and cause moving vehicles to cross cell boundaries more often. Benchmark both downtown and rural routes. Record keys scanned, candidates fetched, accepted results and index updates per minute.

Crossing the date line

A viewport that goes from 179.9 degrees east to 179.9 degrees west is narrow on the globe. A naive minimum-to-maximum longitude range covers nearly the whole planet. Use geometry operations with the intended coordinate system, units and wraparound behavior. Test a driver just either side of the boundary and compare results with an exhaustive distance calculation on a small fixture.

Cover then refine

  • Fetch intersecting cells and check candidates.

Scan one prefix

  • A road can cross a cell edge within a few metres.

Summary

The whole topic in one minute.

Choose an index that cheaply overselects candidates. A complete covering protects recall; exact distance protects precision.

Sources