Search in sources :

Example 1 with TransitPathLeg

use of org.opentripplanner.transit.raptor.api.path.TransitPathLeg in project OpenTripPlanner by opentripplanner.

the class ItineraryMapper method createItinerary.

private Itinerary createItinerary(Path<TripSchedule> path, StopAtDistance accessPath, StopAtDistance egressPath) {
    if (path == null) {
        return null;
    }
    Itinerary itinerary = new Itinerary();
    itinerary.walkDistance = 0.0;
    itinerary.transitTime = 0;
    itinerary.waitingTime = 0;
    itinerary.weight = path.cost();
    int numberOfTransits = 0;
    // Access leg
    Leg leg = new Leg();
    AccessPathLeg<TripSchedule> accessLeg = path.accessLeg();
    leg.startTime = accessLeg.fromTime();
    leg.endTime = accessLeg.toTime();
    leg.from = request.tc().fromPlace;
    leg.to = request.tc().toPlace;
    leg.mode = WALK;
    leg.distance = accessPath.distance;
    itinerary.addLeg(leg);
    PathLeg<TripSchedule> pathLeg = accessLeg.nextLeg();
    int previousArrivalTime = -1;
    while (pathLeg.isTransitLeg() || pathLeg.isTransferLeg()) {
        leg = new Leg();
        // Transfer leg if present
        if (pathLeg.isTransferLeg()) {
            TransferPathLeg<?> it = pathLeg.asTransferLeg();
            previousArrivalTime = it.toTime();
            leg.startTime = it.fromTime();
            leg.endTime = previousArrivalTime;
            leg.mode = WALK;
            leg.from = mapToPlace(it.fromStop());
            leg.to = mapToPlace(it.toStop());
            // distanceMMToMeters (transferPath.getDistance());
            leg.distance = -1.0;
        } else {
            // Transit leg
            TransitPathLeg<TripSchedule> it = pathLeg.asTransitLeg();
            itinerary.transitTime += it.toTime() - it.fromTime();
            itinerary.waitingTime += it.fromTime() - previousArrivalTime;
            previousArrivalTime = it.toTime();
            ++numberOfTransits;
            leg.distance = 0.0;
            TripSchedule tripSchedule = it.trip();
            TripPattern tripPattern = tripSchedule.getOriginalTripPattern();
            Route routeInfo = tripPattern.route;
            leg.from = mapToPlace(it.fromStop());
            leg.to = mapToPlace(it.toStop());
            leg.route = routeInfo.getShortName();
            leg.agencyName = routeInfo.getAgency().getName();
            leg.tripShortName = tripSchedule.getOriginalTripPattern().name;
            leg.agencyId = routeInfo.getAgency().getId();
            leg.routeShortName = routeInfo.getShortName();
            leg.routeLongName = routeInfo.getLongName();
            leg.mode = TraverseMode.fromTransitMode(tripSchedule.getOriginalTripPattern().getMode());
            leg.startTime = it.fromTime();
            leg.endTime = it.toTime();
        }
        itinerary.addLeg(leg);
        pathLeg = pathLeg.nextLeg();
    }
    // Egress leg
    leg = new Leg();
    EgressPathLeg<TripSchedule> egressLeg = pathLeg.asEgressLeg();
    leg.startTime = egressLeg.fromTime();
    leg.endTime = egressLeg.toTime();
    leg.from = mapToPlace(egressLeg.fromStop());
    leg.to = request.tc().toPlace;
    leg.mode = WALK;
    leg.distance = egressPath.distance;
    itinerary.addLeg(leg);
    itinerary.startTime = itinerary.legs.get(0).startTime;
    itinerary.endTime = leg.endTime;
    itinerary.duration = itinerary.endTime - itinerary.startTime;
    // The number of transfers is the number of transits minus one, we can NOT count the number of Transfers
    // in the path or itinerary, because transfers at the same stop does not produce a transfer object, just two
    // transits following each other.
    itinerary.transfers = numberOfTransits - 1;
    return itinerary;
}
Also used : Itinerary(org.opentripplanner.transit.raptor.speed_test.model.Itinerary) TripSchedule(org.opentripplanner.routing.algorithm.raptor.transit.TripSchedule) TripPattern(org.opentripplanner.model.TripPattern) Route(org.opentripplanner.model.Route) TransitPathLeg(org.opentripplanner.transit.raptor.api.path.TransitPathLeg) Leg(org.opentripplanner.transit.raptor.speed_test.model.Leg) TransferPathLeg(org.opentripplanner.transit.raptor.api.path.TransferPathLeg) AccessPathLeg(org.opentripplanner.transit.raptor.api.path.AccessPathLeg) PathLeg(org.opentripplanner.transit.raptor.api.path.PathLeg) EgressPathLeg(org.opentripplanner.transit.raptor.api.path.EgressPathLeg)

Example 2 with TransitPathLeg

use of org.opentripplanner.transit.raptor.api.path.TransitPathLeg in project OpenTripPlanner by opentripplanner.

the class RaptorPathToItineraryMapper method mapTransitLeg.

