Search in sources :

Example 1 with TripPlan

use of org.opentripplanner.api.model.TripPlan 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, MediaType.APPLICATION_XML + Q, MediaType.TEXT_XML + Q })
public Response 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.
    Response response = new Response(uriInfo);
    RoutingRequest request = null;
    Router router = null;
    List<GraphPath> paths = null;
    try {
        /* Fill in request fields from query parameters via shared superclass method, catching any errors. */
        request = super.buildRequest();
        router = otpServer.getRouter(request.routerId);
        /* Find some good GraphPaths through the OTP Graph. */
        // we could also get a persistent router-scoped GraphPathFinder but there's no setup cost here
        GraphPathFinder gpFinder = new GraphPathFinder(router);
        paths = gpFinder.graphPathFinderEntryPoint(request);
        /* Convert the internal GraphPaths to a TripPlan object that is included in an OTP web service Response. */
        TripPlan plan = GraphPathToTripPlanConverter.generatePlan(paths, request);
        response.setPlan(plan);
    } catch (Exception e) {
        PlannerError error = new PlannerError(e);
        if (!PlannerError.isPlanningError(e.getClass()))
            LOG.warn("Error while planning path: ", e);
        response.setError(error);
    } finally {
        if (request != null) {
            if (request.rctx != null) {
                response.debugOutput = request.rctx.debugOutput;
            }
            // TODO verify that this cleanup step is being done on Analyst web services
            request.cleanup();
        }
    }
    /* Populate up the elevation metadata */
    response.elevationMetadata = new ElevationMetadata();
    response.elevationMetadata.ellipsoidToGeoidDifference = router.graph.ellipsoidToGeoidDifference;
    response.elevationMetadata.geoidElevation = request.geoidElevation;
    /* 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.modes.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 (paths != null) {
            for (GraphPath path : paths) {
                sb.append(path.getDuration());
                sb.append(' ');
                sb.append(path.getTrips().size());
                sb.append(' ');
            }
        }
        router.requestLogger.info(sb.toString());
    }
    return response;
}
Also used : GraphPath(org.opentripplanner.routing.spt.GraphPath) TripPlan(org.opentripplanner.api.model.TripPlan) Router(org.opentripplanner.standalone.Router) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder) PlannerError(org.opentripplanner.api.model.error.PlannerError) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with TripPlan

use of org.opentripplanner.api.model.TripPlan in project OpenTripPlanner by opentripplanner.

the class TestIntermediatePlaces method handleRequest.

private void handleRequest(GenericLocation from, GenericLocation to, GenericLocation[] via, String modes, boolean arriveBy) {
    RoutingRequest request = new RoutingRequest(modes);
    request.setDateTime("2016-04-20", "13:00", timeZone);
    request.setArriveBy(arriveBy);
    request.from = from;
    request.to = to;
    for (GenericLocation intermediateLocation : via) {
        request.addIntermediatePlace(intermediateLocation);
    }
    List<GraphPath> pathList = graphPathFinder.graphPathFinderEntryPoint(request);
    assertNotNull(pathList);
    assertFalse(pathList.isEmpty());
    TripPlan plan = GraphPathToTripPlanConverter.generatePlan(pathList, request);
    assertLocationIsVeryCloseToPlace(from, plan.from);
    assertLocationIsVeryCloseToPlace(to, plan.to);
    assertTrue(1 <= plan.itinerary.size());
    for (Itinerary itinerary : plan.itinerary) {
        validateIntermediatePlacesVisited(itinerary, via);
        assertTrue(via.length < itinerary.legs.size());
        validateLegsTemporally(request, itinerary);
        validateLegsSpatially(plan, itinerary);
        if (modes.contains("TRANSIT")) {
            assert itinerary.transitTime > 0;
        }
    }
}
Also used : GraphPath(org.opentripplanner.routing.spt.GraphPath) TripPlan(org.opentripplanner.api.model.TripPlan) Itinerary(org.opentripplanner.api.model.Itinerary) GenericLocation(org.opentripplanner.common.model.GenericLocation) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest)

Example 3 with TripPlan

use of org.opentripplanner.api.model.TripPlan in project OpenTripPlanner by opentripplanner.

the class GtfsTest method plan.

public Leg[] plan(long dateTime, String fromVertex, String toVertex, String onTripId, boolean wheelchairAccessible, boolean preferLeastTransfers, TraverseMode preferredMode, String excludedRoute, String excludedStop, int legCount) {
    final TraverseMode mode = preferredMode != null ? preferredMode : TraverseMode.TRANSIT;
    RoutingRequest routingRequest = new RoutingRequest();
    routingRequest.setNumItineraries(1);
    routingRequest.setArriveBy(dateTime < 0);
    routingRequest.dateTime = Math.abs(dateTime);
    if (fromVertex != null && !fromVertex.isEmpty()) {
        routingRequest.from = (new GenericLocation(null, feedId.getId() + ":" + fromVertex));
    }
    if (toVertex != null && !toVertex.isEmpty()) {
        routingRequest.to = new GenericLocation(null, feedId.getId() + ":" + toVertex);
    }
    if (onTripId != null && !onTripId.isEmpty()) {
        routingRequest.startingTransitTripId = (new AgencyAndId(feedId.getId(), onTripId));
    }
    routingRequest.setRoutingContext(graph);
    routingRequest.setWheelchairAccessible(wheelchairAccessible);
    routingRequest.transferPenalty = (preferLeastTransfers ? 300 : 0);
    routingRequest.setModes(new TraverseModeSet(TraverseMode.WALK, mode));
    // TODO route matcher still using underscores because it's quite nonstandard and should be eliminated from the 1.0 release rather than reworked
    if (excludedRoute != null && !excludedRoute.isEmpty()) {
        routingRequest.setBannedRoutes(feedId.getId() + "__" + excludedRoute);
    }
    if (excludedStop != null && !excludedStop.isEmpty()) {
        routingRequest.setBannedStopsHard(feedId.getId() + ":" + excludedStop);
    }
    routingRequest.setOtherThanPreferredRoutesPenalty(0);
    // The walk board cost is set low because it interferes with test 2c1.
    // As long as boarding has a very low cost, waiting should not be "better" than riding
    // since this makes interlining _worse_ than alighting and re-boarding the same line.
    // TODO rethink whether it makes sense to weight waiting to board _less_ than 1.
    routingRequest.setWaitReluctance(1);
    routingRequest.setWalkBoardCost(30);
    List<GraphPath> paths = new GraphPathFinder(router).getPaths(routingRequest);
    TripPlan tripPlan = GraphPathToTripPlanConverter.generatePlan(paths, routingRequest);
    // Stored in instance field for use in individual tests
    itinerary = tripPlan.itinerary.get(0);
    assertEquals(legCount, itinerary.legs.size());
    return itinerary.legs.toArray(new Leg[legCount]);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GraphPath(org.opentripplanner.routing.spt.GraphPath) TripPlan(org.opentripplanner.api.model.TripPlan) GenericLocation(org.opentripplanner.common.model.GenericLocation) TraverseMode(org.opentripplanner.routing.core.TraverseMode) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder)

Aggregations

TripPlan (org.opentripplanner.api.model.TripPlan)3 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)3 GraphPath (org.opentripplanner.routing.spt.GraphPath)3 GenericLocation (org.opentripplanner.common.model.GenericLocation)2 GraphPathFinder (org.opentripplanner.routing.impl.GraphPathFinder)2 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 Itinerary (org.opentripplanner.api.model.Itinerary)1 PlannerError (org.opentripplanner.api.model.error.PlannerError)1 TraverseMode (org.opentripplanner.routing.core.TraverseMode)1 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)1 Router (org.opentripplanner.standalone.Router)1