Skip to content

Commit e17cac7

Browse files
Make methods in graph simpler
1 parent 1d37651 commit e17cac7

File tree

5 files changed

+67
-48
lines changed

5 files changed

+67
-48
lines changed

application/src/main/java/org/opentripplanner/routing/graph/Graph.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.opentripplanner.routing.services.notes.StreetNotesService;
2323
import org.opentripplanner.street.model.edge.Edge;
2424
import org.opentripplanner.street.model.edge.StreetEdge;
25+
import org.opentripplanner.street.model.vertex.StationCentroidVertex;
2526
import org.opentripplanner.street.model.vertex.TransitStopVertex;
2627
import org.opentripplanner.street.model.vertex.Vertex;
2728
import org.opentripplanner.street.model.vertex.VertexLabel;
@@ -210,7 +211,7 @@ public <T extends Vertex> List<T> getVerticesOfType(Class<T> cls) {
210211
@Nullable
211212
public TransitStopVertex getStopVertex(FeedScopedId id) {
212213
requireIndex();
213-
return streetIndex.getStopVertex(id);
214+
return streetIndex.findStopVertex(id).orElse(null);
214215
}
215216

216217
/**
@@ -223,15 +224,12 @@ public Optional<TransitStopVertex> findStopVertex(FeedScopedId stopId) {
223224
}
224225

225226
/**
226-
* Get the vertex for a site id. If the id corresponds to a regular stop it will return the
227-
* vertex for the stop.
228-
* If the id corresponds to a station and the station is configured to route to centroid
229-
* then that vertex will be returned.
230-
* Otherwise, an empty optional will be returned.
227+
* If the {@code id} is a station id and it is configured to route to its center,
228+
* return the corresponding vertex, otherwise return an empty optional.
231229
*/
232-
public Optional<Vertex> findStopOrCentroidVertex(FeedScopedId stopId) {
230+
public Optional<StationCentroidVertex> findStationCentroidVertex(FeedScopedId stopId) {
233231
requireIndex();
234-
return streetIndex.findStopOrCentroidVertex(stopId);
232+
return streetIndex.findStationCentroidVertex(stopId);
235233
}
236234

237235
/**

application/src/main/java/org/opentripplanner/routing/graph/StreetIndex.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Map;
77
import java.util.Optional;
88
import java.util.stream.Collectors;
9-
import javax.annotation.Nullable;
109
import org.locationtech.jts.geom.Coordinate;
1110
import org.locationtech.jts.geom.Envelope;
1211
import org.locationtech.jts.geom.LineString;
@@ -34,7 +33,7 @@ class StreetIndex {
3433

3534
private static final Logger LOG = LoggerFactory.getLogger(StreetIndex.class);
3635

37-
private final Map<FeedScopedId, TransitStopVertex> stopVerticesById;
36+
private final Map<FeedScopedId, TransitStopVertex> stopVertices;
3837

3938
/**
4039
* This list contains transitStationVertices for the stations that are configured to route to centroid
@@ -50,36 +49,24 @@ class StreetIndex {
5049
StreetIndex(Graph graph) {
5150
this.edgeIndex = new EdgeSpatialIndex();
5251
this.vertexIndex = new HashGridSpatialIndex<>();
53-
this.stopVerticesById = indexStopIds(graph);
52+
this.stopVertices = indexStopIds(graph);
5453

5554
this.stationCentroidVertices = indexStationCentroids(graph);
5655
postSetup(graph.getVertices());
5756
}
5857

59-
/**
60-
* @see Graph#getStopVertex(FeedScopedId)
61-
*/
62-
@Nullable
63-
TransitStopVertex getStopVertex(FeedScopedId stopId) {
64-
return stopVerticesById.get(stopId);
65-
}
66-
6758
/**
6859
* @see Graph#findStopVertex(FeedScopedId) (FeedScopedId)
6960
*/
7061
Optional<TransitStopVertex> findStopVertex(FeedScopedId id) {
71-
return Optional.ofNullable(stopVerticesById.get(id));
62+
return Optional.ofNullable(stopVertices.get(id));
7263
}
7364

7465
/**
75-
* @see Graph#findStopOrCentroidVertex(FeedScopedId)
66+
* @see Graph#findStationCentroidVertex(FeedScopedId)
7667
*/
77-
Optional<Vertex> findStopOrCentroidVertex(FeedScopedId id) {
78-
var stationVertex = stationCentroidVertices.get(id);
79-
if (stationVertex != null) {
80-
return Optional.of(stationVertex);
81-
}
82-
return findStopVertex(id).map(Vertex.class::cast);
68+
Optional<StationCentroidVertex> findStationCentroidVertex(FeedScopedId id) {
69+
return Optional.ofNullable(stationCentroidVertices.get(id));
8370
}
8471

8572
/**

application/src/main/java/org/opentripplanner/street/search/TemporaryVerticesContainer.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,25 @@ private Set<Vertex> getStreetVerticesForLocation(
174174
}
175175
} else {
176176
if (location.stopId != null) {
177-
// check if stop or station centroid is found
178-
// station centroids are a special vertex to indicate that you don't want to route
179-
// to/from the child stops because they are too spread out.
180-
var stopVertices = graph.findStopOrCentroidVertex(location.stopId);
181-
if (stopVertices.isPresent()) {
182-
return Set.of(stopVertices.get());
183-
} else {
184-
// in the regular case you want to resolve a (multi-modal) station into its child stops
185-
var vertices = findStopOrChildStopVertices(location.stopId);
186-
if (!vertices.isEmpty()) {
187-
return vertices
188-
.stream()
189-
.map(Vertex.class::cast)
190-
.collect(Collectors.toUnmodifiableSet());
191-
}
177+
// check if there is a stop by the given id
178+
var stopVertex = graph.findStopVertex(location.stopId);
179+
if (stopVertex.isPresent()) {
180+
return Set.of(stopVertex.get());
181+
}
182+
183+
// station centroids may be used instead of child stop vertices for stations
184+
var centroidVertex = graph.findStationCentroidVertex(location.stopId);
185+
if (centroidVertex.isPresent()) {
186+
return Set.of(centroidVertex.get());
187+
}
188+
189+
// in the regular case you want to resolve a (multi-modal) station into its child stops
190+
var childVertices = findStopOrChildStopVertices(location.stopId);
191+
if (!childVertices.isEmpty()) {
192+
return childVertices
193+
.stream()
194+
.map(Vertex.class::cast)
195+
.collect(Collectors.toUnmodifiableSet());
192196
}
193197
}
194198
}

application/src/test/java/org/opentripplanner/routing/graph/StreetIndexTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ class StreetIndexTest {
2626
@Test
2727
void stopId() {
2828
var streetIndex = buildIndex();
29-
assertEquals(stopVertex, streetIndex.getStopVertex(stop.getId()));
3029
assertThat(streetIndex.findStopVertex(stop.getId())).hasValue(stopVertex);
3130
}
3231

3332
@Test
3433
void nonExistentId() {
3534
var streetIndex = buildIndex();
36-
assertNull(streetIndex.getStopVertex(id("non-existent-stop-id")));
3735
assertThat(streetIndex.findStopVertex(id("non-existent-stop-id"))).isEmpty();
3836
}
3937

4038
@Test
4139
void stationCentroid() {
4240
var streetIndex = buildIndex();
43-
assertNull(streetIndex.getStopVertex(station.getId()));
44-
assertThat(streetIndex.findStopOrCentroidVertex(station.getId())).hasValue(centroidVertex);
41+
assertThat(streetIndex.findStationCentroidVertex(station.getId())).hasValue(centroidVertex);
4542
}
4643

4744
private StreetIndex buildIndex() {

application/src/test/java/org/opentripplanner/street/search/TemporaryVerticesContainerTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,32 @@
1616
import org.opentripplanner.model.GenericLocation;
1717
import org.opentripplanner.routing.graph.Graph;
1818
import org.opentripplanner.street.model._data.StreetModelForTest;
19+
import org.opentripplanner.street.model.edge.StreetStationCentroidLink;
20+
import org.opentripplanner.street.model.vertex.StationCentroidVertex;
1921
import org.opentripplanner.street.model.vertex.TransitStopVertex;
2022
import org.opentripplanner.street.model.vertex.Vertex;
2123
import org.opentripplanner.transit.model._data.TimetableRepositoryForTest;
2224
import org.opentripplanner.transit.model.framework.FeedScopedId;
2325
import org.opentripplanner.transit.model.site.RegularStop;
26+
import org.opentripplanner.transit.model.site.Station;
2427

2528
class TemporaryVerticesContainerTest {
2629

2730
private static final WgsCoordinate CENTER = new WgsCoordinate(0, 0);
2831
private static final int DISTANCE = 20;
2932

33+
private static final FeedScopedId ALPHA_ID = id("alpha");
3034
private static final FeedScopedId OMEGA_ID = id("omega");
3135

3236
private final TimetableRepositoryForTest testModel = TimetableRepositoryForTest.of();
37+
38+
private final Station stationAlpha = testModel
39+
.station("alpha")
40+
.withId(ALPHA_ID)
41+
.withCoordinate(CENTER)
42+
.withShouldRouteToCentroid(true)
43+
.build();
44+
3345
private final RegularStop stopA = testModel
3446
.stop("A")
3547
.withCoordinate(CENTER.moveEastMeters(DISTANCE))
@@ -46,7 +58,7 @@ class TemporaryVerticesContainerTest {
4658
.stop("D")
4759
.withCoordinate(CENTER.moveNorthMeters(DISTANCE))
4860
.build();
49-
private final Graph graph = buildGraph(stopA, stopB, stopC, stopD);
61+
private final Graph graph = buildGraph(stationAlpha, stopA, stopB, stopC, stopD);
5062

5163
@Test
5264
void coordinates() {
@@ -102,10 +114,31 @@ void stationId() {
102114
assertThat(toStops(container.getFromStopVertices())).containsExactly(stopC, stopD);
103115
}
104116

105-
private static Graph buildGraph(RegularStop... stops) {
117+
@Test
118+
void centroid() {
119+
var container = new TemporaryVerticesContainer(
120+
graph,
121+
TestVertexLinker.of(graph),
122+
Set::of,
123+
GenericLocation.fromStopId("station", ALPHA_ID.getFeedId(), ALPHA_ID.getId()),
124+
stopToLocation(stopB),
125+
WALK,
126+
WALK
127+
);
128+
var fromVertices = List.copyOf(container.getFromVertices());
129+
assertThat(fromVertices).hasSize(1);
130+
131+
var station = ((StationCentroidVertex) fromVertices.getFirst()).getStation();
132+
assertEquals(station, this.stationAlpha);
133+
}
134+
135+
private static Graph buildGraph(Station station, RegularStop... stops) {
106136
var graph = new Graph();
107137
var center = StreetModelForTest.intersectionVertex(CENTER.asJtsCoordinate());
108138
graph.addVertex(center);
139+
var centroidVertex = new StationCentroidVertex(station);
140+
graph.addVertex(centroidVertex);
141+
StreetStationCentroidLink.createStreetStationLink(centroidVertex, center);
109142
Arrays.stream(stops).forEach(s -> {
110143
graph.addVertex(TransitStopVertex.of().withStop(s).build());
111144
var vertex = StreetModelForTest.intersectionVertex(s.getCoordinate().asJtsCoordinate());

0 commit comments

Comments
 (0)