Skip to content

Commit

Permalink
Merge pull request #5648 from ibi-group/walk-time
Browse files Browse the repository at this point in the history
Separate walk time from non-transit time
  • Loading branch information
leonardehrenfried authored Feb 1, 2024
2 parents 9ed5f22 + 7fc2e87 commit a50ef42
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public DataFetcher<Long> waitingTime() {

@Override
public DataFetcher<Double> walkDistance() {
return environment -> getSource(environment).getNonTransitDistanceMeters();
return environment -> getSource(environment).walkDistanceMeters();
}

@Override
public DataFetcher<Long> walkTime() {
return environment -> (long) getSource(environment).getNonTransitDuration().toSeconds();
return environment -> (long) getSource(environment).walkDuration().toSeconds();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ public static GraphQLObjectType create(
.name("walkTime")
.description("How much time is spent walking, in seconds.")
.type(ExtendedScalars.GraphQLLong)
// TODO This unfortunately include BIKE and CAR
.dataFetcher(env -> itinerary(env).getNonTransitDuration().toSeconds())
.dataFetcher(env -> itinerary(env).walkDuration().toSeconds())
.build()
)
.field(
Expand All @@ -158,7 +157,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,8 +33,7 @@ int transfers() {
}

private void calculate(List<Leg> legs) {
totalDuration =
Duration.between(legs.get(0).getStartTime(), legs.get(legs.size() - 1).getEndTime());
totalDuration = Duration.between(legs.getFirst().getStartTime(), legs.getLast().getEndTime());

for (Leg leg : legs) {
Duration dt = leg.getDuration();
Expand All @@ -45,6 +46,11 @@ private void calculate(List<Leg> legs) {
} else if (leg.isStreetLeg()) {
nonTransitDuration = nonTransitDuration.plus(dt);
nonTransitDistanceMeters += leg.getDistanceMeters();

if (leg.isWalkingLeg()) {
walkDuration = walkDuration.plus(leg.getDuration());
walkDistanceMeters = walkDistanceMeters + leg.getDistanceMeters();
}
} else if (leg instanceof UnknownTransitPathLeg unknownTransitPathLeg) {
nTransitLegs += unknownTransitPathLeg.getNumberOfTransfers() + 1;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/opentripplanner/model/plan/Itinerary.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ 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;
Expand Down Expand Up @@ -76,6 +78,8 @@ public Itinerary(List<Leg> 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;
Expand Down Expand Up @@ -680,6 +684,20 @@ 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;
}

private static int penaltyCost(TimeAndCost penalty) {
return penalty.cost().toSeconds();
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/opentripplanner/model/plan/ItineraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ void walkBusBusWalkTrainWalk() {
);
}

@Test
void walkSeparateFromBike() {
var itin = newItinerary(A, T11_00).walk(D2m, B).bicycle(T11_05, T11_15, D).walk(D3m, E).build();

assertEquals(ofMinutes(15), itin.getNonTransitDuration());
assertEquals(ofMinutes(5), itin.walkDuration());

assertEquals(420, itin.walkDistanceMeters());
assertEquals(3420, itin.getNonTransitDistanceMeters());
}

@Test
void walkSeparateFromCar() {
var itin = newItinerary(A, T11_00).walk(D2m, B).carHail(D10m, D).walk(D3m, E).build();

assertEquals(ofMinutes(15), itin.getNonTransitDuration());
assertEquals(ofMinutes(5), itin.walkDuration());

assertEquals(420, itin.walkDistanceMeters());
assertEquals(15420.0, itin.getNonTransitDistanceMeters());
}

@Test
void legIndex() {
var itinerary = newItinerary(A, T11_00)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"co2" : 123.0
},
"numberOfTransfers" : 1,
"walkDistance" : 28.0,
"walkTime" : 20,
"legs" : [
{
"mode" : "WALK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
co2
}
numberOfTransfers
walkDistance
walkTime
legs {
mode
from {
Expand Down

0 comments on commit a50ef42

Please sign in to comment.