Skip to content

Commit

Permalink
Merge pull request #509 from peterstace/geos_unary_union
Browse files Browse the repository at this point in the history
Wrap GEOS's Unary Union
  • Loading branch information
peterstace committed May 23, 2023
2 parents 90b2aa2 + 6dd2424 commit 98c7404
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Adds a wrapper in the `geos` package for the `GEOSUnaryUnion_r` function
(exposed as `UnaryUnion`).

## v0.42.1

2023-05-05
Expand Down
10 changes: 10 additions & 0 deletions geos/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,13 @@ func CoverageUnion(g geom.Geometry, opts ...geom.ConstructorOption) (geom.Geomet
})
return result, wrap(err, "executing GEOSCoverageUnion_r")
}

// UnaryUnion is a single argument version of Union. It is most useful when
// supplied with a GeometryCollection, resulting in the union of the
// GeometryCollection's child geometries.
func UnaryUnion(g geom.Geometry, opts ...geom.ConstructorOption) (geom.Geometry, error) {
result, err := unaryOpG(g, opts, func(ctx C.GEOSContextHandle_t, g *C.GEOSGeometry) *C.GEOSGeometry {
return C.GEOSUnaryUnion_r(ctx, g)
})
return result, wrap(err, "executing GEOSUnaryUnion_r")
}
18 changes: 18 additions & 0 deletions geos/entrypoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,24 @@ func TestMakeValid(t *testing.T) {
}
}

func TestUnaryUnion(t *testing.T) {
for i, tt := range []struct {
input string
wantOutput string
}{
{
"GEOMETRYCOLLECTION(POLYGON((0 0,0 2,2 2,2 0,0 0)),POLYGON((1 1,1 3,3 3,3 1,1 1)))",
"POLYGON((0 0,2 0,2 1,3 1,3 3,1 3,1 2,0 2,0 0))",
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got, err := UnaryUnion(geomFromWKT(t, tt.input))
expectNoErr(t, err)
expectGeomEq(t, got, geomFromWKT(t, tt.wantOutput), geom.IgnoreOrder)
})
}
}

func TestCoverageUnion(t *testing.T) {
for i, tc := range []struct {
input string
Expand Down

0 comments on commit 98c7404

Please sign in to comment.