Skip to content

Commit a759fd9

Browse files
authored
Merge pull request #7056 from entur/fix-coordinate-fallback
Fix coordinate fallback when linking locations in the street graph
2 parents 48284b8 + e193e24 commit a759fd9

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

application/src/main/java/org/opentripplanner/routing/linking/LinkingContextFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ private Set<Vertex> getStreetVerticesForLocation(
280280
var carRoutableVertex = getCarRoutableStreetVertex(container, location, type);
281281
carRoutableVertex.ifPresent(results::add);
282282
}
283-
} else if (location.getCoordinate() != null) {
283+
}
284+
285+
// If no vertices found from stop ID lookup and coordinates are available, use coordinates as fallback
286+
if (results.isEmpty() && location.getCoordinate() != null) {
284287
// Connect a temporary vertex from coordinate to graph
285288
results.add(
286289
vertexCreationService.createVertexFromCoordinate(

application/src/test/java/org/opentripplanner/routing/linking/LinkingContextFactoryTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.opentripplanner.street.model.edge.Edge;
2929
import org.opentripplanner.street.model.edge.StreetStationCentroidLink;
3030
import org.opentripplanner.street.model.vertex.StationCentroidVertex;
31+
import org.opentripplanner.street.model.vertex.TemporaryStreetLocation;
3132
import org.opentripplanner.street.model.vertex.TransitStopVertex;
3233
import org.opentripplanner.street.model.vertex.Vertex;
3334
import org.opentripplanner.street.search.TraverseMode;
@@ -288,6 +289,41 @@ void walkingBetterThanTransitException() {
288289
assertEquals(RoutingErrorCode.WALKING_BETTER_THAN_TRANSIT, fromError.code);
289290
}
290291

292+
@Test
293+
void nonExistingPlaceIdWithCoordinatesShouldFallbackToCoordinates() {
294+
var container = new TemporaryVerticesContainer();
295+
var nonExistingStopId = new FeedScopedId("F", "NonExistingStop");
296+
297+
// Create locations with both a non-existing stop ID and valid coordinates
298+
var from = new GenericLocation("From", nonExistingStopId, stopA.getLat(), stopA.getLon());
299+
var to = new GenericLocation(
300+
"To",
301+
new FeedScopedId("F", "AnotherNonExisting"),
302+
stopD.getLat(),
303+
stopD.getLon()
304+
);
305+
306+
var request = LinkingContextRequest.of()
307+
.withFrom(from)
308+
.withTo(to)
309+
.withDirectMode(StreetMode.WALK)
310+
.build();
311+
312+
// This should NOT throw an exception - it should fall back to using coordinates
313+
var linkingContext = linkingContextFactory.create(container, request);
314+
315+
// Verify that vertices were created from the coordinates
316+
var fromVertices = linkingContext.findVertices(from);
317+
assertThat(fromVertices).hasSize(1);
318+
assertTemporaryVertexOnStop(fromVertices.stream().findFirst().get(), stopA);
319+
320+
var toVertices = linkingContext.findVertices(to);
321+
assertThat(toVertices).hasSize(1);
322+
assertTemporaryVertexOnStop(toVertices.stream().findFirst().get(), stopD);
323+
324+
container.close();
325+
}
326+
291327
private static Graph buildGraph(Station station, RegularStop... stops) {
292328
var graph = new Graph();
293329
var left = StreetModelForTest.intersectionVertex(CENTER.asJtsCoordinate());
@@ -323,6 +359,12 @@ private static Graph buildGraph(Station station, RegularStop... stops) {
323359
return graph;
324360
}
325361

362+
private void assertTemporaryVertexOnStop(Vertex vertex, RegularStop stop) {
363+
assertThat(vertex).isInstanceOf(TemporaryStreetLocation.class);
364+
assertEquals(stop.getLat(), vertex.getLat(), 0.0001);
365+
assertEquals(stop.getLon(), vertex.getLon(), 0.0001);
366+
}
367+
326368
private RegularStop toStop(Set<? extends Vertex> fromVertices) {
327369
assertThat(fromVertices).hasSize(1);
328370
var id = ((TransitStopVertex) List.copyOf(fromVertices).getFirst()).getId();

0 commit comments

Comments
 (0)