diff --git a/go.sum b/go.sum index 15af9c4..1068d42 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= diff --git a/predicates.go b/predicates.go index 3e64f38..5e25e93 100644 --- a/predicates.go +++ b/predicates.go @@ -85,3 +85,18 @@ func polygonToGeometry(geofence *geom.Polygon) *geos.Geometry { return geos.Must(geos.NewPolygon(shellGeos, holes...)) } + +func polygonToGeometryExample(geofence *geom.Polygon) *geos.Geometry { + // Convert the outer shell to geos format. + shell := geofence.LinearRing(0).Coords() + shellGeos := geomToGeosCoords(shell) + + // Convert each hole to geos format. + numHoles := geofence.NumLinearRings() - 1 + holes := make([][]geos.Coord, numHoles) + for i := 0; i < numHoles; i++ { + holes[i] = geomToGeosCoords(geofence.LinearRing(i).Coords()) + } + + return geos.Must(geos.NewPolygon(shellGeos, holes...)) +} diff --git a/predicates_test.go b/predicates_test.go index 1dfec6d..8ce1d52 100644 --- a/predicates_test.go +++ b/predicates_test.go @@ -56,3 +56,12 @@ func TestPredicates(t *testing.T) { }) } } + +func TestPolygonToGeometryExample(t *testing.T) { + geofence := readFileAsGeometry(t, "testdata/regents.geojson") + + geom := polygonToGeometryExample(geofence) + perimeterLength, err := geom.Length() + require.NoError(t, err) + assert.Equal(t, geofence.Length(), perimeterLength) +}