Skip to content

Restructure relationships in street model #6605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: dev-2.x
Choose a base branch
from
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void mapAreaStopsInVertex(TestCase tc) {

graph.addVertex(fromVertex);
assertTrue(fromVertex.areaStops().isEmpty());
graph.index();

var mapper = new AreaStopsToVerticesMapper(graph, TRANSIT_MODEL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.opentripplanner.gtfs.graphbuilder.GtfsModule;
import org.opentripplanner.model.GenericLocation;
import org.opentripplanner.model.calendar.ServiceDateInterval;
import org.opentripplanner.model.modes.ExcludeAllTransitFilter;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.api.RoutingService;
import org.opentripplanner.routing.api.request.RouteRequest;
Expand Down Expand Up @@ -211,7 +210,7 @@ private static void addGtfsToGraph(
).buildGraph();

timetableRepository.index();
graph.index(timetableRepository.getSiteRepository());
graph.index();
}

private Itinerary getItinerary(GenericLocation from, GenericLocation to, int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static TestOtpModel buildFlexGraph(File file) {
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, true));
module.buildGraph();
timetableRepository.index();
graph.index(timetableRepository.getSiteRepository());
graph.index();
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, false));
assertTrue(timetableRepository.hasFlexTrips());
return new TestOtpModel(graph, timetableRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@ public void renderTile(TileRenderContext context) {
// Grow a bit the envelope to prevent rendering glitches between tiles
Envelope bboxWithMargins = context.expandPixels(lineWidth * 2.0, lineWidth * 2.0);

var streetIndex = context.graph.getStreetIndex();

Collection<Vertex> vertices = streetIndex
.getVerticesForEnvelope(bboxWithMargins)
Collection<Vertex> vertices = context.graph
.findVertices(bboxWithMargins)
.stream()
.sorted(evRenderer::vertexSorter)
.toList();

Collection<Edge> edges = streetIndex
.getEdgesForEnvelope(bboxWithMargins)
Collection<Edge> edges = context.graph
.findEdges(bboxWithMargins)
.stream()
.distinct()
.sorted(evRenderer::edgeSorter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.opentripplanner.framework.geometry.GeometryUtils;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.street.model.vertex.StreetVertex;
import org.opentripplanner.transit.model.site.AreaStop;
import org.opentripplanner.transit.service.TimetableRepository;
Expand Down Expand Up @@ -40,8 +39,6 @@ public void buildGraph() {
return;
}

StreetIndex streetIndex = graph.getStreetIndexSafe(timetableRepository.getSiteRepository());

ProgressTracker progress = ProgressTracker.track(
"Add flex locations to street vertices",
1,
Expand All @@ -54,7 +51,7 @@ public void buildGraph() {
.listAreaStops()
.parallelStream()
.flatMap(areaStop -> {
var matchedVertices = matchingVerticesForStop(streetIndex, areaStop);
var matchedVertices = matchingVerticesForStop(graph, areaStop);
// Keep lambda! A method-ref would cause incorrect class and line number to be logged
progress.step(m -> LOG.info(m));
return matchedVertices;
Expand All @@ -76,12 +73,9 @@ public void buildGraph() {
LOG.info(progress.completeMessage());
}

private static Stream<MatchResult> matchingVerticesForStop(
StreetIndex streetIndex,
AreaStop areaStop
) {
return streetIndex
.getVerticesForEnvelope(areaStop.getGeometry().getEnvelopeInternal())
private static Stream<MatchResult> matchingVerticesForStop(Graph graph, AreaStop areaStop) {
return graph
.findVertices(areaStop.getGeometry().getEnvelopeInternal())
.stream()
.filter(StreetVertex.class::isInstance)
.map(StreetVertex.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.opentripplanner.framework.i18n.LocalizedString;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.routing.linking.VertexLinker;
import org.opentripplanner.service.osminfo.OsmInfoGraphBuildService;
import org.opentripplanner.service.osminfo.model.Platform;
Expand All @@ -39,7 +38,6 @@
import org.opentripplanner.street.search.TraverseModeSet;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StationElement;
import org.opentripplanner.transit.service.TimetableRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,29 +66,25 @@ public class OsmBoardingLocationsModule implements GraphBuilderModule {
private final Graph graph;

private final OsmInfoGraphBuildService osmInfoGraphBuildService;
private final TimetableRepository timetableRepository;
private final VertexFactory vertexFactory;

private VertexLinker linker;

@Inject
public OsmBoardingLocationsModule(
Graph graph,
OsmInfoGraphBuildService osmInfoGraphBuildService,
TimetableRepository timetableRepository
OsmInfoGraphBuildService osmInfoGraphBuildService
) {
this.graph = graph;
this.osmInfoGraphBuildService = osmInfoGraphBuildService;
this.timetableRepository = timetableRepository;
this.vertexFactory = new VertexFactory(graph);
}

@Override
public void buildGraph() {
LOG.info("Improving boarding locations by checking OSM entities...");

StreetIndex streetIndex = graph.getStreetIndexSafe(timetableRepository.getSiteRepository());
this.linker = streetIndex.getVertexLinker();
this.linker = graph.getLinkerSafe();
int successes = 0;

for (TransitStopVertex ts : graph.getVerticesOfType(TransitStopVertex.class)) {
Expand All @@ -106,7 +100,7 @@ public void buildGraph() {
if (alreadyLinked) continue;
// only connect transit stops that are not part of a pathway network
if (!ts.hasPathways()) {
if (!connectVertexToStop(ts, streetIndex)) {
if (!connectVertexToStop(ts, graph)) {
LOG.debug("Could not connect {} at {}", ts.getStop().getCode(), ts.getCoordinate());
} else {
successes++;
Expand All @@ -116,7 +110,7 @@ public void buildGraph() {
LOG.info("Found {} OSM references which match a stop's id or code", successes);
}

private boolean connectVertexToStop(TransitStopVertex ts, StreetIndex index) {
private boolean connectVertexToStop(TransitStopVertex ts, Graph index) {
if (connectVertexToNode(ts, index)) return true;

if (connectVertexToWay(ts, index)) return true;
Expand All @@ -139,10 +133,10 @@ private Envelope getEnvelope(TransitStopVertex ts) {
*
* @return if the vertex has been connected
*/
private boolean connectVertexToArea(TransitStopVertex ts, StreetIndex index) {
private boolean connectVertexToArea(TransitStopVertex ts, Graph graph) {
RegularStop stop = ts.getStop();
var nearbyAreaGroups = index
.getEdgesForEnvelope(getEnvelope(ts))
var nearbyAreaGroups = graph
.findEdges(getEnvelope(ts))
.stream()
.filter(AreaEdge.class::isInstance)
.map(AreaEdge.class::cast)
Expand Down Expand Up @@ -180,11 +174,11 @@ private boolean connectVertexToArea(TransitStopVertex ts, StreetIndex index) {
*
* @return if the vertex has been connected
*/
private boolean connectVertexToWay(TransitStopVertex ts, StreetIndex index) {
private boolean connectVertexToWay(TransitStopVertex ts, Graph graph) {
var stop = ts.getStop();
var nearbyEdges = new HashMap<Platform, List<Edge>>();

for (var edge : index.getEdgesForEnvelope(getEnvelope(ts))) {
for (var edge : graph.findEdges(getEnvelope(ts))) {
osmInfoGraphBuildService
.findPlatform(edge)
.ifPresent(platform -> {
Expand Down Expand Up @@ -229,9 +223,9 @@ private boolean connectVertexToWay(TransitStopVertex ts, StreetIndex index) {
*
* @return If the vertex has been connected.
*/
private boolean connectVertexToNode(TransitStopVertex ts, StreetIndex index) {
var nearbyBoardingLocations = index
.getVerticesForEnvelope(getEnvelope(ts))
private boolean connectVertexToNode(TransitStopVertex ts, Graph graph) {
var nearbyBoardingLocations = graph
.findVertices(getEnvelope(ts))
.stream()
.filter(OsmBoardingLocationVertex.class::isInstance)
.map(OsmBoardingLocationVertex.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ public StreetLinkerModule(
@Override
public void buildGraph() {
timetableRepository.index();
graph.index(timetableRepository.getSiteRepository());
graph.getLinker().setAreaVisibility(this.areaVisibility);
graph.getLinker().setMaxAreaNodes(this.maxAreaNodes);
graph.getLinkerSafe().setAreaVisibility(this.areaVisibility);
graph.getLinkerSafe().setMaxAreaNodes(this.maxAreaNodes);

if (graph.hasStreets) {
linkTransitStops(graph, timetableRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.opentripplanner.graph_builder.module.StreetLinkerModule;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.routing.linking.VertexLinker;
import org.opentripplanner.street.model.StreetTraversalPermission;
import org.opentripplanner.street.model.edge.AreaEdge;
Expand Down Expand Up @@ -56,7 +55,6 @@ public class PruneIslands implements GraphBuilderModule {
private int adaptivePruningDistance;
private double adaptivePruningFactor;
private VertexLinker vertexLinker;
private StreetIndex streetIndex;

public PruneIslands(
Graph graph,
Expand All @@ -81,8 +79,7 @@ public void buildGraph() {
adaptivePruningDistance
);

this.vertexLinker = graph.getLinkerSafe(timetableRepository.getSiteRepository());
this.streetIndex = graph.getStreetIndexSafe(timetableRepository.getSiteRepository());
this.vertexLinker = graph.getLinkerSafe();

pruneIslands(TraverseMode.BICYCLE);
pruneIslands(TraverseMode.WALK);
Expand Down Expand Up @@ -254,7 +251,7 @@ private int processIslands(
// do not remove real islands which have only ferry stops
if (!onlyFerry && island.streetSize() < pruningThresholdWithStops * adaptivePruningFactor) {
double sizeCoeff = (adaptivePruningFactor > 1.0)
? island.distanceFromOtherGraph(streetIndex, adaptivePruningDistance) /
? island.distanceFromOtherGraph(graph, adaptivePruningDistance) /
adaptivePruningDistance
: 1.0;

Expand All @@ -269,7 +266,7 @@ private int processIslands(
//for islands without stops
if (island.streetSize() < pruningThresholdWithoutStops * adaptivePruningFactor) {
double sizeCoeff = (adaptivePruningFactor > 1.0)
? island.distanceFromOtherGraph(streetIndex, adaptivePruningDistance) /
? island.distanceFromOtherGraph(graph, adaptivePruningDistance) /
adaptivePruningDistance
: 1.0;
if (island.streetSize() * sizeCoeff < pruningThresholdWithoutStops) {
Expand Down Expand Up @@ -448,8 +445,6 @@ private boolean restrictOrRemove(
}
if (changed) {
if (permission == StreetTraversalPermission.NONE) {
// currently we must update spatial index manually, graph.removeEdge does not do that
vertexLinker.removePermanentEdgeFromIndex(pse);
graph.removeEdge(pse);
stats.put("removed", stats.get("removed") + 1);
removed++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.locationtech.jts.geom.Point;
import org.opentripplanner.framework.geometry.GeometryUtils;
import org.opentripplanner.framework.geometry.SphericalDistanceLibrary;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.street.model.vertex.OsmVertex;
import org.opentripplanner.street.model.vertex.TransitStopVertex;
import org.opentripplanner.street.model.vertex.Vertex;
Expand Down Expand Up @@ -88,7 +88,7 @@ Iterator<TransitStopVertex> stopIterator() {
// For speed reasons, graph geometry only within given search radius is considered.
// Distance is estimated using minimal vertex to vertex search instead of computing
// distances between graph edges. This is good enough for our heuristics.
double distanceFromOtherGraph(StreetIndex index, double searchRadius) {
double distanceFromOtherGraph(Graph graph, double searchRadius) {
Vertex v = getRepresentativeVertex();
double xscale = Math.cos((v.getCoordinate().y * Math.PI) / 180);
double searchRadiusDegrees = SphericalDistanceLibrary.metersToDegrees(searchRadius);
Expand All @@ -104,8 +104,8 @@ Iterator<TransitStopVertex> stopIterator() {
}
envelope.expandBy(searchRadiusDegrees / xscale, searchRadiusDegrees);

return index
.getVerticesForEnvelope(envelope)
return graph
.findVertices(envelope)
.stream()
.filter(vx -> !contains(vx))
.map(vx -> vertexDistanceFromSubgraph(vx, searchRadius))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.opentripplanner.graph_builder.module.osm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -14,7 +13,6 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.opentripplanner.astar.model.GraphPath;
Expand Down Expand Up @@ -46,7 +44,6 @@
import org.opentripplanner.street.model.vertex.IntersectionVertex;
import org.opentripplanner.street.model.vertex.OsmVertex;
import org.opentripplanner.street.model.vertex.Vertex;
import org.opentripplanner.street.model.vertex.VertexFactory;
import org.opentripplanner.street.search.StreetSearchBuilder;
import org.opentripplanner.street.search.state.State;
import org.opentripplanner.street.search.strategy.DominanceFunctions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
import org.opentripplanner.inspector.vector.LayerBuilder;
import org.opentripplanner.inspector.vector.LayerParameters;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.street.model.edge.Edge;

/**
* Selects all edges to be displayed for debugging.
*/
public class EdgeLayerBuilder extends LayerBuilder<Edge> {

private final StreetIndex streetIndex;
private final Graph graph;

public EdgeLayerBuilder(Graph graph, LayerParameters layerParameters) {
super(new EdgePropertyMapper(), layerParameters.name(), layerParameters.expansionFactor());
this.streetIndex = graph.getStreetIndex();
this.graph = graph;
}

@Override
protected List<Geometry> getGeometries(Envelope query) {
return streetIndex
.getEdgesForEnvelope(query)
return graph
.findEdges(query)
.stream()
.filter(e -> e.getGeometry() != null)
.map(edge -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.opentripplanner.inspector.vector.LayerBuilder;
import org.opentripplanner.inspector.vector.LayerParameters;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.service.vehiclerental.street.NoRestriction;
import org.opentripplanner.street.model.vertex.Vertex;
import org.opentripplanner.transit.model.site.AreaStop;
Expand All @@ -17,21 +16,21 @@
*/
public class GeofencingZonesLayerBuilder extends LayerBuilder<Vertex> {

private final StreetIndex streetIndex;
private final Graph graph;

public GeofencingZonesLayerBuilder(Graph graph, LayerParameters layerParameters) {
super(
new GeofencingZonesPropertyMapper(),
layerParameters.name(),
layerParameters.expansionFactor()
);
this.streetIndex = graph.getStreetIndex();
this.graph = graph;
}

@Override
protected List<Geometry> getGeometries(Envelope query) {
return streetIndex
.getVerticesForEnvelope(query)
return graph
.findVertices(query)
.stream()
.filter(se -> !(se.rentalRestrictions() instanceof NoRestriction))
.map(vertex -> {
Expand Down
Loading
Loading