routingalternatives: consider transit options
This commit is contained in:
@@ -54,7 +54,7 @@ public class CartaClient {
|
||||
destination,
|
||||
mode.toLowerCase(), // possible values: walking, driving, cycling
|
||||
arrivalTime.getEpochSecond(),
|
||||
departureTime.getEpochSecond(),
|
||||
departureTime != null ? departureTime.getEpochSecond() : null,
|
||||
alternatives,
|
||||
walkingSpeed,
|
||||
transferTime,
|
||||
|
||||
@@ -3,6 +3,8 @@ package SuMoC.backend.routing;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
|
||||
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
|
||||
import SuMoC.backend.routing.services.RoutingService;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -29,8 +31,8 @@ public class RoutingController {
|
||||
this.routingService = routingService;
|
||||
}
|
||||
|
||||
// TODO operation description + how to handle preferences (optional, if not provided use saved users prefs or default
|
||||
@PostMapping("/request")
|
||||
@Schema(description = "Calculates alternative, sustainable routes for the request. The delta of departure and arrival in the requestdto is used as baseline to determine if alternatives are acceptable.")
|
||||
public ResponseEntity<List<RouteDTO>> requestAlternativeRoutes(@Valid @RequestBody RoutingAlternativeRequestDTO routingAlternativeRequestDTO) {
|
||||
try {
|
||||
if (routingAlternativeRequestDTO.getRoutingPreferencesDTO() == null) { // Use default preferences if none are provided
|
||||
|
||||
@@ -44,10 +44,12 @@ public class RouteDTO {
|
||||
private List<StepDTO> steps;
|
||||
@Schema(description = "Amount of CO2e savings in gramm compared to a car trip for the same route.")
|
||||
private double co2Savings;
|
||||
@Schema(description = "CO2e emission of this route.")
|
||||
private double co2Emissions;
|
||||
@Schema(description = "The temporal difference in minutes compared to the original route the user requested alternatives for.")
|
||||
private double timeDifferenceSeconds;
|
||||
|
||||
public RouteDTO(AddressDTO startAddress, @Nullable String firstStopName, double startLat, double startLon, AddressDTO endAddress, @Nullable String lastStopName, double endLat, double endLon, Instant startDateTime, Instant endDateTime, List<StepDTO> steps, double co2Savings, double timeDifferenceSeconds) {
|
||||
public RouteDTO(AddressDTO startAddress, @Nullable String firstStopName, double startLat, double startLon, AddressDTO endAddress, @Nullable String lastStopName, double endLat, double endLon, Instant startDateTime, Instant endDateTime, List<StepDTO> steps, double co2Savings, double timeDifferenceSeconds, double co2Emissions) {
|
||||
this.startAddress = startAddress;
|
||||
this.firstStopName = firstStopName;
|
||||
this.startLat = startLat;
|
||||
@@ -60,6 +62,7 @@ public class RouteDTO {
|
||||
this.endDateTime = endDateTime;
|
||||
this.steps = steps;
|
||||
this.co2Savings = co2Savings;
|
||||
this.co2Emissions = co2Emissions;
|
||||
this.timeDifferenceSeconds = timeDifferenceSeconds;
|
||||
}
|
||||
|
||||
@@ -106,7 +109,8 @@ public class RouteDTO {
|
||||
Instant.now(),
|
||||
new ArrayList<StepDTO>(),
|
||||
120.5f,
|
||||
+16.3f
|
||||
+16.3f,
|
||||
20
|
||||
);
|
||||
dto.steps.add(new StepDTO(
|
||||
Modality.WALKING,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package SuMoC.backend.routing.services;
|
||||
|
||||
import SuMoC.backend.routing.dtos.StepDTO;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import sumoc.backend.carta.client.model.Route;
|
||||
import sumoc.backend.carta.client.model.Step;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Processes a Carta Route and extracts StepDTOs and first , last stop name.
|
||||
*/
|
||||
public class CartaTransitRouteProcessor {
|
||||
private final Route cartaRoute;
|
||||
|
||||
private List<StepDTO> steps = null;
|
||||
|
||||
private String firstStopName = null;
|
||||
|
||||
private String lastStopName = null;
|
||||
|
||||
public CartaTransitRouteProcessor(Route cartaRoute) {
|
||||
this.cartaRoute = cartaRoute;
|
||||
this.process();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts information from the route.
|
||||
*/
|
||||
public void process() {
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
|
||||
assert cartaRoute.getSteps() != null;
|
||||
for (Step step : cartaRoute.getSteps()) {
|
||||
// Get first stop name
|
||||
if (this.firstStopName == null && step.getType().equals("TransitStep")) {
|
||||
this.firstStopName = step.getStops().getFirst().getName();
|
||||
}
|
||||
// always set last stop as lastStopName so it will match in the end
|
||||
if (step.getType().equals("TransitStep")) {
|
||||
this.lastStopName = step.getStops().getLast().getName();
|
||||
}
|
||||
|
||||
Modality stepModality = step.getType().equals("TransitStep")
|
||||
? Modality.fromCartaString(step.getLine().getType().toString())
|
||||
: Modality.fromCartaString(step.getType());
|
||||
|
||||
steps.add(new StepDTO(
|
||||
stepModality,
|
||||
step.getType().equals("TransitStep") ? step.getLine().getCode() : null,
|
||||
step.getDistance()
|
||||
));
|
||||
}
|
||||
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
public List<StepDTO> getSteps() {
|
||||
if (this.steps == null) {
|
||||
throw new IllegalStateException("Getter called before processing");
|
||||
}
|
||||
return this.steps;
|
||||
}
|
||||
|
||||
public String getFirstStopName() {
|
||||
if (this.firstStopName == null) {
|
||||
throw new IllegalStateException("Getter called before processing");
|
||||
}
|
||||
return this.firstStopName;
|
||||
}
|
||||
|
||||
public String getLastStopName() {
|
||||
if (this.lastStopName == null) {
|
||||
throw new IllegalStateException("Getter called before processing");
|
||||
}
|
||||
return this.lastStopName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+37
-5
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.routing;
|
||||
package SuMoC.backend.routing.services;
|
||||
|
||||
import SuMoC.backend.carta.CartaClient;
|
||||
import SuMoC.backend.geocoding.AddressDTO;
|
||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
// TODO refactor this service into smaller submodules
|
||||
public class RoutingService {
|
||||
|
||||
private final GeoCodingService geoCodingService;
|
||||
@@ -52,7 +53,7 @@ public class RoutingService {
|
||||
requestDTO.getDestinationLat() + "," + requestDTO.getDestinationLon(),
|
||||
mode,
|
||||
requestDTO.getArrivalTime(),
|
||||
requestDTO.getDepartureTime(),
|
||||
null,
|
||||
alternatives, // alternatives
|
||||
null, // walkingSpeed
|
||||
null, // transferTime TODO what is default? adapt?
|
||||
@@ -118,6 +119,8 @@ public class RoutingService {
|
||||
HashMap<String, Direction> directionMap = getPossibleDirectionsFromCarta(requestDTO);
|
||||
ArrayList<RouteDTO> suggestedAlternatives = new ArrayList<>();
|
||||
|
||||
// TODO we have to use actual trip duration as comparison and not cartas car duration
|
||||
|
||||
// Fetch origin and destination addresses concurrently
|
||||
var addressesMono = Mono.zip(
|
||||
geoCodingService.getAddress(requestDTO.getOriginLat(), requestDTO.getOriginLon()),
|
||||
@@ -174,7 +177,8 @@ public class RoutingService {
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
bikeRoute.getDuration() - carRoute.getDuration()
|
||||
bikeRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
@@ -207,14 +211,42 @@ public class RoutingService {
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
walkRoute.getDuration() - carRoute.getDuration()
|
||||
walkRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO public transit
|
||||
if (directionMap.containsKey("transit")) {
|
||||
Direction transitDirection = directionMap.get("transit");
|
||||
if (transitDirection.getRoutes() != null && !transitDirection.getRoutes().isEmpty()) {
|
||||
for (Route route : transitDirection.getRoutes()) {
|
||||
// TODO check if length toleratable - maybe move this check to the end
|
||||
|
||||
CartaTransitRouteProcessor routeProcessor = new CartaTransitRouteProcessor(route);
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
routeProcessor.getFirstStopName(),
|
||||
route.getSourceCoordinate().getLatitude(),
|
||||
route.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
routeProcessor.getLastStopName(),
|
||||
route.getTargetCoordinate().getLatitude(),
|
||||
route.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(route.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
routeProcessor.getSteps(),
|
||||
carCarbondDioxide - route.getEmission().getCarbonDioxide(),
|
||||
route.getDuration() - carRoute.getDuration(),
|
||||
route.getEmission().getCarbonDioxide()
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO if consider weather
|
||||
// get weather for origin
|
||||
// if bad, remove walking and biking options
|
||||
@@ -10,5 +10,19 @@ public enum Modality {
|
||||
TRAIN,
|
||||
CAR,
|
||||
MOTORCYCLE,
|
||||
TAXI
|
||||
TAXI;
|
||||
|
||||
public static Modality fromCartaString(String cartaType) {
|
||||
return switch (cartaType.toLowerCase()) {
|
||||
case "tram", "subway" -> TRAM;
|
||||
case "rail", "monorail" -> TRAIN;
|
||||
case "bus", "trolleybus" -> BUS;
|
||||
case "ferry", "cablecar", "gondola", "funicular" ->
|
||||
throw new UnsupportedOperationException("Transport type needs to be added to backend: " + cartaType);
|
||||
// these are to dervive modality from carta step type
|
||||
case "walkingstep" -> WALKING;
|
||||
case "cyclingstep" -> BICYCLE;
|
||||
default -> throw new IllegalArgumentException("Unknown CARTA modality: " + cartaType);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user