Repository navigation
Align geometry-level contains/covers with OGC/PostGIS semantics - #267
Merged
Merged
Conversation
contains(_:) now implements OGC ST_Contains: the receiver must cover the other geometry and their interiors must intersect. covers(_:) is the boundary-inclusive predicate and no longer delegates to contains. Coverage is segment-aware: segments are split at container boundary intersections and each sub-segment is tested, so concave containers, holes and lines leaving a notch are handled. Line boundaries follow OGC rules (endpoints are boundary, MultiLineStrings use mod-2). Predicates operate in the receiver's projection, and antimeridian-crossing geometries are cut at ±180° before checking. Document the semantics in CHANGELOG.md and add regression tests for concave coverage, hole-filling polygons, boundary lines and line endpoints across projections.
Cover hole boundaries on antimeridian-crossing polygons: - EPSG:4326 natively: outer/hole edges and vertices, +180/-180 equivalence, interior-only contains vs boundary-inclusive covers. - EPSG:4978 via projected coordinates: interior, hole interior and exterior. - EPSG:3857 via cutAtAntimeridian(): the supported representation for projected CRSs, including hole boundary semantics. - Geometry-level covers/contains: a contained polygon above the hole, a polygon filling the hole, and a line crossing the hole. Note: a raw antimeridian-crossing polygon in EPSG:3857 is not handled by point-in-polygon (ring wraparound detection is geographic-only), so the 3857 test cuts first. This predates the OGC work and is unrelated to the overlay algorithms.
Overlay previously operated directly on raw EPSG:3857 coordinates. Antimeridian-crossing rings therefore took the long way around the world, while point-in-polygon treated them as the short way, so overlay operations on crossing polygons produced wrong or empty results. - Add Overlay.AntimeridianFrame: rings are unwrapped by taking the shortest path across the projection's wraparound extent, polygons are aligned to the first polygon's frame, and results are wrapped back afterwards. - Pass the normalized polygons to the overlay boundary test. Union, Difference and SymmetricDifference now use that parameter instead of captured originals; Intersection already did. - Deduplicate coincident same-direction edges, which previously produced a ring plus an identical hole when overlaying identical polygons. - Make Ring.locate and the Polygon.locate bbox fallback use the projection's wraparound extent instead of hard-coded degrees, so raw crossing polygons work in EPSG:3857 too. - Add overlay antimeridian tests for intersection, union, difference and symmetric difference, including a crossing hole, and restore the raw 3857 point-in-polygon test.
Overlay only split edges at proper intersections. When two polygons shared part of an edge (for example two antimeridian-crossing polygons with the same longitudes), the overlapping edge portions were never split at each other's endpoints. Both full-length edges survived boundary filtering, so buildRings could fail to close and the union result was malformed. Split each edge at the other edge's endpoints when the segments are collinear and overlapping, so the shared portion can be matched by mergeReversePairs. This was exposed by the new antimeridian union test on the CI runner.
…on results The previous collinear fix only ran when LineSegment.intersection returned nil. On macOS, projection rounding makes the shared longitudes differ by tiny amounts, so intersection can return a point instead; the overlapping edge portions were then never split at both endpoints and the union still produced unclosed rings. Always split an edge at the other edge's endpoints using a micron-scale collinearity tolerance, and add the proper intersection point when one exists. Merge and deduplicate edges with the same quantized vertex keys used by ring assembly, so tiny platform-dependent coordinate differences do not leave duplicate boundary edges. Add a near-collinear union regression test. Tighten the tolerance to a micron after a metre-scale tolerance disturbed dense buffer geometry. Full suite: 2610 tests / 193 suites.
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.
Summary
Follow-up to #266. The point API is now OGC-aligned, but the geometry-to-geometry predicates still used boundary-inclusive vertex checks. This PR makes
GeoJson.contains(_:)andGeoJson.covers(_:)match OGC Simple Features / PostGIS, and adds antimeridian support to overlay operations.Semantics
contains(_:)is interior-only (ST_Contains).covers(_:)is boundary-inclusive (ST_Covers) and no longer delegates tocontains.isWithin(_:)/coveredBy(_:)remain the converses.Line boundaries follow OGC rules:
LineStringendpoints are.boundary, other on-line points.interiorMultiLineStringuses the mod-2 endpoint ruleSo
line.contains(endpoint)isfalsewhileline.covers(endpoint)istrue, and a polygon does not contain a line that lies entirely on its boundary.Changes
poleOfInaccessibility) must be covered,CHANGELOG.mdwith the geometry-level semantics.This fixes cases the old implementation got wrong:
coversonly),Antimeridian overlay support
Overlay previously operated directly on raw EPSG:3857 coordinates. A crossing ring therefore took the long way around the world while point-in-polygon took the short way, so overlay on crossing polygons was inconsistent.
Overlay.AntimeridianFrame:[-extent, extent).Union,DifferenceandSymmetricDifferencenow use that parameter instead of captured originals;Intersectionalready did.Ring.locateand thePolygon.locatebbox fallback now use the projection's wraparound extent instead of hard-coded degrees, so raw crossing polygons also work in EPSG:3857.Antimeridian tests
4326hole matrix: outer/hole edges and vertices,+180/-180equivalence,containsvscovers.4978projected hole semantics (boundary points excluded due to Point-in-polygon: on-boundary test uses== 0.0on a non-robust cross product, so points exactly on an edge are often not detected #258's non-robust edge test).3857, includingMultiPolygon.covers/contains: contained polygon above the hole, polygon filling the hole, line crossing the hole.Verification
BooleanContains/BooleanCovers: 7 of the new tests fail, confirming they exercise the fixed behaviour.antimeridianCrossingIdentical,antimeridianCrossingOverlap,concaveHullCrossingAntimeridian) now pass through the normalized path without special casing.Follow-up to #266.