Skip to content

Fix antimeridian line clipping dropping half of >180° segments (#408) - #410

Merged
trasch merged 1 commit into
mainfrom
fix/408-antimeridian-line-clip
Oct 30, 2026
Merged

trasch merged 1 commit into
mainfrom
fix/408-antimeridian-line-clip

Conversation

@trasch

@trasch trasch commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Closes #408

Problem

LineString.clipped(to:) lost one half of any segment spanning more than 180° of longitude, even when the segment lay entirely inside the clip box:

let box = BoundingBox(southWest: Coordinate3D(latitude: -85.0, longitude: -110.0),
                      northEast: Coordinate3D(latitude: 85.0, longitude: 110.0))
let line = LineString([Coordinate3D(x: -100.0, y: 0.0, projection: .epsg4326),
                       Coordinate3D(x: 100.0, y: 0.0, projection: .epsg4326)])!

line.clipped(to: box)?.lineStrings.map { $0.coordinates.map(\.longitude) }
// before: [[110.0, 100.0]]          ❌ left half gone
// after:  [[110.0, 100.0], [-100.0, -110.0]]  ✅

Root cause

BoundingBox.clipLineParts unwraps coordinates across the antimeridian, then shifted the whole line by one world period chosen from the line's centre. A >180° segment unwraps to more than one world period, so it reaches into two world copies of the clip box. Only the copy nearest the line centre was clipped, so the other half was always discarded.

Fix

Sources/GISTools/Algorithms/BoundingBoxClip.swift now computes the exact range of whole-period box copies that overlap the unwrapped line's x range:

k >= (lineMinX - boxMaxX) / period
k <= (lineMaxX - boxMinX) / period

Every overlapping copy is clipped and all resulting pieces are kept (deduplicated by normalized coordinates). Non-wrapping projections (noSRID, EPSG:4978) keep the unchanged direct path.

This also fixes two related losses that predated the issue:

  • a plain antimeridian-crossing line clipped against the world box — the z0 case, where a route from 179° to -179° previously kept only the western half;
  • multi-segment lines whose segments land in different world copies.

Behaviour note

A >180° segment is still unwrapped the short way through ±180°, per the library's existing antimeridian rule. The clip now keeps both pieces that rule implies rather than one.

Tests

Eight new regression tests in Tests/GISToolsTests/Algorithms/BoundingBoxClipTests.swift:

  • the exact issue example,
  • world box, and boxes ending at 180, -180, 179.999, -179.999, 170, -170,
  • a crossing box narrower than the segment,
  • EPSG:3857 (via originShift),
  • EPSG:4978 (no wraparound — clipped flat),
  • noSRID,
  • a plain antimeridian-crossing line inside the world box.

Verification

  • swift build — clean, no warnings
  • swift test — 2708 tests / 175 suites pass
  • SwiftFormat lint — no new violations (the two reported in the test file are pre-existing on untouched line 187)
  • CHANGELOG.md — entry added under ## Unreleased

BoundingBox.clipLineParts unwrapped coordinates across the antimeridian and
then shifted the whole line by a single world period chosen from the line's
centre. A segment spanning more than 180° of longitude unwraps to a length
greater than one world period, so it reaches into two world copies of the
clip box; only the copy nearest the centre was clipped, silently discarding
the other half.

Compute the exact range of whole-period box copies that overlap the unwrapped
line's x range and clip against every one of them, keeping all resulting
pieces. Non-wrapping projections keep the direct path.

This also fixes a plain antimeridian-crossing line clipped against the world
box (the z0 case) and multi-segment lines whose segments land in different
world copies.

Adds regression tests for the issue example, boxes ending at ±180 and just
inside either edge, a crossing box narrower than the segment, EPSG:3857,
EPSG:4978, noSRID and the z0 world box.
@trasch trasch self-assigned this Oct 30, 2026
@trasch trasch added the bug Something isn't working label Oct 30, 2026
@trasch
trasch merged commit fadb1fb into main Oct 30, 2026
6 checks passed
@trasch
trasch deleted the fix/408-antimeridian-line-clip branch October 30, 2026 11:34
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.

LineString.clipped(to:) keeps only one half of a segment spanning more than 180° of longitude

1 participant