Skip to content

Measure the great-circle distance to lines and polygons - #500

Open
Andy2003 wants to merge 3 commits into
masterfrom
fix/orthodromic-nearest-point
Open

Andy2003 wants to merge 3 commits into
masterfrom
fix/orthodromic-nearest-point

Conversation

@Andy2003

Copy link
Copy Markdown
Collaborator

spatial.withinDistance and spatial.closest picked the nearest point of a line or polygon in flat lon/lat degrees, then measured the great-circle distance to it. Away from the equator that point is wrong: from (0, 60) to LINESTRING (1 60, 0 62) it measured 62.7 km instead of 53.85 km, so geometries near the limit were dropped. Across ±180° it measured to the far end of the geometry.

Edges stay straight lines in lon/lat, as in the index and spatial.intersects. The distance is now the great-circle distance to the nearest point on them. Points inside a polygon still give 0.

The math costs more than the flat-degree shortcut. For one geometry, measured: about 10 µs at 100 vertices and about 0.8 ms at 10 000, compared with 1 µs and 0.07 ms before. Parsing the same 10 000-vertex polygon from WKT takes 5 ms.

Out of scope

  • The search window that selects the candidates is fixed in Cover the whole distance circle in the lon/lat search window #499. Without it, a geometry across ±180° can still be missed before its distance is measured.
  • The compat snapshots change in one value: the distance to LINESTRING (15 60, 16 61) drops from 6.21 to 4.96 km.

The nearest point was picked in flat lon/lat degrees, which is wrong away
from the equator and across the antimeridian. Edges between vertices are
now taken as great-circle arcs.
Great-circle arcs disagreed with the containment check, the index and
spatial.intersects, which all use straight lon/lat edges. The distance is
now the great-circle distance to the nearest point of the straight edge,
pruning edges that cannot be closer than the best match. An empty geometry
is infinitely far away.
@Andy2003 Andy2003 self-assigned this Nov 18, 2026
@Andy2003 Andy2003 added the fix Bugfix label Nov 18, 2026
@Andy2003
Andy2003 requested a lite review from Copilot November 18, 2026 19:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions

Copy link
Copy Markdown

Test Results

452 tests  +17   430 ✅ +17   2m 33s ⏱️ -42s
 33 suites ± 0    22 💤 ± 0 
 33 files   ± 0     0 ❌ ± 0 

Results for commit b8bf0c8. ± Comparison against base commit e390381.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants