Find nearby eligible evidence with explicit coordinate systems, radius limits and geographic provenance.
Geospatial retrieval: combine meaning, distance and jurisdiction
Define what the location means
Geospatial retrieval finds records whose physical coverage matters to the query. 'Show repair notices near station P-47' could mean notices located at a point, assets within a radius, or policies applying to a jurisdiction polygon. Those are different predicates. Store geometry type, coordinate reference system, accuracy, valid interval and source identity with the record. A document mentioning a city name is not necessarily located there. Query routing should distinguish place intent from a text-only mention before choosing a spatial operation.
Filter space before semantic rank
Apply tenant permission, active revision and geographic eligibility before semantic ranking. A nearby restricted maintenance ticket should not appear in a result count, map marker or generated answer. Use an indexed radius predicate for point proximity, then compute exact distance for candidates; use containment or intersection for coverage areas. Do not compare raw latitude-longitude degree differences with a radius in meters. Corpus permissions remain part of the spatial query, not a presentation-layer patch.
Keep uncertainty visible
A geocoded address may identify a street rather than a building. A device location can drift; a service district boundary can change. Store location accuracy and boundary version, and avoid a definitive inside/outside claim when an uncertainty circle crosses the edge. For a 'within 2 km' question, state whether distance is straight-line, walking route or drive time. The retrieval result should include the location source and the calculated distance, so the claim can be checked. Temporal retrieval governs historical boundaries.
Combine place and text without score confusion
A spatial index can return a bounded set of eligible records, after which lexical or vector ranking identifies the relevant notice. For broad areas, text search may produce candidates first, followed by exact geometry checks. Choose order from selectivity and cost, but keep both filters mandatory. Ranking a notice highly cannot make an out-of-area record eligible. Do not merge kilometers and semantic similarity into one unexplained scalar. Source budgets can reserve results across nearby notices and region-wide rules.
Test boundaries, not just centers
Judge cases on both sides of a district border, exactly at the radius, with missing coordinates, stale geocodes, overlapping regions and different tenant scopes. Report eligible-record recall separately from relevance and answer support. Include a named location that appears in a document body but lies outside its service area. A system that finds the right topic and wrong jurisdiction must fail even if its prose sounds plausible. Capture geometry version and distance method in the evaluation trace.
Worked contract
from dataclasses import dataclass
from math import asin, cos, radians, sin, sqrt
@dataclass(frozen=True)
class ServiceNotice:
notice_id: str
tenant: str
latitude: float
longitude: float
text: str
def distance_km(lat_a: float, lon_a: float, lat_b: float, lon_b: float) -> float:
if any(not -90 <= lat <= 90 for lat in (lat_a, lat_b)):
raise ValueError("invalid latitude")
if any(not -180 <= lon <= 180 for lon in (lon_a, lon_b)):
raise ValueError("invalid longitude")
north = radians(lat_b - lat_a)
east = radians(lon_b - lon_a)
arc = sin(north / 2) ** 2 + cos(radians(lat_a)) * cos(radians(lat_b)) * sin(east / 2) ** 2
return 2 * 6371.0088 * asin(min(1.0, sqrt(arc)))
def nearby(notices: list[ServiceNotice], tenant: str,
latitude: float, longitude: float, radius_km: float) -> list[str]:
if radius_km < 0:
raise ValueError("negative radius")
return [notice.notice_id for notice in notices if notice.tenant == tenant
and distance_km(latitude, longitude, notice.latitude, notice.longitude) <= radius_km]
notices = [ServiceNotice("pump-47", "north", 28.6139, 77.2090, "Valve inspection"),
ServiceNotice("pump-83", "north", 28.7041, 77.1025, "Valve inspection"),
ServiceNotice("private", "south", 28.6139, 77.2090, "Valve inspection")]
assert nearby(notices, "north", 28.6139, 77.2090, 2) == ["pump-47"]Performance and operating cost
The transparent scan costs O(N) distance calculations and O(K) output space for K matches. A spatial index narrows large corpora before exact distance checks; maintaining it adds storage and write work. Haversine computes a spherical straight-line approximation, not route distance or polygon coverage. Use a geography-aware database predicate for production radius searches and measure boundary errors where accuracy matters.
Common Mistakes
- Comparing coordinate degrees directly with kilometers.
- Treating a place name in text as a verified record location.
- Filtering geography after showing restricted candidate metadata.
- Ignoring location uncertainty near a boundary.
Validation drill
Prepare 61 notices across two districts with five missing geocodes and seven boundary cases. Compare point-radius and jurisdiction queries, reporting eligible recall, false inclusions and supported location claims.
