Skip to content

Commit

Permalink
Merge pull request #259 from ibi-group/add-accuracy-and-deviation
Browse files Browse the repository at this point in the history
feat(TrackingLocation): Add fields for deviation + loc accuracy.
  • Loading branch information
binh-dam-ibigroup authored Oct 14, 2024
2 parents d2d1a22 + 052736a commit e006ca8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/latest-spark-swagger-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,12 @@ definitions:
- "ENDED"
- "DEVIATED"
- "COMPLETED"
locationAccuracy:
type: "number"
format: "double"
deviationMeters:
type: "number"
format: "double"
TrackingResponse:
type: "object"
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ void canGenerateInstructionAndStatus(
assertEquals(status.name(), trackResponse.tripStatus);
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,
jsonPayload,
headers,
HttpMethod.POST
);

assertEquals(HttpStatus.OK_200, response.status);
trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class);
assertNotEquals(0, trackResponse.frequencySeconds);
assertEquals(instruction, trackResponse.instruction, message);
assertNotNull(trackResponse.journeyId);
assertEquals(trackedJourney.id, trackResponse.journeyId);
}

private static Stream<Arguments> createInstructionAndStatusCases() {
Expand Down

0 comments on commit e006ca8

Please sign in to comment.