Skip to content

Add ClipByRect operation to geos package#705

Merged
peterstace merged 4 commits intomasterfrom
rect_clip
Mar 27, 2026
Merged

Add ClipByRect operation to geos package#705
peterstace merged 4 commits intomasterfrom
rect_clip

Conversation

@peterstace
Copy link
Copy Markdown
Owner

@peterstace peterstace commented Mar 27, 2026

Description

Expose GEOSClipByRect_r via the geos package, providing an optimised way to
clip a geometry to an axis-aligned rectangle. This is faster than computing a
full Intersection with a rectangular polygon because GEOS uses specialised
internal clipping.

The public API accepts a geom.Envelope (the library's existing abstraction for
axis-aligned rectangles). An empty envelope short-circuits to an empty geometry
without calling into GEOS. Version gating is included for GEOS 3.5.0.

Check List

Have you:

  • Added unit tests?

    Yes — 8 table-driven test cases covering polygon fully inside/partially
    overlapping/fully outside, linestring clipping, point inside/outside, empty
    input geometry, and empty envelope.

  • Add cmprefimpl tests? (if appropriate?)

    No — this is a new GEOS-only operation with no pure-Go reference
    implementation to compare against.

  • Updated release notes? (if appropriate?)

    Yes — added to the Unreleased section in CHANGELOG.md.

  • Updated the README.md (if new functionality is added?)

    No — the README doesn't enumerate individual GEOS operations.

Related Issue

  • N/A

Benchmark Results

  • N/A — this is a thin wrapper around a GEOS C function; no custom algorithmic
    work to benchmark.

Expose GEOSClipByRect_r via the geos package, providing an optimised
way to clip a geometry to an axis-aligned rectangle. This is faster
than computing a full Intersection with a rectangular polygon because
GEOS uses specialised internal clipping.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR exposes GEOS’s optimized GEOSClipByRect_r through the public geos package as geos.ClipByRect, allowing fast clipping of geometries to an axis-aligned rectangle described by geom.Envelope, including GEOS version gating and an empty-envelope fast path.

Changes:

  • Add rawgeos.ClipByRect wrapper with GEOS ≥ 3.5.0 gating and C stub fallback for older GEOS headers/libs.
  • Add public geos.ClipByRect(g, rect) API that accepts geom.Envelope and short-circuits empty envelopes.
  • Add table-driven unit tests for clipping behavior and update CHANGELOG.md.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
internal/rawgeos/entrypoints.go Adds GEOS entrypoint + version gating/stub and the Go-level raw wrapper.
geos/entrypoints.go Adds the public geos.ClipByRect API taking a geom.Envelope.
geos/entrypoints_test.go Adds table-driven tests covering several geometry types and edge cases.
CHANGELOG.md Documents the new public API in the Unreleased section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@peterstace peterstace merged commit abb3854 into master Mar 27, 2026
1 check passed
@peterstace peterstace deleted the rect_clip branch March 27, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants