Skip to content

Commit

Permalink
Merge pull request #6136 from Jnction/fix-nextLeg-outOfBounds
Browse files Browse the repository at this point in the history
Fix hop geometries when one pattern is replaced by another with different number of stops
  • Loading branch information
optionsome authored Nov 7, 2024
2 parents 5e7b3a6 + 15c862d commit 01ef0bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -219,15 +217,12 @@ private List<LineString> 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<LineString> 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
Expand All @@ -236,15 +231,8 @@ private List<LineString> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,8 +35,8 @@ class TripPatternTest {
);

private static final List<LineString> 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
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit 01ef0bf

Please sign in to comment.