From bb2e7530f380d599dac479c1d769a673a8195b3b Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Mon, 29 Jan 2024 13:47:15 +0100 Subject: [PATCH] Separate walk from non-transit time --- .../apis/gtfs/datafetchers/ItineraryImpl.java | 4 ++-- .../model/plan/TripPatternType.java | 2 +- .../plan/ItinerariesCalculateLegTotals.java | 9 ++++++++- .../opentripplanner/model/plan/Itinerary.java | 19 +++++++++++++++++++ .../apis/gtfs/expectations/plan-extended.json | 2 ++ .../apis/gtfs/queries/plan-extended.graphql | 2 ++ 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/ItineraryImpl.java b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/ItineraryImpl.java index 5399783bee0..63104ecc79a 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/ItineraryImpl.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/ItineraryImpl.java @@ -94,12 +94,12 @@ public DataFetcher waitingTime() { @Override public DataFetcher walkDistance() { - return environment -> getSource(environment).getNonTransitDistanceMeters(); + return environment -> getSource(environment).walkDistanceMeters(); } @Override public DataFetcher walkTime() { - return environment -> (long) getSource(environment).getNonTransitDuration().toSeconds(); + return environment -> (long) getSource(environment).walkDuration().toSeconds(); } @Override diff --git a/src/main/java/org/opentripplanner/apis/transmodel/model/plan/TripPatternType.java b/src/main/java/org/opentripplanner/apis/transmodel/model/plan/TripPatternType.java index 158d6a9a042..bbef4172c69 100644 --- a/src/main/java/org/opentripplanner/apis/transmodel/model/plan/TripPatternType.java +++ b/src/main/java/org/opentripplanner/apis/transmodel/model/plan/TripPatternType.java @@ -158,7 +158,7 @@ public static GraphQLObjectType create( .name("walkDistance") .deprecate("Replaced by `streetDistance`.") .type(Scalars.GraphQLFloat) - .dataFetcher(env -> itinerary(env).getNonTransitDistanceMeters()) + .dataFetcher(env -> itinerary(env).walkDistanceMeters()) .build() ) .field( diff --git a/src/main/java/org/opentripplanner/model/plan/ItinerariesCalculateLegTotals.java b/src/main/java/org/opentripplanner/model/plan/ItinerariesCalculateLegTotals.java index c512e47d732..50d859d821d 100644 --- a/src/main/java/org/opentripplanner/model/plan/ItinerariesCalculateLegTotals.java +++ b/src/main/java/org/opentripplanner/model/plan/ItinerariesCalculateLegTotals.java @@ -13,6 +13,8 @@ class ItinerariesCalculateLegTotals { int nTransitLegs = 0; Duration nonTransitDuration = Duration.ZERO; double nonTransitDistanceMeters = 0.0; + Duration walkDuration = Duration.ZERO; + double walkDistanceMeters = 0.0; Duration waitingDuration = Duration.ZERO; boolean walkOnly = true; boolean streetOnly = true; @@ -42,9 +44,14 @@ private void calculate(List legs) { if (!leg.isInterlinedWithPreviousLeg()) { ++nTransitLegs; } - } else if (leg.isStreetLeg()) { + } else if (leg instanceof StreetLeg streetLeg){ nonTransitDuration = nonTransitDuration.plus(dt); nonTransitDistanceMeters += leg.getDistanceMeters(); + + if(streetLeg.isWalkingLeg()){ + walkDuration = walkDuration.plus(streetLeg.getDuration()); + walkDistanceMeters = walkDistanceMeters + streetLeg.getDistanceMeters(); + } } else if (leg instanceof UnknownTransitPathLeg unknownTransitPathLeg) { nTransitLegs += unknownTransitPathLeg.getNumberOfTransfers() + 1; } diff --git a/src/main/java/org/opentripplanner/model/plan/Itinerary.java b/src/main/java/org/opentripplanner/model/plan/Itinerary.java index 903e04c8b0c..2addae47413 100644 --- a/src/main/java/org/opentripplanner/model/plan/Itinerary.java +++ b/src/main/java/org/opentripplanner/model/plan/Itinerary.java @@ -39,6 +39,9 @@ public class Itinerary implements ItinerarySortKey { private final boolean walkOnly; private final boolean streetOnly; private final Duration nonTransitDuration; + private final Duration walkDuration; + private final double walkDistanceMeters; + /* mutable primitive properties */ private Double elevationLost = 0.0; @@ -75,6 +78,8 @@ public Itinerary(List legs) { this.transitDuration = totals.transitDuration; this.nonTransitDuration = totals.nonTransitDuration; this.nonTransitDistanceMeters = DoubleUtils.roundTo2Decimals(totals.nonTransitDistanceMeters); + this.walkDuration = totals.walkDuration; + this.walkDistanceMeters = totals.walkDistanceMeters; this.waitingDuration = totals.waitingDuration; this.walkOnly = totals.walkOnly; this.streetOnly = totals.streetOnly; @@ -663,4 +668,18 @@ public void setEmissionsPerPerson(Emissions emissionsPerPerson) { public Emissions getEmissionsPerPerson() { return this.emissionsPerPerson; } + + /** + * How much walking this itinerary contains, in meters. + */ + public double walkDistanceMeters() { + return walkDistanceMeters; + } + + /** + * How long the walking is contained in this itinerary. + */ + public Duration walkDuration() { + return walkDuration; + } } diff --git a/src/test/resources/org/opentripplanner/apis/gtfs/expectations/plan-extended.json b/src/test/resources/org/opentripplanner/apis/gtfs/expectations/plan-extended.json index 681bb4715ab..557cdec8659 100644 --- a/src/test/resources/org/opentripplanner/apis/gtfs/expectations/plan-extended.json +++ b/src/test/resources/org/opentripplanner/apis/gtfs/expectations/plan-extended.json @@ -11,6 +11,8 @@ "co2" : 123.0 }, "numberOfTransfers" : 1, + "walkDistance" : 28.0, + "walkTime" : 20, "legs" : [ { "mode" : "WALK", diff --git a/src/test/resources/org/opentripplanner/apis/gtfs/queries/plan-extended.graphql b/src/test/resources/org/opentripplanner/apis/gtfs/queries/plan-extended.graphql index 76c8590ab30..db30d8489ef 100644 --- a/src/test/resources/org/opentripplanner/apis/gtfs/queries/plan-extended.graphql +++ b/src/test/resources/org/opentripplanner/apis/gtfs/queries/plan-extended.graphql @@ -24,6 +24,8 @@ co2 } numberOfTransfers + walkDistance + walkTime legs { mode from {