use of org.opentripplanner.standalone.server.Router in project OpenTripPlanner by opentripplanner.
the class TransmodelGraphQLPlanner method createRequest.
private RoutingRequest createRequest(DataFetchingEnvironment environment) {
TransmodelRequestContext context = environment.getContext();
Router router = context.getRouter();
RoutingRequest request = router.defaultRoutingRequest.clone();
DataFetcherDecorator callWith = new DataFetcherDecorator(environment);
callWith.argument("locale", (String v) -> request.locale = Locale.forLanguageTag(v));
callWith.argument("from", (Map<String, Object> v) -> request.from = toGenericLocation(v));
callWith.argument("to", (Map<String, Object> v) -> request.to = toGenericLocation(v));
callWith.argument("dateTime", millisSinceEpoch -> request.setDateTime(new Date((long) millisSinceEpoch)), Date::new);
callWith.argument("searchWindow", (Integer m) -> request.searchWindow = Duration.ofMinutes(m));
callWith.argument("wheelchair", request::setWheelchairAccessible);
callWith.argument("numTripPatterns", request::setNumItineraries);
callWith.argument("transitGeneralizedCostLimit", (DoubleFunction<Double> it) -> request.transitGeneralizedCostLimit = it);
callWith.argument("maximumWalkDistance", request::setMaxWalkDistance);
// callWith.argument("maxTransferWalkDistance", request::setMaxTransferWalkDistance);
callWith.argument("maxPreTransitTime", request::setMaxPreTransitTime);
// callWith.argument("preTransitReluctance", (Double v) -> request.setPreTransitReluctance(v));
// callWith.argument("maxPreTransitWalkDistance", (Double v) -> request.setMaxPreTransitWalkDistance(v));
callWith.argument("walkBoardCost", request::setWalkBoardCost);
callWith.argument("walkReluctance", request::setWalkReluctance);
callWith.argument("waitReluctance", request::setWaitReluctance);
callWith.argument("walkBoardCost", request::setWalkBoardCost);
// callWith.argument("walkOnStreetReluctance", request::setWalkOnStreetReluctance);
callWith.argument("waitReluctance", request::setWaitReluctance);
callWith.argument("waitAtBeginningFactor", request::setWaitAtBeginningFactor);
callWith.argument("walkSpeed", (Double v) -> request.walkSpeed = v);
callWith.argument("bikeSpeed", (Double v) -> request.bikeSpeed = v);
callWith.argument("bikeSwitchTime", (Integer v) -> request.bikeSwitchTime = v);
callWith.argument("bikeSwitchCost", (Integer v) -> request.bikeSwitchCost = v);
// callWith.argument("transitDistanceReluctance", (Double v) -> request.transitDistanceReluctance = v);
BicycleOptimizeType optimize = environment.getArgument("optimize");
if (optimize == BicycleOptimizeType.TRIANGLE) {
try {
RoutingRequest.assertTriangleParameters(request.bikeTriangleSafetyFactor, request.bikeTriangleTimeFactor, request.bikeTriangleSlopeFactor);
callWith.argument("triangle.safetyFactor", request::setBikeTriangleSafetyFactor);
callWith.argument("triangle.slopeFactor", request::setBikeTriangleSlopeFactor);
callWith.argument("triangle.timeFactor", request::setBikeTriangleTimeFactor);
} catch (ParameterException e) {
throw new RuntimeException(e);
}
}
callWith.argument("arriveBy", request::setArriveBy);
request.showIntermediateStops = true;
callWith.argument("vias", (List<Map<String, Object>> v) -> request.intermediatePlaces = v.stream().map(this::toGenericLocation).collect(Collectors.toList()));
callWith.argument("preferred.authorities", (Collection<String> authorities) -> request.setPreferredAgencies(mapIDsToDomain(authorities)));
callWith.argument("unpreferred.authorities", (Collection<String> authorities) -> request.setUnpreferredAgencies(mapIDsToDomain(authorities)));
callWith.argument("whiteListed.authorities", (Collection<String> authorities) -> request.setWhiteListedAgencies(mapIDsToDomain(authorities)));
callWith.argument("banned.authorities", (Collection<String> authorities) -> request.setBannedAgencies(mapIDsToDomain(authorities)));
callWith.argument("preferred.otherThanPreferredLinesPenalty", request::setOtherThanPreferredRoutesPenalty);
callWith.argument("preferred.lines", (List<String> lines) -> request.setPreferredRoutes(mapIDsToDomain(lines)));
callWith.argument("unpreferred.lines", (List<String> lines) -> request.setUnpreferredRoutes(mapIDsToDomain(lines)));
callWith.argument("whiteListed.lines", (List<String> lines) -> request.setWhiteListedRoutes(mapIDsToDomain(lines)));
callWith.argument("banned.lines", (List<String> lines) -> request.setBannedRoutes(mapIDsToDomain(lines)));
callWith.argument("banned.serviceJourneys", (Collection<String> serviceJourneys) -> request.bannedTrips = toBannedTrips(serviceJourneys));
// callWith.argument("banned.quays", quays -> request.setBannedStops(mappingUtil.prepareListOfFeedScopedId((List<String>) quays)));
// callWith.argument("banned.quaysHard", quaysHard -> request.setBannedStopsHard(mappingUtil.prepareListOfFeedScopedId((List<String>) quaysHard)));
// callWith.argument("heuristicStepsPerMainStep", (Integer v) -> request.heuristicStepsPerMainStep = v);
// callWith.argument("compactLegsByReversedSearch", (Boolean v) -> { /* not used any more */ });
// callWith.argument("banFirstServiceJourneysFromReuseNo", (Integer v) -> request.banFirstTripsFromReuseNo = v);
callWith.argument("allowBikeRental", (Boolean v) -> request.bikeRental = v);
callWith.argument("debugItineraryFilter", (Boolean v) -> request.debugItineraryFilter = v);
callWith.argument("transferPenalty", (Integer v) -> request.transferCost = v);
if (optimize == BicycleOptimizeType.TRANSFERS) {
optimize = BicycleOptimizeType.QUICK;
request.transferCost += 1800;
}
if (optimize != null) {
request.optimize = optimize;
}
if (GqlUtil.hasArgument(environment, "modes")) {
ElementWrapper<StreetMode> accessMode = new ElementWrapper<>();
ElementWrapper<StreetMode> egressMode = new ElementWrapper<>();
ElementWrapper<StreetMode> directMode = new ElementWrapper<>();
ElementWrapper<ArrayList<TransitMode>> transitModes = new ElementWrapper<>();
callWith.argument("modes.accessMode", accessMode::set);
callWith.argument("modes.egressMode", egressMode::set);
callWith.argument("modes.directMode", directMode::set);
callWith.argument("modes.transportMode", transitModes::set);
if (transitModes.get() == null) {
// Default to all transport modes if transport modes not specified
transitModes.set(new ArrayList<>(Arrays.asList(TransitMode.values())));
}
request.modes = new RequestModes(accessMode.get(), egressMode.get(), directMode.get(), new HashSet<>(transitModes.get()));
}
if (request.bikeRental && !GqlUtil.hasArgument(environment, "bikeSpeed")) {
// slower bike speed for bike sharing, based on empirical evidence from DC.
request.bikeSpeed = 4.3;
}
callWith.argument("minimumTransferTime", (Integer v) -> request.transferSlack = v);
callWith.argument("transferSlack", (Integer v) -> request.transferSlack = v);
callWith.argument("boardSlackDefault", (Integer v) -> request.boardSlack = v);
callWith.argument("boardSlackList", (Object v) -> request.boardSlackForMode = TransportModeSlack.mapToDomain(v));
callWith.argument("alightSlackDefault", (Integer v) -> request.alightSlack = v);
callWith.argument("alightSlackList", (Object v) -> request.alightSlackForMode = TransportModeSlack.mapToDomain(v));
callWith.argument("maximumTransfers", (Integer v) -> request.maxTransfers = v);
final long NOW_THRESHOLD_MILLIS = 15 * 60 * 60 * 1000;
boolean tripPlannedForNow = Math.abs(request.getDateTime().getTime() - new Date().getTime()) < NOW_THRESHOLD_MILLIS;
// TODO the same thing for GTFS-RT
request.useBikeRentalAvailabilityInformation = (tripPlannedForNow);
callWith.argument("ignoreRealtimeUpdates", (Boolean v) -> request.ignoreRealtimeUpdates = v);
return request;
}
use of org.opentripplanner.standalone.server.Router in project OpenTripPlanner by opentripplanner.
the class BikeRental method getBikeRentalStations.
@GET
@Produces(MediaType.APPLICATION_JSON)
public BikeRentalStationList getBikeRentalStations(@QueryParam("lowerLeft") String lowerLeft, @QueryParam("upperRight") String upperRight, @QueryParam("locale") String locale_param) {
Router router = otpServer.getRouter();
BikeRentalStationService bikeRentalService = router.graph.getService(BikeRentalStationService.class);
Locale locale;
locale = ResourceBundleSingleton.INSTANCE.getLocale(locale_param);
if (bikeRentalService == null)
return new BikeRentalStationList();
Envelope envelope;
if (lowerLeft != null) {
envelope = getEnvelope(lowerLeft, upperRight);
} else {
envelope = new Envelope(-180, 180, -90, 90);
}
Collection<BikeRentalStation> stations = bikeRentalService.getBikeRentalStations();
List<BikeRentalStation> out = new ArrayList<>();
for (BikeRentalStation station : stations) {
if (envelope.contains(station.x, station.y)) {
BikeRentalStation station_localized = station.clone();
station_localized.locale = locale;
out.add(station_localized);
}
}
BikeRentalStationList brsl = new BikeRentalStationList();
brsl.stations = out;
return brsl;
}
use of org.opentripplanner.standalone.server.Router in project OpenTripPlanner by opentripplanner.
the class PlannerResource method plan.
// We inject info about the incoming request so we can include the incoming query
// parameters in the outgoing response. This is a TriMet requirement.
// Jersey uses @Context to inject internal types and @InjectParam or @Resource for DI objects.
@GET
@Produces(MediaType.APPLICATION_JSON)
public TripPlannerResponse plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest) {
/*
* TODO: add Lang / Locale parameter, and thus get localized content (Messages & more...)
* TODO: from/to inputs should be converted / geocoded / etc... here, and maybe send coords
* or vertex ids to planner (or error back to user)
* TODO: org.opentripplanner.routing.module.PathServiceImpl has COOORD parsing. Abstract that
* out so it's used here too...
*/
// Create response object, containing a copy of all request parameters. Maybe they should be in the debug section of the response.
TripPlannerResponse response = new TripPlannerResponse(uriInfo);
RoutingRequest request = null;
Router router = null;
RoutingResponse res = null;
try {
/* Fill in request fields from query parameters via shared superclass method, catching any errors. */
request = super.buildRequest();
router = otpServer.getRouter();
// Route
RoutingService routingService = new RoutingService(router.graph);
res = routingService.route(request, router);
// Map to API
TripPlanMapper tripPlanMapper = new TripPlanMapper(request.locale);
response.setPlan(tripPlanMapper.mapTripPlan(res.getTripPlan()));
response.setMetadata(TripSearchMetadataMapper.mapTripSearchMetadata(res.getMetadata()));
if (!res.getRoutingErrors().isEmpty()) {
// The api can only return one error message, so the first one is mapped
response.setError(PlannerErrorMapper.mapMessage(res.getRoutingErrors().get(0)));
}
/* Populate up the elevation metadata */
response.elevationMetadata = new ElevationMetadata();
response.elevationMetadata.ellipsoidToGeoidDifference = router.graph.ellipsoidToGeoidDifference;
response.elevationMetadata.geoidElevation = request.geoidElevation;
response.debugOutput = res.getDebugAggregator().finishedRendering();
} catch (Exception e) {
LOG.error("System error", e);
PlannerError error = new PlannerError();
error.setMsg(Message.SYSTEM_ERROR);
response.setError(error);
}
/* Log this request if such logging is enabled. */
if (request != null && router != null && router.requestLogger != null) {
StringBuilder sb = new StringBuilder();
String clientIpAddress = grizzlyRequest.getRemoteAddr();
// sb.append(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
sb.append(clientIpAddress);
sb.append(' ');
sb.append(request.arriveBy ? "ARRIVE" : "DEPART");
sb.append(' ');
sb.append(LocalDateTime.ofInstant(Instant.ofEpochSecond(request.dateTime), ZoneId.systemDefault()));
sb.append(' ');
sb.append(request.streetSubRequestModes.getAsStr());
sb.append(' ');
sb.append(request.from.lat);
sb.append(' ');
sb.append(request.from.lng);
sb.append(' ');
sb.append(request.to.lat);
sb.append(' ');
sb.append(request.to.lng);
sb.append(' ');
if (res != null) {
for (Itinerary it : res.getTripPlan().itineraries) {
sb.append(it.durationSeconds);
sb.append(' ');
}
}
router.requestLogger.info(sb.toString());
}
return response;
}
use of org.opentripplanner.standalone.server.Router in project OpenTripPlanner by opentripplanner.
the class OTPMain method startOTPServer.
/**
* All startup logic is in an instance method instead of the static main method so it is possible to build graphs
* from web services or scripts, not just from the command line. If options cause an OTP API server to start up,
* this method will return when the web server shuts down.
*
* @throws RuntimeException if an error occurs while loading the graph.
*/
private static void startOTPServer(CommandLineParameters params) {
LOG.info("Searching for configuration and input files in {}", params.getBaseDirectory().getAbsolutePath());
Graph graph = null;
OTPAppConstruction app = new OTPAppConstruction(params);
// Validate data sources, command line arguments and config before loading and
// processing input data to fail early
app.validateConfigAndDataSources();
/* Load graph from disk if one is not present from build. */
if (params.doLoadGraph() || params.doLoadStreetGraph()) {
DataSource inputGraph = params.doLoadGraph() ? app.store().getGraph() : app.store().getStreetGraph();
SerializedGraphObject obj = SerializedGraphObject.load(inputGraph);
graph = obj.graph;
app.config().updateConfigFromSerializedGraph(obj.buildConfig, obj.routerConfig);
}
/* Start graph builder if requested. */
if (params.doBuildStreet() || params.doBuildTransit()) {
// Abort building a graph if the file can not be saved
SerializedGraphObject.verifyTheOutputGraphIsWritableIfDataSourceExist(app.graphOutputDataSource());
GraphBuilder graphBuilder = app.createGraphBuilder(graph);
if (graphBuilder != null) {
graphBuilder.run();
// Hand off the graph to the server as the default graph
graph = graphBuilder.getGraph();
} else {
throw new IllegalStateException("An error occurred while building the graph.");
}
// Store graph and config used to build it, also store router-config for easy deployment
// with using the embedded router config.
new SerializedGraphObject(graph, app.config().buildConfig(), app.config().routerConfig()).save(app.graphOutputDataSource());
}
if (graph == null) {
LOG.error("Nothing to do, no graph loaded or build. Exiting.");
System.exit(101);
}
if (!params.doServe()) {
LOG.info("Done building graph. Exiting.");
return;
}
// Index graph for travel search
graph.index();
Router router = new Router(graph, app.config().routerConfig());
router.startup();
/* Start visualizer if requested. */
if (params.visualize) {
router.graphVisualizer = new GraphVisualizer(router);
router.graphVisualizer.run();
}
// However, currently the server runs in a blocking way and waits for shutdown, so has to run last.
if (params.doServe()) {
GrizzlyServer grizzlyServer = app.createGrizzlyServer(router);
// Loop to restart server on uncaught fatal exceptions.
while (true) {
try {
grizzlyServer.run();
return;
} catch (Throwable throwable) {
LOG.error("An uncaught error occurred inside OTP. Restarting server. Error was: {}", ThrowableUtils.detailedString(throwable));
}
}
}
}
use of org.opentripplanner.standalone.server.Router in project OpenTripPlanner by opentripplanner.
the class GraphPathFinder method getPaths.
/**
* This no longer does "trip banning" to find multiple itineraries.
* It just searches once trying to find a non-transit path.
*/
public List<GraphPath> getPaths(RoutingRequest options) {
if (options == null) {
LOG.error("PathService was passed a null routing request.");
return null;
}
if (options.streetSubRequestModes.isTransit()) {
throw new UnsupportedOperationException("Transit search not supported");
}
// Reuse one instance of AStar for all N requests, which are carried out sequentially
AStar aStar = new AStar();
if (options.rctx == null) {
options.setRoutingContext(router.graph);
// The special long-distance heuristic should be sufficient to constrain the search to the right area.
}
// If this Router has a GraphVisualizer attached to it, set it as a callback for the AStar search
if (router.graphVisualizer != null) {
aStar.setTraverseVisitor(router.graphVisualizer.traverseVisitor);
// options.disableRemainingWeightHeuristic = true; // DEBUG
}
// FORCING the dominance function to weight only
options.dominanceFunction = new DominanceFunction.MinimumWeight();
LOG.debug("rreq={}", options);
// Choose an appropriate heuristic for goal direction.
RemainingWeightHeuristic heuristic;
if (options.disableRemainingWeightHeuristic || options.oneToMany) {
heuristic = new TrivialRemainingWeightHeuristic();
} else {
heuristic = new EuclideanRemainingWeightHeuristic();
}
options.rctx.remainingWeightHeuristic = heuristic;
/* maxWalk has a different meaning than it used to. It's the radius around the origin or destination within
* which you can walk on the streets. An unlimited value would cause the bidi heuristic to do unbounded street
* searches and consider the whole graph walkable.
*
* After the limited areas of the street network around the origin and destination are explored, the
* options.maxWalkDistance will be set to unlimited for similar reasons to maxTransfers above. That happens
* in method org.opentripplanner.routing.algorithm.astar.strategies.InterleavedBidirectionalHeuristic.initialize
*/
if (options.maxWalkDistance == Double.MAX_VALUE)
options.maxWalkDistance = DEFAULT_MAX_WALK;
if (options.maxWalkDistance > CLAMP_MAX_WALK)
options.maxWalkDistance = CLAMP_MAX_WALK;
long searchBeginTime = System.currentTimeMillis();
LOG.debug("BEGIN SEARCH");
double timeout = searchBeginTime + router.streetRoutingTimeoutSeconds() * 1000;
// Convert from absolute to relative time
timeout -= System.currentTimeMillis();
// Convert milliseconds to seconds
timeout /= 1000;
if (timeout <= 0) {
// Catch the case where advancing to the next (lower) timeout value means the search is timed out
// before it even begins. Passing a negative relative timeout in the SPT call would mean "no timeout".
options.rctx.aborted = true;
return null;
}
// Don't dig through the SPT object, just ask the A star algorithm for the states that reached the target.
aStar.getShortestPathTree(options, timeout);
List<GraphPath> paths = aStar.getPathsToTarget().stream().filter(path -> {
double duration = options.useRequestedDateTimeInMaxHours ? options.arriveBy ? options.dateTime - path.getStartTime() : path.getEndTime() - options.dateTime : path.getDuration();
return duration < options.maxHours * 60 * 60;
}).collect(Collectors.toList());
LOG.debug("we have {} paths", paths.size());
LOG.debug("END SEARCH ({} msec)", System.currentTimeMillis() - searchBeginTime);
Collections.sort(paths, options.getPathComparator(options.arriveBy));
return paths;
}
Aggregations