Skip to content

Commit

Permalink
fix(TravelerLocator): Include beginning of routing as start of leg.
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Feb 27, 2025
1 parent 9241fd3 commit 91ae3ab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ public static boolean isApproachingEndOfLeg(TravelerPosition travelerPosition) {
* Is the traveler at the start of a leg.
*/
public static boolean isAtStartOfLeg(TravelerPosition travelerPosition) {
Coordinates legDestination = new Coordinates(travelerPosition.expectedLeg.from);
return getDistance(travelerPosition.currentPosition, legDestination) <= TRIP_INSTRUCTION_UPCOMING_RADIUS;
return getDistanceToStartOfLeg(travelerPosition) <= TRIP_INSTRUCTION_UPCOMING_RADIUS;
}

/**
Expand Down Expand Up @@ -413,6 +412,33 @@ public static Instant getBusDepartureTime(Leg busLeg) {
).toInstant();
}

private static double getDistanceToStartOfLeg(TravelerPosition travelerPosition) {
return getDistanceToStartOfLeg(travelerPosition, travelerPosition.getLegPositions());
}

/**
* Get the distance from the traveler's current position to the leg destination from given leg positions.
*/
public static double getDistanceToStartOfLeg(TravelerPosition travelerPosition, List<Coordinates> legPositions) {
Coordinates secondCoordinate = legPositions.get(1);
Coordinates firstCoordinate = legPositions.get(0);
Coordinates legOrigin = new Coordinates(travelerPosition.expectedLeg.from);

// HACK:
// If the first leg position coordinate is identical to the leg origin,
// it probably means the origin is off the street network, so the first shape coordinate is at pos (1).
// If the first leg position coordinate differs from the leg origin,
// then the origin is probably on the street network, so the first shape coordinate is at pos (0).
double distanceToFirstShapeCoords = getDistance(
travelerPosition.currentPosition,
firstCoordinate.equals(legOrigin) ? secondCoordinate : firstCoordinate
);

double distanceToLegOrigin = getDistance(travelerPosition.currentPosition, legOrigin);

return Math.min(distanceToFirstShapeCoords, distanceToLegOrigin);
}

private static double getDistanceToEndOfLeg(TravelerPosition travelerPosition) {
return getDistanceToEndOfLeg(travelerPosition, travelerPosition.getLegPositions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.middleware.triptracker.ManageLegTraversal.getSecondsToMilliseconds;
import static org.opentripplanner.middleware.triptracker.ManageLegTraversal.interpolatePoints;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.getNextWayPoint;
Expand Down Expand Up @@ -762,6 +761,30 @@ void cumulativeSegmentTimeMatchesWalkLegDuration() {
assertEquals(busStopToJusticeCenterItinerary.legs.get(0).duration, cumulative, 0.01f);
}

/**
* Handles cases where the distance to end of leg was previously incorrectly computed.
*/
@ParameterizedTest
@MethodSource("createDistanceToStartOfLegCases")
void testGetDistanceToStartOfLeg(Itinerary itinerary, Coordinates coordinates, boolean isWithinRadius) {
TrackedJourney trackedJourney = new TrackedJourney();
trackedJourney.locations = List.of(
new TrackingLocation(Instant.now(), coordinates.lat, coordinates.lon)
);
TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, itinerary, null);

assertEquals(isWithinRadius, TravelerLocator.isAtStartOfLeg(travelerPosition));
}

private static Stream<Arguments> createDistanceToStartOfLegCases() {
return Stream.of(
// Close to start of routing (outside of origin building) for walk trip to One Justice Square
Arguments.of( walkGjacTo1js, new Coordinates(33.951786, -83.992887), true),
// Inside of origin building away from start of routing for walk trip to One Justice Square
Arguments.of(walkGjacTo1js, new Coordinates(33.951563, -83.992954), false)
);
}

/**
* Handles cases where the distance to end of leg was previously incorrectly computed.
*/
Expand Down

0 comments on commit 91ae3ab

Please sign in to comment.