Skip to content

Commit

Permalink
Merge pull request #2937 from entur/otp2_cleanup_routing_service
Browse files Browse the repository at this point in the history
SW_2 - Otp2 cleanup routing service
  • Loading branch information
t2gran committed Feb 5, 2020
2 parents 1c28caf + e3989e5 commit afe6da4
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@
import org.apache.commons.lang3.StringUtils;
import org.opentripplanner.api.common.Message;
import org.opentripplanner.api.common.ParameterException;
import org.opentripplanner.api.model.Itinerary;
import org.opentripplanner.api.model.Place;
import org.opentripplanner.api.model.TripPlan;
import org.opentripplanner.api.model.error.PlannerError;
import org.opentripplanner.api.parameter.QualifiedModeSet;
import org.opentripplanner.api.resource.DebugOutput;
import org.opentripplanner.api.resource.GraphPathToTripPlanConverter;
import org.opentripplanner.common.geometry.SphericalDistanceLibrary;
import org.opentripplanner.common.model.GenericLocation;
import org.opentripplanner.ext.transmodelapi.mapping.TransmodelMappingUtil;
import org.opentripplanner.model.FeedScopedId;
import org.opentripplanner.routing.algorithm.raptor.router.RaptorRouter;
import org.opentripplanner.routing.algorithm.RoutingWorker;
import org.opentripplanner.routing.algorithm.mapping.TripPlanMapper;
import org.opentripplanner.routing.core.OptimizeType;
import org.opentripplanner.routing.core.RoutingRequest;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.error.PathNotFoundException;
import org.opentripplanner.routing.graph.GraphIndex;
import org.opentripplanner.routing.graph.Vertex;
import org.opentripplanner.routing.impl.GraphPathFinder;
import org.opentripplanner.routing.request.BannedStopSet;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.routing.vertextype.TransitStopVertex;
import org.opentripplanner.standalone.server.Router;
import org.slf4j.Logger;
Expand All @@ -36,7 +30,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -62,55 +55,25 @@ public Map<String, Object> plan(DataFetchingEnvironment environment) {
Router router = environment.getContext();
RoutingRequest request = createRequest(environment);

TripPlan plan = new TripPlan(
new Place(request.from.lng, request.from.lat, request.getFromPlace().name),
new Place(request.to.lng, request.to.lat, request.getToPlace().name),
request.getDateTime());
TripPlan plan;
List<Message> messages = new ArrayList<>();
DebugOutput debugOutput = new DebugOutput();

try {
List<Itinerary> itineraries = new ArrayList<>();

// TODO This currently only calculates the distances between the first fromVertex
// and the first toVertex
if (request.modes.getNonTransitSet().isValid()) {
double distance = SphericalDistanceLibrary.distance(
request.rctx.fromVertices.iterator().next().getCoordinate(),
request.rctx.toVertices.iterator().next().getCoordinate()
);
double limit = request.maxWalkDistance * 2;
// Handle int overflow, in which case the multiplication will be less than zero
if (limit < 0 || distance < limit) {
itineraries.addAll(findNonTransitItineraries(request, router));
}
}


if (request.modes.isTransit()) {
RaptorRouter raptorRouter = new RaptorRouter(request, router.graph.getRealtimeTransitLayer());
itineraries.addAll(raptorRouter.route());
}

if (itineraries.isEmpty()) {
throw new PathNotFoundException();
}

plan = createTripPlan(request, itineraries);
} catch (Exception e) {
RoutingWorker worker = new RoutingWorker(request);
plan = worker.route(router);
}
catch (Exception e) {
plan = TripPlanMapper.mapTripPlan(request, Collections.emptyList());
PlannerError error = new PlannerError(e);
if (!PlannerError.isPlanningError(e.getClass()))
LOG.warn("Error while planning path: ", e);
messages.add(error.message);
} finally {
if (request != null) {
if (request.rctx != null) {
debugOutput = request.rctx.debugOutput;
}
request.cleanup(); // TODO verify that this cleanup step is being done on Analyst web services
if (request.rctx != null) {
debugOutput = request.rctx.debugOutput;
}
}

return ImmutableMap.<String, Object>builder()
.put("plan", plan)
.put("messages", messages)
Expand Down Expand Up @@ -373,37 +336,4 @@ public static boolean hasArgument(DataFetchingEnvironment environment, String na
public static <T> boolean hasArgument(Map<String, T> m, String name) {
return m.containsKey(name) && m.get(name) != null;
}

private List<Itinerary> findNonTransitItineraries(RoutingRequest request, Router router) {
RoutingRequest nonTransitRequest = request.clone();
nonTransitRequest.modes.setTransit(false);

try {
// we could also get a persistent router-scoped GraphPathFinder but there's no setup cost here
GraphPathFinder gpFinder = new GraphPathFinder(router);
List<GraphPath> paths = gpFinder.graphPathFinderEntryPoint(nonTransitRequest);

/* Convert the internal GraphPaths to a TripPlan object that is included in an OTP web service Response. */
TripPlan plan = GraphPathToTripPlanConverter.generatePlan(paths, request);
return plan.itinerary;
} catch (PathNotFoundException e) {
return Collections.emptyList();
}
}

private TripPlan createTripPlan(RoutingRequest request, List<Itinerary> itineraries) {
Place from = new Place();
Place to = new Place();
if (!itineraries.isEmpty()) {
from = itineraries.get(0).legs.get(0).from;
to = itineraries.get(0).legs.get(itineraries.get(0).legs.size() - 1).to;
}
TripPlan tripPlan = new TripPlan(from, to, request.getDateTime());
itineraries = itineraries.stream().sorted(Comparator.comparing(i -> i.endTime))
.limit(request.numItineraries).collect(Collectors.toList());
tripPlan.itinerary = itineraries;
LOG.info("Returning {} itineraries", itineraries.size());
return tripPlan;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

import org.glassfish.grizzly.http.server.Request;
import org.opentripplanner.api.common.RoutingResource;
import org.opentripplanner.api.model.Itinerary;
import org.opentripplanner.api.model.Place;
import org.opentripplanner.api.model.TripPlan;
import org.opentripplanner.api.model.error.PlannerError;
import org.opentripplanner.common.geometry.SphericalDistanceLibrary;
import org.opentripplanner.routing.algorithm.raptor.router.RaptorRouter;
import org.opentripplanner.routing.algorithm.RoutingWorker;
import org.opentripplanner.routing.core.RoutingRequest;
import org.opentripplanner.routing.error.PathNotFoundException;
import org.opentripplanner.routing.impl.GraphPathFinder;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.standalone.server.Router;
import org.slf4j.Logger;
Expand All @@ -25,11 +20,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static org.opentripplanner.api.resource.ServerInfo.Q;

Expand Down Expand Up @@ -73,41 +64,18 @@ public Response plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest)
router = otpServer.getRouter(null);
request.setRoutingContext(router.graph);

List<Itinerary> itineraries = new ArrayList<>();
RoutingWorker worker = new RoutingWorker(request);

// TODO This currently only calculates the distances between the first fromVertex
// and the first toVertex
if (request.modes.getNonTransitSet().isValid()) {
double distance = SphericalDistanceLibrary.distance(
request.rctx.fromVertices.iterator().next().getCoordinate(),
request.rctx.toVertices.iterator().next().getCoordinate()
);
double limit = request.maxWalkDistance * 2;
// Handle int overflow, in which case the multiplication will be less than zero
if (limit < 0 || distance < limit) {
itineraries.addAll(findNonTransitItineraries(request, router));
}
}
TripPlan tripPlan = worker.route(router);

if (request.modes.isTransit()) {
// Route on realtime data, and compare with scheduled data while converting to an Itinerary.
RaptorRouter raptorRouter = new RaptorRouter(request, router.graph.getRealtimeTransitLayer());
itineraries.addAll(raptorRouter.route());
}

if (itineraries.isEmpty()) {
throw new PathNotFoundException();
}

TripPlan tripPlan = createTripPlan(request, itineraries);
response.setPlan(tripPlan);

/* Populate up the elevation metadata */
response.elevationMetadata = new ElevationMetadata();
response.elevationMetadata.ellipsoidToGeoidDifference = router.graph.ellipsoidToGeoidDifference;
response.elevationMetadata.geoidElevation = request.geoidElevation;

} catch (Exception e) {
}
catch (Exception e) {
PlannerError error = new PlannerError(e);
if(!PlannerError.isPlanningError(e.getClass()))
LOG.warn("Error while planning path: ", e);
Expand All @@ -117,7 +85,6 @@ public Response plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest)
if (request.rctx != null) {
response.debugOutput = request.rctx.debugOutput;
}
request.cleanup(); // TODO verify that this cleanup step is being done on Analyst web services
}
}

