Geospatial radius search 00:00:01 The nearest shop is across a boundary The car needs a charger. My phone is a hundred twenty metres west of an imaginary grid line. The nearest charger is seven hundred eighty metres from me, on the other side. The charger belongs to a different index cell. If I ask only for the contents of my home cell, the charger never becomes a candidate. A query confined to my cell returns zero results, even though a valid charger lies just beyond the edge, proving we must cover adjacent cells. Look at what the empty list actually tells the driver. No charger was indexed inside one particular rectangle. The question on the phone asks about a circle around a person who can cross that rectangle in a few steps. 00:00:39 Cover a two-kilometre circle I ask for every charger within two kilometres. Cover the whole search circle with cells, including both sides of the nearby grid line and any other intersecting cells. Fetch the charger identifiers in that covering set. Some of those pins can be outside the circle because a rectangle can overlap the circle at just one corner. Use a consistent spherical distance for this example. Coordinates close to opposite ends of the written longitude scale can still be near each other on Earth. Pick one distance contract and carry it through indexing, filtering and the test oracle. A road trip would require a separate route calculation. This request is for straight-line distance over the chosen Earth model. 00:01:18 Discard geometric false positives Suppose the covering cells return two chargers, one seven hundred eighty metres away and another two thousand forty metres away. Both are plausible candidates at this stage. Calculate the distance to each candidate from its latitude and longitude. Compare those distances with the same two-thousand-metre radius used to construct the cover. Keep the charger at seven hundred eighty metres and drop the one at two thousand forty metres. Under an inclusive radius, a charger exactly two thousand metres away also belongs. An extra candidate costs a distance check. A missed candidate can never be rescued by sorting or distance checks downstream. That asymmetry explains why the first stage intentionally reads some area outside the circle. 00:02:01 Sort valid results and bound work We now sort the remaining results by actual distance, so the closest charger appears first in the list. If a candidate appears twice through overlapping reads, deduplicate by charger identifier. Two distinct chargers at the same coordinates still deserve separate entries. Smaller cells generally reduce the extra area fetched, but a fixed search circle can then span many more lookups. Compare both costs on realistic distributions. For the nearest three, filter first and take the first three after sorting, with a stable identifier as a tie breaker. Taking three arbitrary bucket entries first could discard the nearest valid charger. 00:02:40 Try the awkward cases We test locations near the date line and opposite sides of the boundary, confirming the model handles edge cases correctly. Compare the indexed answer to a full scan with the same distance function. In our two-charger case, exactly one passes. An unexpected difference deserves investigation. Even if my home cell has no shops, the system still finds nearby chargers by checking adjacent cells and computing real distances. Put a reference point exactly on the search circle, one just outside, and one just across the longitude wrap. Repeat at several latitudes. The index and a full distance scan should agree about every included identifier.