From 6c3195cee20f7a94cfc0f193c9927dc1b0852f86 Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 24 Apr 2020 11:48:54 +0100 Subject: [PATCH 1/3] Remove test for coverage example --- hashfill_test.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/hashfill_test.go b/hashfill_test.go index 3874eeb..8723494 100644 --- a/hashfill_test.go +++ b/hashfill_test.go @@ -42,19 +42,19 @@ func TestRecursiveFillIntersectsNotFixed(t *testing.T) { assert.True(t, contains(hashes, "gcpvhtb0")) // 8 } -func TestRecursiveFillIntersectsFixed(t *testing.T) { - geofence := readFileAsGeometry(t, "testdata/regents.geojson") - filler := NewRecursiveFiller( - WithMaxPrecision(8), - WithFixedPrecision(), - ) - - hashes, err := filler.Fill(geofence, FillIntersects) - assert.NoError(t, err) - assert.Len(t, hashes, 3242) - assert.False(t, contains(hashes, "gcpvht0")) // No precision 7 - assert.True(t, contains(hashes, "gcpvhtb0")) // 8 -} +// func TestRecursiveFillIntersectsFixed(t *testing.T) { +// geofence := readFileAsGeometry(t, "testdata/regents.geojson") +// filler := NewRecursiveFiller( +// WithMaxPrecision(8), +// WithFixedPrecision(), +// ) + +// hashes, err := filler.Fill(geofence, FillIntersects) +// assert.NoError(t, err) +// assert.Len(t, hashes, 3242) +// assert.False(t, contains(hashes, "gcpvht0")) // No precision 7 +// assert.True(t, contains(hashes, "gcpvhtb0")) // 8 +// } func TestRecursiveFillContains(t *testing.T) { geofence := readFileAsGeometry(t, "testdata/london.geojson") From fc2fbc0add553a454472e403cee8c4c639115d72 Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 24 Apr 2020 11:50:16 +0100 Subject: [PATCH 2/3] Add comment: --- hashfill.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hashfill.go b/hashfill.go index 8f3901d..cb5c490 100644 --- a/hashfill.go +++ b/hashfill.go @@ -80,6 +80,7 @@ func NewRecursiveFiller(options ...Option) *RecursiveFiller { // It works by computing a set of variable length geohashes which are contained // in the polygon, then optionally extending those hashes out to the specified precision. func (f RecursiveFiller) Fill(fence *geom.Polygon, mode FillMode) ([]string, error) { + // Comment to demonstrate coverage in this func. hashes, err := f.computeVariableHashses(fence, mode, "") if err != nil { return nil, err @@ -91,6 +92,7 @@ func (f RecursiveFiller) Fill(fence *geom.Polygon, mode FillMode) ([]string, err // If we want fixed precision, we have to iterate through each hash and split it down // to the precision we want. + // Comment to demonstrate coverage in this func. out := make([]string, 0, len(hashes)) for _, hash := range hashes { extended := f.extendHashToMaxPrecision(hash) From af4c0e3ea4d69cd35046df481fe2eccf085a02dd Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 24 Apr 2020 11:56:21 +0100 Subject: [PATCH 3/3] Better example --- go.sum | 1 + hashfill.go | 2 -- hashfill_test.go | 26 +++++++++++++------------- predicates.go | 15 +++++++++++++++ predicates_test.go | 9 +++++++++ 5 files changed, 38 insertions(+), 15 deletions(-) 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/hashfill.go b/hashfill.go index cb5c490..8f3901d 100644 --- a/hashfill.go +++ b/hashfill.go @@ -80,7 +80,6 @@ func NewRecursiveFiller(options ...Option) *RecursiveFiller { // It works by computing a set of variable length geohashes which are contained // in the polygon, then optionally extending those hashes out to the specified precision. func (f RecursiveFiller) Fill(fence *geom.Polygon, mode FillMode) ([]string, error) { - // Comment to demonstrate coverage in this func. hashes, err := f.computeVariableHashses(fence, mode, "") if err != nil { return nil, err @@ -92,7 +91,6 @@ func (f RecursiveFiller) Fill(fence *geom.Polygon, mode FillMode) ([]string, err // If we want fixed precision, we have to iterate through each hash and split it down // to the precision we want. - // Comment to demonstrate coverage in this func. out := make([]string, 0, len(hashes)) for _, hash := range hashes { extended := f.extendHashToMaxPrecision(hash) diff --git a/hashfill_test.go b/hashfill_test.go index 8723494..3874eeb 100644 --- a/hashfill_test.go +++ b/hashfill_test.go @@ -42,19 +42,19 @@ func TestRecursiveFillIntersectsNotFixed(t *testing.T) { assert.True(t, contains(hashes, "gcpvhtb0")) // 8 } -// func TestRecursiveFillIntersectsFixed(t *testing.T) { -// geofence := readFileAsGeometry(t, "testdata/regents.geojson") -// filler := NewRecursiveFiller( -// WithMaxPrecision(8), -// WithFixedPrecision(), -// ) - -// hashes, err := filler.Fill(geofence, FillIntersects) -// assert.NoError(t, err) -// assert.Len(t, hashes, 3242) -// assert.False(t, contains(hashes, "gcpvht0")) // No precision 7 -// assert.True(t, contains(hashes, "gcpvhtb0")) // 8 -// } +func TestRecursiveFillIntersectsFixed(t *testing.T) { + geofence := readFileAsGeometry(t, "testdata/regents.geojson") + filler := NewRecursiveFiller( + WithMaxPrecision(8), + WithFixedPrecision(), + ) + + hashes, err := filler.Fill(geofence, FillIntersects) + assert.NoError(t, err) + assert.Len(t, hashes, 3242) + assert.False(t, contains(hashes, "gcpvht0")) // No precision 7 + assert.True(t, contains(hashes, "gcpvhtb0")) // 8 +} func TestRecursiveFillContains(t *testing.T) { geofence := readFileAsGeometry(t, "testdata/london.geojson") 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) +}