Expand Down Expand Up @@ -153,36 +120,4 @@ public Response plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest)

return response;
}

private List<Itinerary> findNonTransitItineraries(RoutingRequest request, Router router) {
RoutingRequest nonTransitRequest = request.clone();
nonTransitRequest.modes.setTransit(false);

try {
// we could also get a persistent router-scoped GraphPathFinder but there's no setup cost here
GraphPathFinder gpFinder = new GraphPathFinder(router);
List<GraphPath> paths = gpFinder.graphPathFinderEntryPoint(nonTransitRequest);

/* Convert the internal GraphPaths to a TripPlan object that is included in an OTP web service Response. */
TripPlan plan = GraphPathToTripPlanConverter.generatePlan(paths, request);
return plan.itinerary;
} catch (PathNotFoundException e) {
return Collections.emptyList();
}
}

private TripPlan createTripPlan(RoutingRequest request, List<Itinerary> itineraries) {
Place from = new Place();
Place to = new Place();
if (!itineraries.isEmpty()) {
from = itineraries.get(0).legs.get(0).from;
to = itineraries.get(0).legs.get(itineraries.get(0).legs.size() - 1).to;
}
TripPlan tripPlan = new TripPlan(from, to, request.getDateTime());
itineraries = itineraries.stream().sorted(Comparator.comparing(i -> i.endTime))
.limit(request.numItineraries).collect(Collectors.toList());
tripPlan.itinerary = itineraries;
LOG.info("Returning {} itineraries", itineraries.size());
return tripPlan;
}
}

0 comments on commit afe6da4

Please sign in to comment.