Skip to content

Commit 9516d05

Browse files
committed
perf(street): use PreparedGeometry instead of RelateNG for area containment
The containsSegment check in PreparedAreaGroup used RelateNG to prepare and reuse a spatial index over the area-group polygon. Benchmarks (see PR #7715 discussion) show the classic PreparedGeometry API is faster than RelateNG for this contains predicate, and the sibling areasCrossedBy crossing test already uses PreparedGeometry. Switch containsSegment to PreparedGeometry.contains for consistency and performance. This restores the pre-#7696 semantics of plain Polygon.contains (which can throw TopologyException on invalid polygons) but keeps the prepared-index reuse that #7696 introduced.
1 parent 2e891ca commit 9516d05

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

street/src/main/java/org/opentripplanner/street/linking/PreparedAreaGroup.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import org.locationtech.jts.geom.LineString;
88
import org.locationtech.jts.geom.prep.PreparedGeometry;
99
import org.locationtech.jts.geom.prep.PreparedGeometryFactory;
10-
import org.locationtech.jts.operation.relateng.RelateNG;
11-
import org.locationtech.jts.operation.relateng.RelatePredicate;
1210
import org.opentripplanner.street.geometry.GeometryUtils;
1311
import org.opentripplanner.street.model.edge.Area;
1412
import org.opentripplanner.street.model.edge.AreaGroup;
@@ -36,7 +34,7 @@ final class PreparedAreaGroup {
3634
private static final double AREA_INTERSECTION_SHRINKING = 0.0001;
3735

3836
private final AreaGroup areaGroup;
39-
private RelateNG prepared;
37+
private PreparedGeometry preparedGroup;
4038
private PreparedGeometry[] preparedAreas;
4139

4240
PreparedAreaGroup(AreaGroup areaGroup) {
@@ -52,14 +50,14 @@ AreaGroup areaGroup() {
5250
* shrunk slightly at both ends first, so that a segment connecting two boundary points is not
5351
* rejected by floating point imprecision at the boundary. Reuses a cached prepared index across
5452
* calls. A plain {@code Polygon.contains(...)} would rebuild a sweep-line index over the polygon
55-
* boundary on every call; {@link RelateNG} builds it once and is also tolerant of invalid /
56-
* self-intersecting OSM polygons (it does not throw {@code TopologyException}).
53+
* boundary on every call; a {@link PreparedGeometry} builds it once and reuses it, matching the
54+
* indexed {@link #areasCrossedBy} crossing test on this same group.
5755
*/
5856
boolean containsSegment(Coordinate from, Coordinate to) {
59-
if (prepared == null) {
60-
prepared = RelateNG.prepare(areaGroup.getGeometry());
57+
if (preparedGroup == null) {
58+
preparedGroup = PreparedGeometryFactory.prepare(areaGroup.getGeometry());
6159
}
62-
return prepared.evaluate(createShrunkLine(from, to), RelatePredicate.contains());
60+
return preparedGroup.contains(createShrunkLine(from, to));
6361
}
6462

6563
/**

0 commit comments

Comments
 (0)