From a3d0f9e96108177437af0ed121d591cdc60a7c10 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Wed, 10 Apr 2024 06:27:00 -0400 Subject: [PATCH 1/2] add polygon index intersection utility --- .../planetiler/geo/PolygonIndex.java | 18 ++++++++++++++++++ .../planetiler/geo/PolygonIndexTest.java | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java index 0d06d9039b..46f7e5ae85 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java @@ -57,6 +57,13 @@ public List getContaining(Point point) { return postFilterContaining(point, items); } + /** Returns the data associated with all polygons containing {@code point}. */ + public List getIntersecting(Geometry geom) { + build(); + List items = index.query(geom.getEnvelopeInternal()); + return postFilterIntersecting(geom, items); + } + private List postFilterContaining(Point point, List items) { List result = new ArrayList<>(items.size()); for (Object item : items) { @@ -68,6 +75,17 @@ private List postFilterContaining(Point point, List items) { return result; } + private List postFilterIntersecting(Geometry geom, List items) { + List result = new ArrayList<>(items.size()); + for (Object item : items) { + if (item instanceof GeomWithData value && value.poly.intersects(geom)) { + @SuppressWarnings("unchecked") T t = (T) value.data; + result.add(t); + } + } + return result; + } + /** * Returns the data associated with either the polygons that contain {@code point} or if none are found than the * nearest polygon to {@code point} with an envelope that contains point. diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/PolygonIndexTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/PolygonIndexTest.java index a959994b77..27ace2ec07 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/PolygonIndexTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/PolygonIndexTest.java @@ -21,10 +21,13 @@ void testEmpty() { void testSingle() { index.put(rectangle(0, 1), 1); assertListsContainSameElements(List.of(1), index.getContaining(newPoint(0.5, 0.5))); + assertListsContainSameElements(List.of(1), index.getIntersecting(newPoint(0.5, 0.5))); + assertListsContainSameElements(List.of(1), index.getIntersecting(rectangle(1, 2))); assertListsContainSameElements(List.of(1), index.getContainingOrNearest(newPoint(0.5, 0.5))); assertListsContainSameElements(List.of(), index.getContaining(newPoint(1.5, 1.5))); assertListsContainSameElements(List.of(), index.getContainingOrNearest(newPoint(1.5, 1.5))); + assertListsContainSameElements(List.of(), index.getIntersecting(rectangle(2, 3))); } @Test @@ -33,6 +36,9 @@ void testMultipleIdentical() { index.put(rectangle(0, 1), 2); assertListsContainSameElements(List.of(1, 2), index.getContaining(newPoint(0.5, 0.5))); assertListsContainSameElements(List.of(1, 2), index.getContainingOrNearest(newPoint(0.5, 0.5))); + assertListsContainSameElements(List.of(1, 2), index.getIntersecting(rectangle(0.5, 1.5))); + assertListsContainSameElements(List.of(1, 2), index.getIntersecting(rectangle(1, 2))); + assertListsContainSameElements(List.of(), index.getIntersecting(rectangle(2, 3))); } @Test From 026f3e4f0a67f4c61ff9286afcf5bd0f9d497b16 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Wed, 10 Apr 2024 06:48:37 -0400 Subject: [PATCH 2/2] record pattern --- .../java/com/onthegomap/planetiler/geo/PolygonIndex.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java index 46f7e5ae85..8f79dff5f6 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java @@ -67,8 +67,8 @@ public List getIntersecting(Geometry geom) { private List postFilterContaining(Point point, List items) { List result = new ArrayList<>(items.size()); for (Object item : items) { - if (item instanceof GeomWithData value && value.poly.contains(point)) { - @SuppressWarnings("unchecked") T t = (T) value.data; + if (item instanceof GeomWithData(var poly,var data) && poly.contains(point)) { + @SuppressWarnings("unchecked") T t = (T) data; result.add(t); } } @@ -78,8 +78,8 @@ private List postFilterContaining(Point point, List items) { private List postFilterIntersecting(Geometry geom, List items) { List result = new ArrayList<>(items.size()); for (Object item : items) { - if (item instanceof GeomWithData value && value.poly.intersects(geom)) { - @SuppressWarnings("unchecked") T t = (T) value.data; + if (item instanceof GeomWithData(var poly,var data) && poly.intersects(geom)) { + @SuppressWarnings("unchecked") T t = (T) data; result.add(t); } }