Skip to content

Align geometry-level contains/covers with OGC/PostGIS semantics - #267

Merged
trasch merged 5 commits into
mainfrom
fix/ogc-geometry-containment
Oct 20, 2026
Merged

trasch merged 5 commits into
mainfrom
fix/ogc-geometry-containment

Conversation

@trasch

@trasch trasch commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

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(_:) and GeoJson.covers(_:) match OGC Simple Features / PostGIS, and adds antimeridian support to overlay operations.

Semantics

contains(A, B) = covers(A, B) && interiors(A) ∩ interiors(B) ≠ ∅
covers(A, B)   = no point of B lies in the exterior of A
  • contains(_:) is interior-only (ST_Contains).
  • covers(_:) is boundary-inclusive (ST_Covers) and no longer delegates to contains.
  • isWithin(_:) / coveredBy(_:) remain the converses.

Line boundaries follow OGC rules:

  • open LineString endpoints are .boundary, other on-line points .interior
  • closed lines have an empty boundary
  • MultiLineString uses the mod-2 endpoint rule

So line.contains(endpoint) is false while line.covers(endpoint) is true, and a polygon does not contain a line that lies entirely on its boundary.

Changes

  • Replaced the vertex-only type dispatch with segment-aware coverage:
    • every segment of B is split at its intersections with all boundary segments of A (including collinear-overlap endpoints),
    • each sub-segment midpoint is tested with point location,
    • an interior sample of B (centroid, falling back to poleOfInaccessibility) must be covered,
    • coverage is rejected when a hole boundary or another MultiPolygon component falls strictly inside B.
  • Added interior-intersection checks: point location for points/multipoints, split-segment samples for lines, and the areal case for polygons.
  • Predicates now operate in the receiver's projection, projecting the other geometry into it (previously everything was projected to EPSG:4326, which collapsed ECEF geometries near the origin).
  • Antimeridian-crossing geometries are cut at ±180° before checking; multi-geometries are evaluated per component.
  • Updated CHANGELOG.md with the geometry-level semantics.

This fixes cases the old implementation got wrong:

  • a line whose endpoints are inside a concave polygon but whose segment leaves through the notch (was covered, now not),
  • a polygon that fills one of the container's holes (was covered, now not),
  • boundary points/lines counted as contained (was true, now covers only),
  • line endpoints counted as contained.

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.

  • Added 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 by whole world-width shifts,
    • result coordinates are wrapped back into [-extent, extent).
  • The normalized polygons are passed to the overlay boundary test. Union, Difference and SymmetricDifference now use that parameter instead of captured originals; Intersection already did.
  • Coincident same-direction edges are deduplicated. Previously, overlaying identical polygons produced a ring plus an identical hole.
  • Ring.locate and the Polygon.locate bbox fallback now use the projection's wraparound extent instead of hard-coded degrees, so raw crossing polygons also work in EPSG:3857.
  • Polygons with a deliberately world-spanning edge are left unchanged (ambiguous per RFC 7946). Two separate non-crossing polygons on opposite sides of the seam are not joined.

Antimeridian tests

Verification

  • 2,609 tests / 193 suites pass; clean build, no warnings.
  • Regression check against the old BooleanContains/BooleanCovers: 7 of the new tests fail, confirming they exercise the fixed behaviour.
  • Existing antimeridian overlay tests (antimeridianCrossingIdentical, antimeridianCrossingOverlap, concaveHullCrossingAntimeridian) now pass through the normalized path without special casing.

Follow-up to #266.

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.
@trasch trasch added the bug Something isn't working label Oct 20, 2026
@trasch trasch self-assigned this Oct 20, 2026
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.
@trasch
trasch merged commit c3ba81b into main Oct 20, 2026
1 check passed
@trasch
trasch deleted the fix/ogc-geometry-containment branch October 20, 2026 08:32
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.

1 participant