implement routing/drections endpoint for requesting routes without alternatives logic, includes refactoring of previous alternative routing logic into carta DirectionsService to reuse code
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
package SuMoC.backend.carta;
|
||||
|
||||
import SuMoC.backend.geocoding.AddressDTO;
|
||||
import SuMoC.backend.geocoding.GeoCodingService;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.routing.dtos.StepDTO;
|
||||
import SuMoC.backend.routing.exceptions.NoCarRouteFoundException;
|
||||
import SuMoC.backend.routing.exceptions.NoCarbonEmissionFoundException;
|
||||
import SuMoC.backend.routing.services.CartaTransitRouteProcessor;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
import sumoc.backend.carta.client.model.Direction;
|
||||
import sumoc.backend.carta.client.model.Route;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DirectionsService {
|
||||
|
||||
private final CartaClient cartaClient;
|
||||
|
||||
private final GeoCodingService geoCodingService;
|
||||
|
||||
public List<RouteDTO> getPossibleAlternativeRoutes(DirectionsRequest requestDTO) {
|
||||
return getPossibleRoutes(requestDTO, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns possible routes for the request - no sanity filtering regarding travel times etc. is applied.
|
||||
* TODO this does not handle the case where only departure time is provided
|
||||
* TODO this does not handle the case where origin/destination is provided as address only
|
||||
* TODO IF both arrival and departure are provided -> use that as baseline time
|
||||
*/
|
||||
public List<RouteDTO> getPossibleRoutes(DirectionsRequest requestDTO, boolean includeCarRoute) {
|
||||
assert requestDTO.getRoutingPreferencesDTO() != null;
|
||||
HashMap<String, Direction> directionMap = getPossibleDirections(requestDTO);
|
||||
ArrayList<RouteDTO> suggestedRoutes = new ArrayList<>();
|
||||
|
||||
// Fetch origin and destination addresses concurrently
|
||||
var addressesMono = Mono.zip(
|
||||
geoCodingService.getAddress(requestDTO.getOriginLat(), requestDTO.getOriginLon()),
|
||||
geoCodingService.getAddress(requestDTO.getDestinationLat(), requestDTO.getDestinationLon())
|
||||
);
|
||||
var addressesTuple = addressesMono.block();
|
||||
AddressDTO originAddress = addressesTuple.getT1();
|
||||
AddressDTO destinationAddress = addressesTuple.getT2();
|
||||
|
||||
// Setting up car data for baseline
|
||||
Route carRoute;
|
||||
try {
|
||||
carRoute = directionMap.get("car").getRoutes().getFirst();
|
||||
} catch (NullPointerException e) {
|
||||
throw new NoCarRouteFoundException("No car route to compare to was found.");
|
||||
}
|
||||
double carCarbondDioxide;
|
||||
try {
|
||||
carCarbondDioxide = carRoute.getEmission().getCarbonDioxide();
|
||||
} catch (NullPointerException e) {
|
||||
throw new NoCarbonEmissionFoundException("No carbon emission was provided for the car route.");
|
||||
}
|
||||
|
||||
if (includeCarRoute) {
|
||||
Direction carDirection = directionMap.get("car");
|
||||
if (carDirection.getRoutes() != null && !carDirection.getRoutes().isEmpty()) {
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.CAR,
|
||||
carRoute.getDistance()
|
||||
)
|
||||
);
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
carRoute.getSourceCoordinate().getLatitude(),
|
||||
carRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
carRoute.getTargetCoordinate().getLatitude(),
|
||||
carRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(carRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
0.0,
|
||||
0L,
|
||||
carCarbondDioxide
|
||||
);
|
||||
|
||||
suggestedRoutes.add(routeDTO);
|
||||
}
|
||||
}
|
||||
|
||||
if (directionMap.containsKey("bike")) {
|
||||
Direction bikeDirection = directionMap.get("bike");
|
||||
if (bikeDirection.getRoutes() != null && !bikeDirection.getRoutes().isEmpty()) {
|
||||
Route bikeRoute = bikeDirection.getRoutes().getFirst();
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.BICYCLE,
|
||||
bikeRoute.getDistance()
|
||||
)
|
||||
);
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
bikeRoute.getSourceCoordinate().getLatitude(),
|
||||
bikeRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
bikeRoute.getTargetCoordinate().getLatitude(),
|
||||
bikeRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(bikeRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
bikeRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedRoutes.add(routeDTO);
|
||||
}
|
||||
}
|
||||
|
||||
if (directionMap.containsKey("walk")) {
|
||||
Direction walkDirection = directionMap.get("walk");
|
||||
if (walkDirection.getRoutes() != null && !walkDirection.getRoutes().isEmpty()) {
|
||||
Route walkRoute = walkDirection.getRoutes().getFirst();
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.WALKING,
|
||||
walkRoute.getDistance()
|
||||
));
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
walkRoute.getSourceCoordinate().getLatitude(),
|
||||
walkRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
walkRoute.getTargetCoordinate().getLatitude(),
|
||||
walkRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(walkRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
walkRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedRoutes.add(routeDTO);
|
||||
}
|
||||
}
|
||||
|
||||
if (directionMap.containsKey("transit")) {
|
||||
Direction transitDirection = directionMap.get("transit");
|
||||
if (transitDirection.getRoutes() != null && !transitDirection.getRoutes().isEmpty()) {
|
||||
for (Route route : transitDirection.getRoutes()) {
|
||||
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()
|
||||
);
|
||||
suggestedRoutes.add(routeDTO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return suggestedRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes concurrent direction calls to CARTA for all modes requested + car as baseline.
|
||||
* Possible keys of the returned Map:
|
||||
* - car (always)
|
||||
* - walk (optional)
|
||||
* - bike (optional)
|
||||
* - transit (optional)
|
||||
*/
|
||||
private HashMap<String, Direction> getPossibleDirections(DirectionsRequest requestDTO) {
|
||||
Map<String, CompletableFuture<Direction>> futuresByMode = new HashMap<>();
|
||||
assert requestDTO.getRoutingPreferencesDTO() != null;
|
||||
|
||||
// serves as baseline
|
||||
futuresByMode.put("car", getCartaFuture(requestDTO, "driving", "", 1));
|
||||
// type string for carta api
|
||||
List<String> typeList = new ArrayList<>();
|
||||
// add request futures for all modes request by user + assemble type string for a possible transit directions request
|
||||
for (Modality modality : requestDTO.getRoutingPreferencesDTO().getModalitiesToConsider()) {
|
||||
switch (modality) {
|
||||
case WALKING -> futuresByMode.put("walk", getCartaFuture(requestDTO, "walking", "", 1));
|
||||
case BICYCLE -> futuresByMode.put("bike", getCartaFuture(requestDTO, "cycling", "", 1));
|
||||
case BUS -> {
|
||||
typeList.add("3"); // bus
|
||||
typeList.add("8"); // trolleybus
|
||||
}
|
||||
// case ESCOOTER -> typeList.add("X"); TODO - check if this and bike should be included in transit or separate
|
||||
case TRAM -> typeList.add("0");
|
||||
case TRAIN -> {
|
||||
typeList.add("1"); // subway
|
||||
typeList.add("2"); // rail
|
||||
typeList.add("9"); // monorail
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!typeList.isEmpty()) { // if any public transit the app allows toggling is selcted we add the funny transit types and make the transit request
|
||||
typeList.add("4"); // Ferry
|
||||
typeList.add("5"); // cablecar
|
||||
typeList.add("6"); // gondola
|
||||
typeList.add("7"); // funicular
|
||||
String types = String.join(",", typeList);
|
||||
futuresByMode.put("transit", getCartaFuture(requestDTO, "transit", types, 3));
|
||||
}
|
||||
|
||||
// return all future results as list when resvoled
|
||||
HashMap<String, Direction> directions = new HashMap<>();
|
||||
for (Map.Entry<String, CompletableFuture<Direction>> entry : futuresByMode.entrySet()) {
|
||||
directions.put(entry.getKey(), entry.getValue().join());
|
||||
}
|
||||
return directions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an individual call to the CARTA API and returns a direction future.
|
||||
*/
|
||||
private CompletableFuture<Direction> getCartaFuture(
|
||||
DirectionsRequest requestDTO,
|
||||
String mode,
|
||||
String types,
|
||||
int alternatives
|
||||
) {
|
||||
return cartaClient.asyncGetRoute(
|
||||
requestDTO.getOriginLat() + "," + requestDTO.getOriginLon(),
|
||||
requestDTO.getDestinationLat() + "," + requestDTO.getDestinationLon(),
|
||||
mode,
|
||||
requestDTO.getArrivalTime(),
|
||||
null,
|
||||
alternatives, // alternatives
|
||||
null, // walkingSpeed
|
||||
null, // transferTime TODO what is default? adapt?
|
||||
types,
|
||||
null, // range
|
||||
requestDTO.getOriginLat() + "," + requestDTO.getOriginLon()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user