Skip to content

Fix getClusters dropping points exactly on the query bbox edge#261

Closed
spokodev wants to merge 1 commit into
mapbox:mainfrom
spokodev:fix/getclusters-bbox-edge
Closed

Fix getClusters dropping points exactly on the query bbox edge#261
spokodev wants to merge 1 commit into
mapbox:mainfrom
spokodev:fix/getclusters-bbox-edge

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown

Fixes #234.

getClusters(bbox, zoom) silently drops a point whose longitude or latitude falls exactly on an edge of the query bbox, even though the documented bbox [westLng, southLat, eastLng, northLat] is inclusive.

const index = new Supercluster().load(points);
// bbox computed as the exact extent of the points:
index.getClusters([minLng, minLat, maxLng, maxLat], 16);
// the point sitting on the min/max edge is missing from the result

This hits the common pattern of computing the bbox as the extent of the points and then querying with it, which is the scenario in #234.

Cause

Points are stored projected and rounded to Float32 (fround(lngX(lng))), but the getClusters range query is built from full-precision Float64 bounds (lngX(minLng), ...). fround can round a stored coordinate slightly outside its exact double, so a point that should sit on the edge ends up just beyond the inclusive range bound and is excluded.

Fix

Project the query bounds through the same fround the stored coordinates use, so an on-edge point compares equal. fround is already defined in the module. The fix is scoped to getClusters.

Testing

Added a test using the coordinates from #234, where the SW-corner point lies exactly on the bbox edge. On the current code that point is dropped (['a','c','d']); with the fix all four are returned. The rest of the suite stays green (16 tests).

Points are stored as Float32 (fround), but the getClusters range query
was built from full-precision Float64 bounds. fround can round a stored
coordinate slightly outside its exact value, so a point whose lng/lat
equals a bbox edge fell just outside the inclusive range and was
dropped. Project the query bounds through the same fround so the edge
comparison is exact. Fixes mapbox#234.
@spokodev spokodev requested a review from a team as a code owner July 2, 2026 04:49
@mourner

mourner commented Jul 2, 2026

Copy link
Copy Markdown
Member

Thanks for the fix! This is superceded by #258 though (no fround anymore).

@mourner mourner closed this Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing points when bounds are scoped to the extent of points

2 participants