Ankit Patel
WritingPapersLabsAboutContact
Distributed Systems & Data··2 min read·972 views

A Cheaper Spatial Join Still Has to Find the Point

In a point-in-polygon join one task covering a dense downtown is still running long after the others have finished

On large geospatial pipelines, I have watched teams burn huge compute budgets answering one simple question: which delivery zone contains this point?

The naive version tests millions of coordinates against thousands of detailed polygon boundaries. A spatial index cuts compute cost by pruning pairs before exact geometry runs. That optimization only stays valid when the cheap candidate step keeps every polygon the exact predicate would match.

Cost-reduction projects break that invariant more often than teams expect.

Candidate filters must be conservative

A common Spark pattern maps each incoming coordinate to an H3 cell and joins against a precomputed cell-to-zone lookup table. Most points match zero or one zone, skipping heavy point-in-polygon evaluation almost entirely.

/* Expensive: runtime point-in-polygon across candidate pairs */
SELECT e.event_id, v.venue_id
FROM location_events e
JOIN venues v ON ST_Contains(v.boundary_geom, ST_Point(e.lng, e.lat));

/* Pre-indexed: discrete integer H3 cell match + border check */
SELECT e.event_id, v.venue_id
FROM location_events e
JOIN venue_h3_cells v ON e.h3_res10 = v.h3_res10
WHERE v.is_interior OR ST_Contains(v.boundary_geom, ST_Point(e.lng, e.lat));

Because that lookup is a candidate filter, false positives are cheap and false negatives are fatal. Admitting an extra border polygon costs one exact geometry check downstream, whereas dropping a valid polygon loses the match permanently.

Take a narrow delivery corridor that clips the corner of an H3 hexagon while the cell's centroid sits outside the polygon. H3's 3.x polyfill API determines membership by cell center, so it omits that hexagon from the lookup table. Every customer coordinate landing inside that clipped corner disappears before exact geometry ever runs.

Center-based polygon fill is compact, fast and documented, yet it is not a conservative boundary covering. Exact spatial joins require either a full intersecting-cell cover or bounding-box overlap filters of the kind PostGIS uses before running exact polygon predicates.

Separate cell products from exact polygon contracts

Teams accept grid approximation for two very different reasons, and mixing them causes quiet data bugs.

Sometimes the product itself is defined on cells, such as regional demand heatmaps or privacy-bucketed hex tiles. There, the grid cell is the specification.

When the business contract promises an exact delivery zone, municipal boundary or geofence, the grid is strictly an index accelerator. Both candidate completeness and exact boundary predicates (ST_Contains versus ST_Covers for points sitting right on an edge) have to be specified once so batch analytics and live serving never disagree. Even an exact geometric join still operates on noisy mobile GPS fixes, so polygon precision never removes sensor error from the input.

Follow the work that shifts into skew and maintenance

Precomputing cell lookups trades CPU in the join for index storage, refresh latency and partition skew. Polygon edits have to propagate cleanly into the lookup table. Ultra-fine H3 resolutions explode lookup row counts, while coarse resolutions pass too many candidates into exact geometry. Switching H3 resolutions mid-pipeline also takes care because parent hexagons do not geometrically contain every square meter of their seven children.

Profile candidate fan-out alongside Spark stage timings: candidate pairs per point, share of records hitting exact geometry, shuffle volume and straggler tasks over dense downtown cells where one partition holds half the afternoon's traffic.

Good cloud cost optimization makes the right query cheaper. Dropping points near polygon edges just changes the answer without telling anyone.

References: H3 3.x region functions and indexing, and PostGIS spatial indexing.

Continue in Distributed Systems & Data