planetiler/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java

128 wiersze
4.0 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.geo;
2021-07-14 09:31:54 +00:00
import java.util.ArrayList;
import java.util.List;
2021-08-11 12:40:49 +00:00
import javax.annotation.concurrent.ThreadSafe;
2021-07-14 09:31:54 +00:00
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.index.strtree.STRtree;
2021-09-10 00:46:20 +00:00
/**
* Index to efficiently query which polygons contain a point.
* <p>
* Writes and reads are thread-safe, but all writes must occur before reads.
*
* @param <T> the type of value associated with each polygon
*/
2021-08-11 12:40:49 +00:00
@ThreadSafe
2021-07-14 09:31:54 +00:00
public class PolygonIndex<T> {
private record GeomWithData<T> (Polygon poly, T data) {}
2021-07-14 09:31:54 +00:00
private final STRtree index = new STRtree();
private PolygonIndex() {}
2021-07-14 09:31:54 +00:00
public static <T> PolygonIndex<T> create() {
return new PolygonIndex<>();
}
2021-07-27 12:38:43 +00:00
private volatile boolean built = false;
private void build() {
if (!built) {
synchronized (this) {
if (!built) {
index.build();
built = true;
}
}
}
}
2021-09-10 00:46:20 +00:00
/** Returns the data associated with the first polygon containing {@code point}. */
2021-07-14 11:13:30 +00:00
public T getOnlyContaining(Point point) {
List<T> result = getContaining(point);
return result.isEmpty() ? null : result.get(0);
2021-07-14 09:31:54 +00:00
}
2021-09-10 00:46:20 +00:00
/** Returns the data associated with all polygons containing {@code point}. */
public List<T> getContaining(Point point) {
build();
// first pre-filter polygons with envelope that overlaps this point
List<?> items = index.query(point.getEnvelopeInternal());
// then post-filter to only polygons that actually contain the point
return postFilterContaining(point, items);
}
2021-09-10 00:46:20 +00:00
private List<T> postFilterContaining(Point point, List<?> items) {
2021-07-14 09:31:54 +00:00
List<T> result = new ArrayList<>(items.size());
for (Object item : items) {
if (item instanceof GeomWithData<?> value && value.poly.contains(point)) {
2021-07-14 09:31:54 +00:00
@SuppressWarnings("unchecked") T t = (T) value.data;
result.add(t);
}
}
return result;
}
2021-09-10 00:46:20 +00:00
/**
* 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.
*/
2021-07-14 11:13:30 +00:00
public List<T> getContainingOrNearest(Point point) {
2021-07-27 12:38:43 +00:00
build();
2021-07-14 11:13:30 +00:00
List<?> items = index.query(point.getEnvelopeInternal());
2021-07-14 09:31:54 +00:00
// optimization: if there's only one then skip checking contains/distance
if (items.size() == 1) {
if (items.get(0)instanceof GeomWithData<?> value) {
2021-07-14 09:31:54 +00:00
@SuppressWarnings("unchecked") T t = (T) value.data;
return List.of(t);
}
}
2021-09-10 00:46:20 +00:00
List<T> result = postFilterContaining(point, items);
// if none contain, then look for the nearest polygon from potential overlaps
2021-07-14 09:31:54 +00:00
if (result.isEmpty()) {
double nearest = Double.MAX_VALUE;
T nearestValue = null;
for (Object item : items) {
if (item instanceof GeomWithData<?> value) {
2021-07-14 11:13:30 +00:00
double distance = value.poly.distance(point);
2021-07-14 09:31:54 +00:00
if (distance < nearest) {
@SuppressWarnings("unchecked") T t = (T) value.data;
nearestValue = t;
nearest = distance;
}
}
}
if (nearestValue != null) {
result.add(nearestValue);
}
}
return result;
}
2021-09-10 00:46:20 +00:00
/** Returns the data associated with a polygon that contains {@code point} or nearest polygon if none are found. */
public T get(Point point) {
List<T> nearests = getContainingOrNearest(point);
2021-07-14 09:31:54 +00:00
return nearests.isEmpty() ? null : nearests.get(0);
}
2021-09-10 00:46:20 +00:00
/** Indexes {@code item} for all polygons contained in {@code geom}. */
2021-08-11 12:40:49 +00:00
public void put(Geometry geom, T item) {
2021-07-14 09:31:54 +00:00
if (geom instanceof Polygon poly) {
2021-07-27 12:38:43 +00:00
// need to externally synchronize inserts into the STRTree
synchronized (this) {
index.insert(poly.getEnvelopeInternal(), new GeomWithData<>(poly, item));
}
2021-07-14 09:31:54 +00:00
} else if (geom instanceof GeometryCollection geoms) {
for (int i = 0; i < geoms.getNumGeometries(); i++) {
put(geoms.getGeometryN(i), item);
}
}
}
}