Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ClosestTripTest {
private static final FlexAccessEgressCallbackAdapter ADAPTER =
new FlexAccessEgressCallbackAdapter() {
@Override
public TransitStopVertex getStopVertexForStopId(FeedScopedId id) {
public TransitStopVertex getStopVertex(FeedScopedId id) {
return null;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ void doNotFilter() {

var trips = closestTrips(matcher);
assertThat(trips).hasSize(1);
assertEquals(List.copyOf(trips).getFirst().flexTrip(), FLEX_TRIP);
assertEquals(FLEX_TRIP, List.copyOf(trips).getFirst().flexTrip());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ private List<FlexServiceDate> createFlexServiceDates(
private class CallbackAdapter implements FlexAccessEgressCallbackAdapter {

@Override
public TransitStopVertex getStopVertexForStopId(FeedScopedId stopId) {
return graph.getStopVertexForStopId(stopId);
public TransitStopVertex getStopVertex(FeedScopedId stopId) {
return graph.getStopVertex(stopId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ StopLocation getAccessEgressStop() {
*/
Stream<FlexAccessEgress> createFlexAccessEgressStream(FlexAccessEgressCallbackAdapter callback) {
if (transferStop instanceof RegularStop stop) {
var flexVertex = callback.getStopVertexForStopId(stop.getId());
var flexVertex = callback.getStopVertex(stop.getId());
return Stream.of(createFlexAccessEgress(new ArrayList<>(), flexVertex, stop)).filter(
Objects::nonNull
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public interface FlexAccessEgressCallbackAdapter {
/** Adapter, look at implementing service for documentation. */
TransitStopVertex getStopVertexForStopId(FeedScopedId id);
TransitStopVertex getStopVertex(FeedScopedId id);

/** Adapter, look at implementing service for documentation. */
Collection<PathTransfer> getTransfersFromStop(StopLocation stop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ private TemporaryVerticesContainer createTemporaryVerticesContainer(
return new TemporaryVerticesContainer(
serverContext.graph(),
serverContext.vertexLinker(),
serverContext.transitService()::findStopOrChildIds,
request.from(),
request.to(),
request.journey().access().mode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static List<Itinerary> route(
var temporaryVertices = new TemporaryVerticesContainer(
serverContext.graph(),
serverContext.vertexLinker(),
serverContext.transitService()::findStopOrChildIds,
request.from(),
request.to(),
request.journey().direct().mode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static List<Itinerary> route(OtpServerRequestContext serverContext, Route
var temporaryVertices = new TemporaryVerticesContainer(
serverContext.graph(),
serverContext.vertexLinker(),
serverContext.transitService()::findStopOrChildIds,
request.from(),
request.to(),
request.journey().direct().mode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -207,19 +208,30 @@ public <T extends Vertex> List<T> getVerticesOfType(Class<T> cls) {
* Return the vertex corresponding to the stop id, or null.
*/
@Nullable
public TransitStopVertex getStopVertexForStopId(FeedScopedId id) {
public TransitStopVertex getStopVertex(FeedScopedId id) {
requireIndex();
return streetIndex.findTransitStopVertex(id);
return streetIndex.getStopVertex(id);
}

/**
* If the {@code id} is a stop id return a set with a single element.
* If it is a station id return a set containing all child stop vertices, or an empty
* set otherwise.
* If the {@code id} is a stop id return the corresponding vertex, otherwise return an empty
* optional.
*/
public Set<TransitStopVertex> findStopOrChildStopsVertices(FeedScopedId stopId) {
public Optional<TransitStopVertex> findStopVertex(FeedScopedId stopId) {
requireIndex();
return streetIndex.getStopOrChildStopsVertices(stopId);
return streetIndex.findStopVertex(stopId);
}

/**
* Get the vertex for a site id. If the id corresponds to a regular stop it will return the
* vertex for the stop.
* If the id corresponds to a station and the station is configured to route to centroid
* then that vertex will be returned.
* Otherwise, an empty optional will be returned.
*/
public Optional<Vertex> findStopOrCentroidVertex(FeedScopedId stopId) {
requireIndex();
return streetIndex.findStopOrCentroidVertex(stopId);
}

/**
Expand Down Expand Up @@ -319,18 +331,7 @@ public OpeningHoursCalendarService getOpeningHoursCalendarService() {
*/
public Collection<Vertex> findVertices(Envelope env) {
requireIndex();
return streetIndex.getVerticesForEnvelope(env);
}

/**
* Get the street vertices for an id. If the id corresponds to a regular stop we will return the
* coordinate for the stop.
* If the id corresponds to a station we will either return the coordinates of the child stops or
* the station centroid if the station is configured to route to centroid.
*/
public Set<Vertex> findStopVertices(FeedScopedId stopId) {
requireIndex();
return streetIndex.findStopVertices(stopId);
return streetIndex.findVertices(env);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.opentripplanner.routing.graph;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.locationtech.jts.geom.Coordinate;
Expand Down Expand Up @@ -39,7 +35,6 @@ class StreetIndex {
private static final Logger LOG = LoggerFactory.getLogger(StreetIndex.class);

private final Map<FeedScopedId, TransitStopVertex> stopVerticesById;
private final ImmutableSetMultimap<FeedScopedId, TransitStopVertex> stopVerticesByParentId;

/**
* This list contains transitStationVertices for the stations that are configured to route to centroid
Expand All @@ -55,31 +50,51 @@ class StreetIndex {
StreetIndex(Graph graph) {
this.edgeIndex = new EdgeSpatialIndex();
this.vertexIndex = new HashGridSpatialIndex<>();
var stopVertices = graph.getVerticesOfType(TransitStopVertex.class);
this.stopVerticesById = indexStopIds(stopVertices);
this.stopVerticesByParentId = indexStationIds(stopVertices);
this.stopVerticesById = indexStopIds(graph);

this.stationCentroidVertices = indexStationCentroids(graph);
postSetup(graph.getVertices());
}

/**
* @see Graph#getStopVertex(FeedScopedId)
*/
@Nullable
TransitStopVertex findTransitStopVertex(FeedScopedId stopId) {
TransitStopVertex getStopVertex(FeedScopedId stopId) {
return stopVerticesById.get(stopId);
}

/**
* @see Graph#findStopVertex(FeedScopedId) (FeedScopedId)
*/
Optional<TransitStopVertex> findStopVertex(FeedScopedId id) {
return Optional.ofNullable(stopVerticesById.get(id));
}

/**
* @see Graph#findStopOrCentroidVertex(FeedScopedId)
*/
Optional<Vertex> findStopOrCentroidVertex(FeedScopedId id) {
var stationVertex = stationCentroidVertices.get(id);
if (stationVertex != null) {
return Optional.of(stationVertex);
}
return findStopVertex(id).map(Vertex.class::cast);
}

/**
* Returns the vertices intersecting with the specified envelope.
*/
List<Vertex> getVerticesForEnvelope(Envelope envelope) {
List<Vertex> findVertices(Envelope envelope) {
List<Vertex> vertices = vertexIndex.query(envelope);
// Here we assume vertices list modifiable
vertices.removeIf(v -> !envelope.contains(new Coordinate(v.getLon(), v.getLat())));
return vertices;
}

/**
* Return the edges whose geometry intersect with the specified envelope. Warning: edges disconnected from the graph
* Return the edges whose geometry intersect with the specified envelope.
* Warning: edges disconnected from the graph
* will not be indexed.
*/
Collection<Edge> findEdges(Envelope envelope) {
Expand All @@ -93,42 +108,6 @@ Collection<Edge> findEdges(Envelope envelope) {
.toList();
}

@Override
public String toString() {
return (
getClass().getName() +
" -- edgeTree: " +
edgeIndex.toString() +
" -- verticesTree: " +
vertexIndex.toString()
);
}

/**
* @param id Id of Stop, Station, MultiModalStation or GroupOfStations
* @return The associated TransitStopVertex or all underlying TransitStopVertices
*/
Set<TransitStopVertex> getStopOrChildStopsVertices(FeedScopedId id) {
if (stopVerticesById.containsKey(id)) {
return Set.of(stopVerticesById.get(id));
} else if (stopVerticesByParentId.containsKey(id)) {
return stopVerticesByParentId.get(id);
} else {
return Set.of();
}
}

/**
* @see Graph#findStopVertices(FeedScopedId)
*/
Set<Vertex> findStopVertices(FeedScopedId id) {
var stationVertex = stationCentroidVertices.get(id);
if (stationVertex != null) {
return Set.of(stationVertex);
}
return Collections.unmodifiableSet(getStopOrChildStopsVertices(id));
}

Collection<Edge> findEdges(Envelope env, Scope scope) {
return edgeIndex.query(env, scope).toList();
}
Expand All @@ -148,6 +127,8 @@ void remove(Vertex vertex) {
vertexIndex.remove(new Envelope(vertex.getCoordinate()), vertex);
}

// private methods

private static LineString edgeGeometryOrStraightLine(Edge e) {
LineString geometry = e.getGeometry();
if (geometry == null) {
Expand Down Expand Up @@ -182,29 +163,15 @@ private void postSetup(Collection<Vertex> vertices) {
LOG.info(progress.completeMessage());
}

private static Map<FeedScopedId, TransitStopVertex> indexStopIds(
Collection<TransitStopVertex> vertices
) {
private static Map<FeedScopedId, TransitStopVertex> indexStopIds(Graph graph) {
var vertices = graph.getVerticesOfType(TransitStopVertex.class);
var map = new HashMap<FeedScopedId, TransitStopVertex>();
for (TransitStopVertex it : vertices) {
map.put(it.getStop().getId(), it);
}
return Map.copyOf(map);
}

private static ImmutableSetMultimap<FeedScopedId, TransitStopVertex> indexStationIds(
Collection<TransitStopVertex> vertices
) {
Multimap<FeedScopedId, TransitStopVertex> map = ArrayListMultimap.create();
vertices
.stream()
.filter(v -> v.getStop().isPartOfStation())
.forEach(v -> {
map.put(v.getStop().getParentStation().getId(), v);
});
return ImmutableSetMultimap.copyOf(map);
}

private static Map<FeedScopedId, StationCentroidVertex> indexStationCentroids(Graph graph) {
return graph
.getVerticesOfType(StationCentroidVertex.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import org.locationtech.jts.geom.Coordinate;
import org.opentripplanner.astar.spi.SkipEdgeStrategy;
import org.opentripplanner.astar.spi.TraverseVisitor;
Expand Down Expand Up @@ -101,6 +102,7 @@ private void findClosestUsingStreets(
var temporaryVertices = new TemporaryVerticesContainer(
graph,
linker,
id -> Set.of(),
GenericLocation.fromCoordinate(lat, lon),
GenericLocation.UNKNOWN,
StreetMode.WALK,
Expand Down
Loading
Loading