kopia lustrzana https://github.com/onthegomap/planetiler
Add polygon index intersection utility (#866)
rodzic
ed373ff3d3
commit
978c64f0c6
|
@ -57,11 +57,29 @@ public class PolygonIndex<T> {
|
||||||
return postFilterContaining(point, items);
|
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) {
|
private List<T> postFilterContaining(Point point, List<?> items) {
|
||||||
List<T> result = new ArrayList<>(items.size());
|
List<T> result = new ArrayList<>(items.size());
|
||||||
for (Object item : items) {
|
for (Object item : items) {
|
||||||
if (item instanceof GeomWithData<?> value && value.poly.contains(point)) {
|
if (item instanceof GeomWithData<?>(var poly,var data) && poly.contains(point)) {
|
||||||
@SuppressWarnings("unchecked") T t = (T) value.data;
|
@SuppressWarnings("unchecked") T t = (T) data;
|
||||||
|
result.add(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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<?>(var poly,var data) && poly.intersects(geom)) {
|
||||||
|
@SuppressWarnings("unchecked") T t = (T) data;
|
||||||
result.add(t);
|
result.add(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,10 +21,13 @@ class PolygonIndexTest {
|
||||||
void testSingle() {
|
void testSingle() {
|
||||||
index.put(rectangle(0, 1), 1);
|
index.put(rectangle(0, 1), 1);
|
||||||
assertListsContainSameElements(List.of(1), index.getContaining(newPoint(0.5, 0.5)));
|
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(1), index.getContainingOrNearest(newPoint(0.5, 0.5)));
|
||||||
|
|
||||||
assertListsContainSameElements(List.of(), index.getContaining(newPoint(1.5, 1.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.getContainingOrNearest(newPoint(1.5, 1.5)));
|
||||||
|
assertListsContainSameElements(List.of(), index.getIntersecting(rectangle(2, 3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -33,6 +36,9 @@ class PolygonIndexTest {
|
||||||
index.put(rectangle(0, 1), 2);
|
index.put(rectangle(0, 1), 2);
|
||||||
assertListsContainSameElements(List.of(1, 2), index.getContaining(newPoint(0.5, 0.5)));
|
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.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
|
@Test
|
||||||
|
|
Ładowanie…
Reference in New Issue