Add ClipByRect operation to geos package#705
Merged
peterstace merged 4 commits intomasterfrom Mar 27, 2026
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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.ClipByRectwrapper with GEOS ≥ 3.5.0 gating and C stub fallback for older GEOS headers/libs. - Add public
geos.ClipByRect(g, rect)API that acceptsgeom.Envelopeand 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.
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.
Description
Expose
GEOSClipByRect_rvia thegeospackage, providing an optimised way toclip a geometry to an axis-aligned rectangle. This is faster than computing a
full
Intersectionwith a rectangular polygon because GEOS uses specialisedinternal clipping.
The public API accepts a
geom.Envelope(the library's existing abstraction foraxis-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
Benchmark Results
work to benchmark.