diff --git a/application/src/main/java/org/opentripplanner/transit/model/network/TripPatternBuilder.java b/application/src/main/java/org/opentripplanner/transit/model/network/TripPatternBuilder.java index ec2451ea66d..66f695521c0 100644 --- a/application/src/main/java/org/opentripplanner/transit/model/network/TripPatternBuilder.java +++ b/application/src/main/java/org/opentripplanner/transit/model/network/TripPatternBuilder.java @@ -1,7 +1,5 @@ package org.opentripplanner.transit.model.network; -import static java.util.Objects.requireNonNullElseGet; - import java.util.ArrayList; import java.util.List; import java.util.function.UnaryOperator; @@ -219,15 +217,12 @@ private List generateHopGeometriesFromOriginalTripPattern() { // being replaced having a different number of stops. In that case the geometry will be // preserved up until the first mismatching stop, and a straight line will be used for // all segments after that. - int sizeOfShortestPattern = Math.min( - stopPattern.getSize(), - originalTripPattern.numberOfStops() - ); - List hopGeometries = new ArrayList<>(); - for (int i = 0; i < sizeOfShortestPattern - 1; i++) { - LineString hopGeometry = originalTripPattern.getHopGeometry(i); + for (int i = 0; i < stopPattern.getSize() - 1; i++) { + LineString hopGeometry = i < originalTripPattern.numberOfStops() - 1 + ? originalTripPattern.getHopGeometry(i) + : null; if (hopGeometry != null && stopPattern.sameStops(originalTripPattern.getStopPattern(), i)) { // Copy hop geometry from previous pattern @@ -236,15 +231,8 @@ private List generateHopGeometriesFromOriginalTripPattern() { hopGeometry != null && stopPattern.sameStations(originalTripPattern.getStopPattern(), i) ) { // Use old geometry but patch first and last point with new stops - var newStart = new Coordinate( - stopPattern.getStop(i).getCoordinate().longitude(), - stopPattern.getStop(i).getCoordinate().latitude() - ); - - var newEnd = new Coordinate( - stopPattern.getStop(i + 1).getCoordinate().longitude(), - stopPattern.getStop(i + 1).getCoordinate().latitude() - ); + var newStart = stopPattern.getStop(i).getCoordinate().asJtsCoordinate(); + var newEnd = stopPattern.getStop(i + 1).getCoordinate().asJtsCoordinate(); Coordinate[] coordinates = originalTripPattern.getHopGeometry(i).getCoordinates().clone(); coordinates[0].setCoordinate(newStart); diff --git a/application/src/test/java/org/opentripplanner/transit/model/network/TripPatternTest.java b/application/src/test/java/org/opentripplanner/transit/model/network/TripPatternTest.java index bb0ec765331..32563545694 100644 --- a/application/src/test/java/org/opentripplanner/transit/model/network/TripPatternTest.java +++ b/application/src/test/java/org/opentripplanner/transit/model/network/TripPatternTest.java @@ -24,7 +24,9 @@ class TripPatternTest { private static final Route ROUTE = TimetableRepositoryForTest.route("routeId").build(); public static final RegularStop STOP_A = TEST_MODEL.stop("A").build(); + public static final RegularStop STOP_X = TEST_MODEL.stop("X").build(); public static final RegularStop STOP_B = TEST_MODEL.stop("B").build(); + public static final RegularStop STOP_Y = TEST_MODEL.stop("Y").build(); public static final RegularStop STOP_C = TEST_MODEL.stop("C").build(); private static final StopPattern STOP_PATTERN = TimetableRepositoryForTest.stopPattern( STOP_A, @@ -33,8 +35,8 @@ class TripPatternTest { ); private static final List HOP_GEOMETRIES = List.of( - makeLineString(STOP_A.getCoordinate(), STOP_B.getCoordinate()), - makeLineString(STOP_B.getCoordinate(), STOP_C.getCoordinate()) + makeLineString(STOP_A.getCoordinate(), STOP_X.getCoordinate(), STOP_B.getCoordinate()), + makeLineString(STOP_B.getCoordinate(), STOP_Y.getCoordinate(), STOP_C.getCoordinate()) ); private static final TripPattern subject = TripPattern @@ -70,6 +72,27 @@ void copy() { assertEquals(HOP_GEOMETRIES.get(1), copy.getHopGeometry(1)); } + @Test + void hopGeometryForReplacementPattern() { + var pattern = TripPattern + .of(id("replacement")) + .withName("replacement") + .withRoute(ROUTE) + .withStopPattern(TimetableRepositoryForTest.stopPattern(STOP_A, STOP_B, STOP_X, STOP_Y)) + .withOriginalTripPattern(subject) + .build(); + + assertEquals(HOP_GEOMETRIES.get(0), pattern.getHopGeometry(0)); + assertEquals( + makeLineString(STOP_B.getCoordinate(), STOP_X.getCoordinate()), + pattern.getHopGeometry(1) + ); + assertEquals( + makeLineString(STOP_X.getCoordinate(), STOP_Y.getCoordinate()), + pattern.getHopGeometry(2) + ); + } + @Test void sameAs() { assertTrue(subject.sameAs(subject.copy().build()));