Repository navigation
Fix antimeridian line clipping dropping half of >180° segments (#408) - #410
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:Root cause
BoundingBox.clipLinePartsunwraps 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.swiftnow computes the exact range of whole-period box copies that overlap the unwrapped line's x range: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:
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:180,-180,179.999,-179.999,170,-170,originShift),noSRID,Verification
swift build— clean, no warningsswift test— 2708 tests / 175 suites passCHANGELOG.md— entry added under## Unreleased