Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for park and ride itineraries when calculating CO2 emissions #5489

Merged
merged 10 commits into from
Nov 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testGetEmissionsForCarRoute() {
@Test
void testNoEmissionsForFeedWithoutEmissionsConfigured() {
Itinerary i = new Itinerary(List.of(createTransitLeg(ROUTE_WITHOUT_EMISSIONS_CONFIGURED)));
assertEquals(null, emissionsFilter.filter(List.of(i)).get(0).getEmissionsPerPerson());
assertNull(emissionsFilter.filter(List.of(i)).get(0).getEmissionsPerPerson());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public List<Itinerary> filter(List<Itinerary> itineraries) {
.map(TransitLeg.class::cast)
.toList();

calculateCo2EmissionsForTransit(transitLegs)
.ifPresent(co2 -> {
itinerary.setEmissionsPerPerson(new Emissions(co2));
});
Optional<Grams> co2ForTransit = calculateCo2EmissionsForTransit(transitLegs);

List<StreetLeg> carLegs = itinerary
.getLegs()
Expand All @@ -43,20 +40,17 @@ public List<Itinerary> filter(List<Itinerary> itineraries) {
.filter(leg -> leg.getMode() == TraverseMode.CAR)
.toList();

calculateCo2EmissionsForCar(carLegs)
.ifPresent(co2 -> {
if (transitLegs.isEmpty()) {
itinerary.setEmissionsPerPerson(new Emissions(co2));
} else if (
itinerary.getEmissionsPerPerson() != null &&
itinerary.getEmissionsPerPerson().getCo2() != null
) {
// Calculate park and ride emissions.
itinerary.setEmissionsPerPerson(
new Emissions(itinerary.getEmissionsPerPerson().getCo2().plus(co2))
);
}
});
Optional<Grams> co2ForCar = calculateCo2EmissionsForCar(carLegs);

if (
co2ForTransit.isPresent() ||
co2ForCar.isPresent() &&
(transitLegs.isEmpty() || co2ForTransit.isPresent())
sharhio marked this conversation as resolved.
Show resolved Hide resolved
) {
Grams co2 = co2ForTransit.isPresent() ? co2ForTransit.get() : new Grams(0);
co2 = co2ForCar.isPresent() ? co2.plus(co2ForCar.get()) : co2;
itinerary.setEmissionsPerPerson(new Emissions(co2));
sharhio marked this conversation as resolved.
Show resolved Hide resolved
}
}
return itineraries;
}
Expand Down