Skip to content

Commit

Permalink
add polygon index intersection utility
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Apr 10, 2024
1 parent ed373ff commit a3d0f9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public List<T> getContaining(Point point) {
return postFilterContaining(point, items);
}

/** Returns the data associated with all polygons containing {@code point}. */
public List<T> getIntersecting(Geometry geom) {
build();
List<?> items = index.query(geom.getEnvelopeInternal());
return postFilterIntersecting(geom, items);
}

private List<T> postFilterContaining(Point point, List<?> items) {
List<T> result = new ArrayList<>(items.size());
for (Object item : items) {
Expand All @@ -68,6 +75,17 @@ private List<T> postFilterContaining(Point point, List<?> items) {
return result;
}

private List<T> postFilterIntersecting(Geometry geom, List<?> items) {
List<T> result = new ArrayList<>(items.size());
for (Object item : items) {
if (item instanceof GeomWithData<?> value && value.poly.intersects(geom)) {

Check warning on line 81 in planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java

View workflow job for this annotation

GitHub Actions / Analyze with Sonar

MINOR CODE_SMELL

Use the record pattern instead of this pattern match variable. rule: java:S6878 (https://sonarcloud.io/organizations/onthegomap/rules?open=java%3AS6878&rule_key=java%3AS6878) issue url: https://sonarcloud.io/project/issues?pullRequest=866&open=AY7Hk7tQf6bcHrENyH99&id=onthegomap_planetiler
@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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a3d0f9e

Please sign in to comment.