private Leg mapTransitLeg(RoutingRequest request, TransitPathLeg<TripSchedule> pathLeg, boolean firstLeg) {
    Stop boardStop = transitLayer.getStopByIndex(pathLeg.fromStop());
    Stop alightStop = transitLayer.getStopByIndex(pathLeg.toStop());
    TripSchedule tripSchedule = pathLeg.trip();
    TripTimes tripTimes = tripSchedule.getOriginalTripTimes();
    Leg leg = new Leg(tripTimes.trip);
    // Find stop positions in pattern where this leg boards and alights.
    // We cannot assume every stop appears only once in a pattern, so we match times instead of stops.
    int boardStopIndexInPattern = tripSchedule.findStopPosInPattern(pathLeg.fromStop(), pathLeg.fromTime(), true);
    int alightStopIndexInPattern = tripSchedule.findStopPosInPattern(pathLeg.toStop(), pathLeg.toTime(), false);
    // Include real-time information in the Leg.
    if (!tripTimes.isScheduled()) {
        leg.realTime = true;
        leg.departureDelay = tripTimes.getDepartureDelay(boardStopIndexInPattern);
        leg.arrivalDelay = tripTimes.getArrivalDelay(alightStopIndexInPattern);
    }
    leg.serviceDate = new ServiceDate(tripSchedule.getServiceDate());
    leg.intermediateStops = new ArrayList<>();
    leg.startTime = createCalendar(pathLeg.fromTime());
    leg.endTime = createCalendar(pathLeg.toTime());
    leg.from = mapStopToPlace(boardStop, boardStopIndexInPattern);
    leg.to = mapStopToPlace(alightStop, alightStopIndexInPattern);
    List<Coordinate> transitLegCoordinates = extractTransitLegCoordinates(pathLeg);
    leg.legGeometry = PolylineEncoder.createEncodings(transitLegCoordinates);
    leg.distanceMeters = getDistanceFromCoordinates(transitLegCoordinates);
    if (request.showIntermediateStops) {
        leg.intermediateStops = extractIntermediateStops(pathLeg);
    }
    leg.headsign = tripTimes.getHeadsign(boardStopIndexInPattern);
    leg.walkSteps = new ArrayList<>();
    // TODO OTP2 - alightRule and boardRule needs mapping
    // Under Raptor, for transit trips, ItineraryMapper converts Path<TripSchedule> directly to Itinerary
    // (the old API response element, within TripPlan). Non-transit trips still use GraphPathToTripPlanConverter
    // to turn A* results (sequences of States and Edges called GraphPaths) into TripPlans which also contain
    // Itineraries.
    // So transit results do not go through GraphPathToTripPlanConverter. It contains logic to find board/alight
    // rules from StopPatterns within TripPatterns, and attach them as machine-readable codes on Legs, but only
    // where you are really boarding or alighting (considering interlining / in-seat transfers).
    // That needs to be re-implemented for the Raptor transit case.
    // - See e2118e0a -> GraphPathToTripPlanConverter#fixupLegs(List<Leg>, State[][]))
    // leg.alightRule = <Assign here>;
    // leg.boardRule =  <Assign here>;
    AlertToLegMapper.addAlertPatchesToLeg(request.getRoutingContext().graph, leg, firstLeg, request.locale);
    return leg;
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Stop(org.opentripplanner.model.Stop) Coordinate(org.locationtech.jts.geom.Coordinate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripSchedule(org.opentripplanner.routing.algorithm.raptor.transit.TripSchedule) Leg(org.opentripplanner.model.plan.Leg) TransitPathLeg(org.opentripplanner.transit.raptor.api.path.TransitPathLeg) TransferPathLeg(org.opentripplanner.transit.raptor.api.path.TransferPathLeg) AccessPathLeg(org.opentripplanner.transit.raptor.api.path.AccessPathLeg) PathLeg(org.opentripplanner.transit.raptor.api.path.PathLeg) EgressPathLeg(org.opentripplanner.transit.raptor.api.path.EgressPathLeg)

Aggregations

TripSchedule (org.opentripplanner.routing.algorithm.raptor.transit.TripSchedule)2 AccessPathLeg (org.opentripplanner.transit.raptor.api.path.AccessPathLeg)2 EgressPathLeg (org.opentripplanner.transit.raptor.api.path.EgressPathLeg)2 PathLeg (org.opentripplanner.transit.raptor.api.path.PathLeg)2 TransferPathLeg (org.opentripplanner.transit.raptor.api.path.TransferPathLeg)2 TransitPathLeg (org.opentripplanner.transit.raptor.api.path.TransitPathLeg)2 Coordinate (org.locationtech.jts.geom.Coordinate)1 Route (org.opentripplanner.model.Route)1 Stop (org.opentripplanner.model.Stop)1 TripPattern (org.opentripplanner.model.TripPattern)1 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)1 Leg (org.opentripplanner.model.plan.Leg)1 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)1 Itinerary (org.opentripplanner.transit.raptor.speed_test.model.Itinerary)1 Leg (org.opentripplanner.transit.raptor.speed_test.model.Leg)1