Search in sources :

Example 1 with PlannerError

use of org.opentripplanner.api.model.error.PlannerError 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)

Aggregations

GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 TripPlan (org.opentripplanner.api.model.TripPlan)1 PlannerError (org.opentripplanner.api.model.error.PlannerError)1 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)1 GraphPathFinder (org.opentripplanner.routing.impl.GraphPathFinder)1 GraphPath (org.opentripplanner.routing.spt.GraphPath)1 Router (org.opentripplanner.standalone.Router)1