From 65b62dba38379bdd66b593dce19a3befa1b9dea0 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:06:59 -0400 Subject: [PATCH] feat(TrackingLocation): Add fields for deviation + loc accuracy. --- .../middleware/triptracker/ManageTripTracking.java | 1 + .../middleware/triptracker/TrackingLocation.java | 7 +++++++ .../middleware/triptracker/TravelerPosition.java | 6 ++++++ .../opentripplanner/middleware/triptracker/TripStatus.java | 6 +----- .../opentripplanner/middleware/utils/GeometryUtils.java | 2 +- src/main/resources/latest-spark-swagger-output.yaml | 6 ++++++ .../controllers/api/TrackedTripControllerTest.java | 5 +++++ 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java index 0e131ce75..1332bdf12 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java @@ -59,6 +59,7 @@ private static TrackingResponse doUpdateTracking(Request request, TripTrackingDa ); TripStatus tripStatus = TripStatus.getTripStatus(travelerPosition); trackedJourney.lastLocation().tripStatus = tripStatus; + trackedJourney.lastLocation().deviationMeters = travelerPosition.getDeviationMeters(); if (create) { Persistence.trackedJourneys.create(trackedJourney); diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java index 087dc6694..25a755db9 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java @@ -18,8 +18,15 @@ public class TrackingLocation { public Date timestamp; + /** Deviation or on-time status computed for this location. */ public TripStatus tripStatus; + /** FIXME: Device location accuracy, reported by the device in a unit TBD. For reporting only. */ + public Double locationAccuracy; + + /** Perpendicular deviation, computed in meters, to the path closest to this location. */ + public Double deviationMeters; + public TrackingLocation() { // Needed for deserializing objects. } diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TravelerPosition.java b/src/main/java/org/opentripplanner/middleware/triptracker/TravelerPosition.java index 40bd3ae8e..104ee417c 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TravelerPosition.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TravelerPosition.java @@ -13,6 +13,7 @@ import static org.opentripplanner.middleware.triptracker.ManageLegTraversal.getExpectedLeg; import static org.opentripplanner.middleware.triptracker.ManageLegTraversal.getNextLeg; import static org.opentripplanner.middleware.triptracker.ManageLegTraversal.getSegmentFromPosition; +import static org.opentripplanner.middleware.utils.GeometryUtils.getDistanceFromLine; public class TravelerPosition { @@ -81,4 +82,9 @@ public TravelerPosition(Leg nextLeg, Instant currentTime) { this.nextLeg = nextLeg; this.currentTime = currentTime; } + + /** Computes the current deviation in meters from the expected itinerary. */ + public double getDeviationMeters() { + return getDistanceFromLine(legSegmentFromPosition.start, legSegmentFromPosition.end, currentPosition); + } } diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TripStatus.java b/src/main/java/org/opentripplanner/middleware/triptracker/TripStatus.java index 0f4f60459..a8c2b485d 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TripStatus.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TripStatus.java @@ -95,11 +95,7 @@ public static Instant getSegmentStartTime(TravelerPosition travelerPosition) { * Checks if the traveler's position is with an acceptable distance of the mode type. */ private static boolean isWithinModeRadius(TravelerPosition travelerPosition) { - double distanceFromExpected = getDistanceFromLine( - travelerPosition.legSegmentFromPosition.start, - travelerPosition.legSegmentFromPosition.end, - travelerPosition.currentPosition - ); + double distanceFromExpected = travelerPosition.getDeviationMeters(); double modeBoundary = getModeRadius(travelerPosition.legSegmentFromPosition.mode); return distanceFromExpected <= modeBoundary; } diff --git a/src/main/java/org/opentripplanner/middleware/utils/GeometryUtils.java b/src/main/java/org/opentripplanner/middleware/utils/GeometryUtils.java index 9f28bb1c9..d725fb6dd 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/GeometryUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/GeometryUtils.java @@ -22,7 +22,7 @@ public static double getDistance(Coordinates start, Coordinates end) { } /** - * Get the distance between a line and point. + * Get the distance in meters between a line and point. */ public static double getDistanceFromLine(Coordinates start, Coordinates end, Coordinates traveler) { double[] startXY = convertLatLonToXY(start.lat, start.lon); diff --git a/src/main/resources/latest-spark-swagger-output.yaml b/src/main/resources/latest-spark-swagger-output.yaml index 6b97e4c5f..89633d291 100644 --- a/src/main/resources/latest-spark-swagger-output.yaml +++ b/src/main/resources/latest-spark-swagger-output.yaml @@ -2772,6 +2772,12 @@ definitions: - "AHEAD_OF_SCHEDULE" - "ENDED" - "DEVIATED" + locationAccuracy: + type: "number" + format: "double" + deviationMeters: + type: "number" + format: "double" TrackingResponse: type: "object" properties: diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java index 27b3dc84b..98a281447 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java @@ -223,6 +223,11 @@ void canStartThenUpdateOngoingJourney(Coordinates coords, String instruction, St assertNotNull(trackResponse.journeyId); trackedJourney = Persistence.trackedJourneys.getById(trackResponse.journeyId); + // Check that deviation fields get computed and recorded. + Double deviationMeters = trackedJourney.lastLocation().deviationMeters; + assertNotNull(deviationMeters); + assertNotEquals(0, deviationMeters); + // Second request to update a journey response = makeRequest( TRACK_TRIP_PATH,