From cfb1a4c84092f4ed3f40f7df8774e2dbefed9b95 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 9 Apr 2018 10:52:39 -0700 Subject: [PATCH] Fix intersects returning nothing in some cases Fixes #295 --- geojson/featurecollection.go | 5 +++-- geojson/featurecollection_test.go | 24 +++++++++++++++++++++++- geojson/geometrycollection.go | 4 ++-- geojson/geometrycollection_test.go | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/geojson/featurecollection.go b/geojson/featurecollection.go index 9870c64d..7e37b5bd 100644 --- a/geojson/featurecollection.go +++ b/geojson/featurecollection.go @@ -170,10 +170,10 @@ func (g FeatureCollection) IntersectsBBox(bbox BBox) bool { } for _, g := range g.Features { if g.IntersectsBBox(bbox) { - return false + return true } } - return true + return false } // Within detects if the object is fully contained inside another object. @@ -201,6 +201,7 @@ func (g FeatureCollection) Intersects(o Object) bool { return false } for _, f := range g.Features { + if f.Intersects(o) { return true } diff --git a/geojson/featurecollection_test.go b/geojson/featurecollection_test.go index b2bc28f2..47064443 100644 --- a/geojson/featurecollection_test.go +++ b/geojson/featurecollection_test.go @@ -1,6 +1,8 @@ package geojson -import "testing" +import ( + "testing" +) func TestFeatureCollection(t *testing.T) { testJSON(t, `{ @@ -90,3 +92,23 @@ func TestFeatureCollection(t *testing.T) { ] }`) } + +func TestPointBounding(t *testing.T) { + featureCollectionJSON := `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-75.1,56.283333]}}]}` + polyFeatureJSON := `{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "Polygon","coordinates": [[ + [-112.8515625,-29.535229562948444], + [85.4296875,-2.535229562948444], + [85.4296875,65.36683689226321], + [-112.8515625,65.36683689226321], + [-112.8515625,-29.535229562948444] + ]]}}]}` + + featureCollection := testJSON(t, featureCollectionJSON).(FeatureCollection) + poly := testJSON(t, polyFeatureJSON).(FeatureCollection) + + r1 := featureCollection.Within(poly) + r2 := featureCollection.Intersects(poly) + if r1 != r2 || !r1 { + t.Fatalf("expected %v/%v, got %v/%v", true, true, r1, r2) + } +} diff --git a/geojson/geometrycollection.go b/geojson/geometrycollection.go index ac01af2f..4aaba827 100644 --- a/geojson/geometrycollection.go +++ b/geojson/geometrycollection.go @@ -169,10 +169,10 @@ func (g GeometryCollection) IntersectsBBox(bbox BBox) bool { } for _, g := range g.Geometries { if g.IntersectsBBox(bbox) { - return false + return true } } - return true + return false } // Within detects if the object is fully contained inside another object. diff --git a/geojson/geometrycollection_test.go b/geojson/geometrycollection_test.go index c52d1ed5..71fe1f2e 100644 --- a/geojson/geometrycollection_test.go +++ b/geojson/geometrycollection_test.go @@ -66,3 +66,20 @@ func TestGeometryCollection(t *testing.T) { ] }`) } +func TestPointBoundingGeomColl(t *testing.T) { + geometryCollectionJSON := `{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[-75.1,56.283333]}]}` + polyFeatureJSON := `{"type": "GeometryCollection","geometries": [{"type": "Polygon","coordinates": [[ + [-112.8515625,-29.535229562948444], + [85.4296875,-2.535229562948444], + [85.4296875,65.36683689226321], + [-112.8515625,65.36683689226321], + [-112.8515625,-29.535229562948444] + ]]}]}` + geometryCollection := testJSON(t, geometryCollectionJSON).(GeometryCollection) + poly := testJSON(t, polyFeatureJSON).(GeometryCollection) + r1 := geometryCollection.Within(poly) + r2 := geometryCollection.Intersects(poly) + if r1 != r2 || !r1 { + t.Fatalf("expected %v/%v, got %v/%v", true, true, r1, r2) + } +}