Skip to content

Fix polygon clipping crash on pole-reaching bounding boxes (#409) - #411

Merged
trasch merged 2 commits into
mainfrom
fix/409-polygon-clip-pole-crash
Oct 30, 2026
Merged

trasch merged 2 commits into
mainfrom
fix/409-polygon-clip-pole-crash

Conversation

@trasch

@trasch trasch commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Closes #409

Problem

Clipping a polygon to a box whose latitude reaches ±90° crashed:

let box = BoundingBox(
    southWest: Coordinate3D(latitude: -90.0, longitude: -180.0),
    northEast: Coordinate3D(latitude: 90.0, longitude: 180.0))

let polygon = Polygon([[ ... ±60° ... ]])!
polygon.clipped(to: box)   // fatal error
Fatal error: Double value cannot be converted to Int because it is either infinite or NaN
Overlay.PolygonIndex.Grid.row   Overlay.swift:248

The polygon is well inside the box, but the box's ±90° latitudes project to infinity in EPSG:3857, so the overlay grid's extent and cell size became NaN and the Int conversion trapped.

Root cause

Two independent trap sites, both fed by non-finite coordinates:

  1. Overlay.PolygonIndex.Grid — the uniform cell index named in the issue's stack trace. cellHeight became NaN, then Int((value - origin) / cellSize) trapped.
  2. OverlayPreparedRing — a second scanline index with the same unguarded Int(...) pattern. This one only surfaced once the first stopped crashing.

Note that Int(...) traps on huge-but-finite values too, not just NaN/infinity.

Fix

Sources/GISTools/Algorithms/Overlay.swift:

  • Root cause — sanitizedForOverlay runs at the top of overlayPolygons and replaces non-finite coordinates with ±GISTool.originShift. Finite values are untouched, so geometry above 85.05° keeps its exact values in operations that do not involve a pole-reaching box.
  • Defence in depth — both grids refuse to build from non-finite bounds, and both clamp their index as a Double before converting to Int. PolygonIndex falls back to a single cell holding every polygon, so containment tests stay correct rather than silently returning false.

Because the fix lives in Overlay, it also fixes the same latent crash in union, intersection, difference and symmetricDifference, which share the choke point.

Behaviour

A polygon inside a pole-reaching box now comes back unchanged (the issue's preferred outcome). No clamping is applied to finite geometry, so union/intersection of polygons between 85.05° and 90° still return their exact values.

Tests

  • BoundingBoxClipTests: the issue's exact case, a south-pole-only box, and the EPSG:4978 cross-projection path.
  • OverlayTests: grid robustness against infinite and NaN bounds, plus a check that the sanitizer clamps non-finite values and leaves finite polygons untouched.

Verification

  • swift build — clean, no warnings
  • swift test — 2707 tests / 175 suites pass
  • SwiftFormat — no new violations (Overlay.swift at 15, same as base; OverlayTests.swift at 0)

Note for reviewers

My first attempt at this turned the crash into a hang: I had written a test that fed the overlay a genuinely infinite polygon, and sanitizing it produced a 20-million-metre-tall shape that sent poleOfInaccessibility into a multi-million-cell grid search. That test was replaced with ones that exercise the grid guards directly. The real-world case — a ±90° box against a normal polygon — is unaffected, since the box only spans the ordinary Mercator world height.

Clipping a polygon to a box whose latitude reaches ±90° trapped in the
overlay. A latitude of ±90° projects to infinity in the EPSG:3857 working
frame, which made the overlay grid's cell size NaN and trapped on the
subsequent Int conversion.

Replace non-finite coordinates with the Web Mercator limit at the overlay
entry point, before any geometry or grid math runs, so a polygon inside the
box comes back unchanged. Only non-finite values are touched: finite
coordinates keep their exact values, so geometry above the Mercator limit is
unaffected in operations that do not involve a pole-reaching box.

Because the fix lives in Overlay, it also fixes the same latent trap in
union, intersection, difference and symmetricDifference, which share the
choke point.

Harden both overlay grids as well: the uniform cell index and the
prepared-ring scanline index now refuse to build from non-finite bounds, and
clamp their indices as Double before converting to Int (a huge but finite
value traps just like NaN). The prepared-ring index was a second, independent
trap site that only surfaced once the first stopped crashing.

Adds regression tests for the issue's case, a south-pole-only box, the
EPSG:4978 cross-projection path, and grid robustness against inf and NaN
bounds.
@trasch trasch self-assigned this Oct 30, 2026
@trasch trasch added the bug Something isn't working label Oct 30, 2026
…pole-crash

# Conflicts:
#	CHANGELOG.md
#	Tests/GISToolsTests/Algorithms/BoundingBoxClipTests.swift
@trasch
trasch merged commit 7a774ee into main Oct 30, 2026
6 checks passed
@trasch
trasch deleted the fix/409-polygon-clip-pole-crash branch October 30, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polygon clipping to a bounding box that reaches ±90° latitude crashes (NaN overlay grid)

1 participant