Skip to content

Commit 30cc925

Browse files
committed
Simplify implementation by removing changes to StreetNearbyStopFinder.
1 parent c90c3ec commit 30cc925

File tree

3 files changed

+61
-180
lines changed

3 files changed

+61
-180
lines changed

application/src/main/java/org/opentripplanner/graph_builder/module/DirectTransferGenerator.java

Lines changed: 59 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,13 @@ public void buildGraph() {
9393
timetableRepository.index();
9494

9595
/* The linker will use streets if they are available, or straight-line distance otherwise. */
96-
NearbyStopFinder nearbyStopFinder = createNearbyStopFinder(
97-
defaultMaxTransferDuration,
98-
Set.of()
99-
);
96+
NearbyStopFinder nearbyStopFinder = createNearbyStopFinder(defaultMaxTransferDuration);
10097
HashMap<StreetMode, NearbyStopFinder> defaultNearbyStopFinders = new HashMap<>();
10198
/* These are used for calculating transfers only between carsAllowedStops. */
10299
HashMap<StreetMode, NearbyStopFinder> carsAllowedStopNearbyStopFinders = new HashMap<>();
103100

104101
List<TransitStopVertex> stops = graph.getVerticesOfType(TransitStopVertex.class);
105-
Set<TransitStopVertex> carsAllowedStops = timetableRepository
106-
.getStopLocationsUsedForCarsAllowedTrips()
107-
.stream()
108-
.map(StopLocation::getId)
109-
.map(graph::getStopVertexForStopId)
110-
// filter out null values if no TransitStopVertex is found for ID
111-
.filter(TransitStopVertex.class::isInstance)
112-
.collect(Collectors.toSet());
102+
Set<StopLocation> carsAllowedStops = timetableRepository.getStopLocationsUsedForCarsAllowedTrips();
113103

114104
LOG.info("Creating transfers based on requests:");
115105
transferRequests.forEach(transferProfile -> LOG.info(transferProfile.toString()));
@@ -147,8 +137,7 @@ public void buildGraph() {
147137
flexTransferRequests,
148138
defaultNearbyStopFinders,
149139
carsAllowedStopNearbyStopFinders,
150-
nearbyStopFinder,
151-
carsAllowedStops
140+
nearbyStopFinder
152141
);
153142

154143
stops
@@ -169,14 +158,29 @@ public void buildGraph() {
169158
// Calculate default transfers.
170159
for (RouteRequest transferProfile : defaultTransferRequests) {
171160
StreetMode mode = transferProfile.journey().transfer().mode();
172-
findNearbyStops(
173-
defaultNearbyStopFinders.get(mode),
174-
ts0,
175-
transferProfile,
176-
stop,
177-
distinctTransfers,
178-
mode
179-
);
161+
for (NearbyStop sd : defaultNearbyStopFinders
162+
.get(mode)
163+
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false)) {
164+
// Skip the origin stop, loop transfers are not needed.
165+
if (sd.stop == stop) {
166+
continue;
167+
}
168+
if (sd.stop.transfersNotAllowed()) {
169+
continue;
170+
}
171+
TransferKey transferKey = new TransferKey(stop, sd.stop, sd.edges);
172+
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
173+
if (pathTransfer == null) {
174+
// If the PathTransfer can't be found, it is created.
175+
distinctTransfers.put(
176+
transferKey,
177+
new PathTransfer(stop, sd.stop, sd.distance, sd.edges, EnumSet.of(mode))
178+
);
179+
} else {
180+
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
181+
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
182+
}
183+
}
180184
}
181185
// Calculate flex transfers if flex routing is enabled.
182186
for (RouteRequest transferProfile : flexTransferRequests) {
@@ -210,17 +214,36 @@ public void buildGraph() {
210214
}
211215
}
212216
// Calculate transfers between stops that can use trips with cars if configured.
213-
if (carsAllowedStops.contains(ts0)) {
217+
if (carsAllowedStops.contains(stop)) {
214218
for (RouteRequest transferProfile : carsAllowedStopTransferRequests) {
215219
StreetMode mode = transferProfile.journey().transfer().mode();
216-
findNearbyStops(
217-
carsAllowedStopNearbyStopFinders.get(mode),
218-
ts0,
219-
transferProfile,
220-
stop,
221-
distinctTransfers,
222-
mode
223-
);
220+
for (NearbyStop sd : carsAllowedStopNearbyStopFinders
221+
.get(mode)
222+
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false)) {
223+
// Skip the origin stop, loop transfers are not needed.
224+
if (sd.stop == stop) {
225+
continue;
226+
}
227+
if (sd.stop.transfersNotAllowed()) {
228+
continue;
229+
}
230+
// Only calculate transfers between carsAllowedStops.
231+
if (!carsAllowedStops.contains(sd.stop)) {
232+
continue;
233+
}
234+
TransferKey transferKey = new TransferKey(stop, sd.stop, sd.edges);
235+
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
236+
if (pathTransfer == null) {
237+
// If the PathTransfer can't be found, it is created.
238+
distinctTransfers.put(
239+
transferKey,
240+
new PathTransfer(stop, sd.stop, sd.distance, sd.edges, EnumSet.of(mode))
241+
);
242+
} else {
243+
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
244+
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
245+
}
246+
}
224247
}
225248
}
226249

@@ -270,10 +293,7 @@ public void buildGraph() {
270293
* whether the graph has a street network and if ConsiderPatternsForDirectTransfers feature is
271294
* enabled.
272295
*/
273-
private NearbyStopFinder createNearbyStopFinder(
274-
Duration radiusByDuration,
275-
Set<Vertex> findOnlyVertices
276-
) {
296+
private NearbyStopFinder createNearbyStopFinder(Duration radiusByDuration) {
277297
var transitService = new DefaultTransitService(timetableRepository);
278298
NearbyStopFinder finder;
279299
if (!graph.hasStreets) {
@@ -283,7 +303,7 @@ private NearbyStopFinder createNearbyStopFinder(
283303
finder = new StraightLineNearbyStopFinder(transitService, radiusByDuration);
284304
} else {
285305
LOG.info("Creating direct transfer edges between stops using the street network from OSM...");
286-
finder = new StreetNearbyStopFinder(radiusByDuration, 0, null, Set.of(), findOnlyVertices);
306+
finder = new StreetNearbyStopFinder(radiusByDuration, 0, null, Set.of());
287307
}
288308

289309
if (OTPFeature.ConsiderPatternsForDirectTransfers.isOn()) {
@@ -299,8 +319,7 @@ private void parseTransferParameters(
299319
List<RouteRequest> flexTransferRequests,
300320
HashMap<StreetMode, NearbyStopFinder> defaultNearbyStopFinders,
301321
HashMap<StreetMode, NearbyStopFinder> carsAllowedStopNearbyStopFinders,
302-
NearbyStopFinder nearbyStopFinder,
303-
Set<TransitStopVertex> carsAllowedStops
322+
NearbyStopFinder nearbyStopFinder
304323
) {
305324
for (RouteRequest transferProfile : transferRequests) {
306325
StreetMode mode = transferProfile.journey().transfer().mode();
@@ -313,10 +332,7 @@ private void parseTransferParameters(
313332
// Set mode-specific maxTransferDuration, if it is set in the build config.
314333
Duration maxTransferDuration = transferParameters.maxTransferDuration();
315334
if (maxTransferDuration != Duration.ZERO) {
316-
defaultNearbyStopFinders.put(
317-
mode,
318-
createNearbyStopFinder(maxTransferDuration, Set.of())
319-
);
335+
defaultNearbyStopFinders.put(mode, createNearbyStopFinder(maxTransferDuration));
320336
} else {
321337
defaultNearbyStopFinders.put(mode, nearbyStopFinder);
322338
}
@@ -327,10 +343,7 @@ private void parseTransferParameters(
327343
carsAllowedStopTransferRequests.add(transferProfile);
328344
carsAllowedStopNearbyStopFinders.put(
329345
mode,
330-
createNearbyStopFinder(
331-
carsAllowedStopMaxTransferDuration,
332-
Collections.<Vertex>unmodifiableSet(carsAllowedStops)
333-
)
346+
createNearbyStopFinder(carsAllowedStopMaxTransferDuration)
334347
);
335348
}
336349
} else {
@@ -350,41 +363,5 @@ private void parseTransferParameters(
350363
}
351364
}
352365

353-
private void findNearbyStops(
354-
NearbyStopFinder nearbyStopFinder,
355-
TransitStopVertex ts0,
356-
RouteRequest transferProfile,
357-
RegularStop stop,
358-
Map<TransferKey, PathTransfer> distinctTransfers,
359-
StreetMode mode
360-
) {
361-
for (NearbyStop sd : nearbyStopFinder.findNearbyStops(
362-
ts0,
363-
transferProfile,
364-
transferProfile.journey().transfer(),
365-
false
366-
)) {
367-
// Skip the origin stop, loop transfers are not needed.
368-
if (sd.stop == stop) {
369-
continue;
370-
}
371-
if (sd.stop.transfersNotAllowed()) {
372-
continue;
373-
}
374-
TransferKey transferKey = new TransferKey(stop, sd.stop, sd.edges);
375-
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
376-
if (pathTransfer == null) {
377-
// If the PathTransfer can't be found, it is created.
378-
distinctTransfers.put(
379-
transferKey,
380-
new PathTransfer(stop, sd.stop, sd.distance, sd.edges, EnumSet.of(mode))
381-
);
382-
} else {
383-
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
384-
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
385-
}
386-
}
387-
}
388-
389366
private record TransferKey(StopLocation source, StopLocation target, List<Edge> edges) {}
390367
}

application/src/main/java/org/opentripplanner/graph_builder/module/nearbystops/StreetNearbyStopFinder.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class StreetNearbyStopFinder implements NearbyStopFinder {
3838
private final int maxStopCount;
3939
private final DataOverlayContext dataOverlayContext;
4040
private final Set<Vertex> ignoreVertices;
41-
private final Set<Vertex> findOnlyVertices;
4241

4342
/**
4443
* Construct a NearbyStopFinder for the given graph and search radius.
@@ -51,7 +50,7 @@ public StreetNearbyStopFinder(
5150
int maxStopCount,
5251
DataOverlayContext dataOverlayContext
5352
) {
54-
this(durationLimit, maxStopCount, dataOverlayContext, Set.of(), Set.of());
53+
this(durationLimit, maxStopCount, dataOverlayContext, Set.of());
5554
}
5655

5756
/**
@@ -66,31 +65,11 @@ public StreetNearbyStopFinder(
6665
int maxStopCount,
6766
DataOverlayContext dataOverlayContext,
6867
Set<Vertex> ignoreVertices
69-
) {
70-
this(durationLimit, maxStopCount, dataOverlayContext, ignoreVertices, Set.of());
71-
}
72-
73-
/**
74-
* Construct a NearbyStopFinder for the given graph and search radius.
75-
*
76-
* @param maxStopCount The maximum stops to return. 0 means no limit. Regardless of the maxStopCount
77-
* we will always return all the directly connected stops.
78-
* @param ignoreVertices A set of stop vertices to ignore and not return NearbyStops for.
79-
*
80-
* @param findOnlyVertices Only return NearbyStops that are in this set. If this is empty, no filtering is performed.
81-
*/
82-
public StreetNearbyStopFinder(
83-
Duration durationLimit,
84-
int maxStopCount,
85-
DataOverlayContext dataOverlayContext,
86-
Set<Vertex> ignoreVertices,
87-
Set<Vertex> findOnlyVertices
8868
) {
8969
this.dataOverlayContext = dataOverlayContext;
9070
this.durationLimit = durationLimit;
9171
this.maxStopCount = maxStopCount;
9272
this.ignoreVertices = ignoreVertices;
93-
this.findOnlyVertices = findOnlyVertices;
9473
}
9574

9675
/**
@@ -170,10 +149,7 @@ public Collection<NearbyStop> findNearbyStops(
170149
continue;
171150
}
172151
if (targetVertex instanceof TransitStopVertex tsv && state.isFinal()) {
173-
// If a set of findOnlyVertices is provided, only add stops that are in the set.
174-
if (findOnlyVertices.isEmpty() || findOnlyVertices.contains(targetVertex)) {
175-
stopsFound.add(NearbyStop.nearbyStopForState(state, tsv.getStop()));
176-
}
152+
stopsFound.add(NearbyStop.nearbyStopForState(state, tsv.getStop()));
177153
}
178154
if (
179155
OTPFeature.FlexRouting.isOn() &&

application/src/test/java/org/opentripplanner/graph_builder/module/nearbystops/StreetNearbyStopFinderTest.java

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -163,78 +163,6 @@ void testIgnoreStopsWithMaxStops() {
163163
assertStopAtDistance(stopC, 200, sortedNearbyStops.get(0));
164164
}
165165

166-
@Test
167-
void testFindOnlyVerticesStops() {
168-
var durationLimit = Duration.ofMinutes(10);
169-
var maxStopCount = 0;
170-
Set<Vertex> findOnlyStops = Set.of(stopB, stopC);
171-
var finder = new StreetNearbyStopFinder(
172-
durationLimit,
173-
maxStopCount,
174-
null,
175-
Set.of(),
176-
findOnlyStops
177-
);
178-
179-
var sortedNearbyStops = sort(
180-
finder.findNearbyStops(stopA, new RouteRequest(), new StreetRequest(), false)
181-
);
182-
183-
assertThat(sortedNearbyStops).hasSize(3);
184-
assertZeroDistanceStop(stopA, sortedNearbyStops.get(0));
185-
assertStopAtDistance(stopB, 100, sortedNearbyStops.get(1));
186-
assertStopAtDistance(stopC, 200, sortedNearbyStops.get(2));
187-
}
188-
189-
@Test
190-
void testFindOnlyVerticesStopsWithIgnore() {
191-
var durationLimit = Duration.ofMinutes(10);
192-
var maxStopCount = 0;
193-
Set<Vertex> findOnlyStops = Set.of(stopB, stopC);
194-
Set<Vertex> ignore = Set.of(stopB);
195-
var finder = new StreetNearbyStopFinder(
196-
durationLimit,
197-
maxStopCount,
198-
null,
199-
ignore,
200-
findOnlyStops
201-
);
202-
203-
var sortedNearbyStops = sort(
204-
finder.findNearbyStops(stopA, new RouteRequest(), new StreetRequest(), false)
205-
);
206-
207-
assertThat(sortedNearbyStops).hasSize(2);
208-
assertZeroDistanceStop(stopA, sortedNearbyStops.get(0));
209-
assertStopAtDistance(stopC, 200, sortedNearbyStops.get(1));
210-
}
211-
212-
@Test
213-
void testFindOnlyVerticesStopsWithDurationLimit() {
214-
// If we only allow walk for 101 seconds and speed is 1 m/s we should only be able to reach
215-
// one extra stop.
216-
var durationLimit = Duration.ofSeconds(101);
217-
var maxStopCount = 0;
218-
Set<Vertex> findOnlyStops = Set.of(stopB, stopC);
219-
var routeRequest = new RouteRequest()
220-
.withPreferences(b -> b.withWalk(walkPreferences -> walkPreferences.withSpeed(1.0)));
221-
222-
var finder = new StreetNearbyStopFinder(
223-
durationLimit,
224-
maxStopCount,
225-
null,
226-
Set.of(),
227-
findOnlyStops
228-
);
229-
var sortedNearbyStops = sort(
230-
finder.findNearbyStops(stopA, routeRequest, new StreetRequest(), false)
231-
);
232-
233-
assertThat(sortedNearbyStops).hasSize(2);
234-
assertZeroDistanceStop(stopA, sortedNearbyStops.get(0));
235-
assertStopAtDistance(stopB, 100, sortedNearbyStops.get(1));
236-
}
237-
238166
static List<NearbyStop> sort(Collection<NearbyStop> stops) {
239167
return stops.stream().sorted(Comparator.comparing(x -> x.distance)).toList();
240168
}

0 commit comments

Comments
 (0)