Search in sources :

Example 21 with Leg

use of org.opentripplanner.model.plan.Leg in project OpenTripPlanner by opentripplanner.

the class RaptorPathToItineraryMapper method mapNonTransitLeg.

private void mapNonTransitLeg(List<Leg> legs, PathLeg<TripSchedule> pathLeg, Transfer transfer, Place from, Place to, boolean onlyIfNonZeroDistance) {
    List<Edge> edges = transfer.getEdges();
    if (edges == null || edges.isEmpty()) {
        Leg leg = new Leg(TraverseMode.WALK);
        leg.from = from;
        leg.to = to;
        leg.startTime = createCalendar(pathLeg.fromTime());
        leg.endTime = createCalendar(pathLeg.toTime());
        leg.legGeometry = PolylineEncoder.createEncodings(transfer.getCoordinates());
        leg.distanceMeters = (double) transfer.getDistanceMeters();
        leg.walkSteps = Collections.emptyList();
        if (!onlyIfNonZeroDistance || leg.distanceMeters > 0) {
            legs.add(leg);
        }
    } else {
        RoutingRequest traverseRequest = request.clone();
        traverseRequest.arriveBy = false;
        StateEditor se = new StateEditor(traverseRequest, edges.get(0).getFromVertex());
        se.setTimeSeconds(startOfTime.plusSeconds(pathLeg.fromTime()).toEpochSecond());
        // se.setNonTransitOptionsFromState(states[0]);
        State s = se.makeState();
        ArrayList<State> transferStates = new ArrayList<>();
        transferStates.add(s);
        for (Edge e : edges) {
            s = e.traverse(s);
            transferStates.add(s);
        }
        State[] states = transferStates.toArray(new State[0]);
        GraphPath graphPath = new GraphPath(states[states.length - 1], false);
        Itinerary subItinerary = GraphPathToItineraryMapper.generateItinerary(graphPath, request.locale);
        if (subItinerary.legs.isEmpty()) {
            return;
        }
        // (#2955)
        if (subItinerary.legs.size() != 1) {
            throw new IllegalArgumentException("Sub itineraries should only contain one leg.");
        }
        subItinerary.legs.get(0).startTime = createCalendar(pathLeg.fromTime());
        subItinerary.legs.get(0).endTime = createCalendar(pathLeg.toTime());
        if (!onlyIfNonZeroDistance || subItinerary.nonTransitDistanceMeters > 0) {
            legs.addAll(subItinerary.legs);
        }
    }
}
Also used : StateEditor(org.opentripplanner.routing.core.StateEditor) State(org.opentripplanner.routing.core.State) ArrayList(java.util.ArrayList) GraphPath(org.opentripplanner.routing.spt.GraphPath) Itinerary(org.opentripplanner.model.plan.Itinerary) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) Edge(org.opentripplanner.routing.graph.Edge) 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)

Example 22 with Leg

use of org.opentripplanner.model.plan.Leg in project OpenTripPlanner by opentripplanner.

the class GraphPathToItineraryMapper method generateFlexLeg.

/**
 * Generate a flex leg from the states belonging to the flex leg
 */
private static Leg generateFlexLeg(Graph graph, List<State> states) {
    State fromState = states.get(0);
    State toState = states.get(1);
    FlexTripEdge flexEdge = (FlexTripEdge) toState.backEdge;
    Calendar startTime = makeCalendar(fromState);
    Calendar endTime = makeCalendar(toState);
    int generalizedCost = (int) (toState.getWeight() - fromState.getWeight());
    Leg leg = new FlexibleTransitLeg(flexEdge, startTime, endTime, generalizedCost);
    AlertToLegMapper.addTransitAlertPatchesToLeg(graph, leg, true);
    return leg;
}
Also used : State(org.opentripplanner.routing.core.State) FlexTripEdge(org.opentripplanner.ext.flex.edgetype.FlexTripEdge) Calendar(java.util.Calendar) FlexibleTransitLeg(org.opentripplanner.ext.flex.FlexibleTransitLeg) FlexibleTransitLeg(org.opentripplanner.ext.flex.FlexibleTransitLeg) Leg(org.opentripplanner.model.plan.Leg) StreetLeg(org.opentripplanner.model.plan.StreetLeg)

Example 23 with Leg

use of org.opentripplanner.model.plan.Leg in project OpenTripPlanner by opentripplanner.

the class RaptorPathToItineraryMapper method createItinerary.

public Itinerary createItinerary(Path<TripSchedule> path) {
    var optimizedPath = path instanceof OptimizedPath ? (OptimizedPath<TripSchedule>) path : null;
    // Map access leg
    List<Leg> legs = new ArrayList<>(mapAccessLeg(path.accessLeg()));
    PathLeg<TripSchedule> pathLeg = path.accessLeg().nextLeg();
    boolean firstLeg = true;
    Leg transitLeg = null;
    while (!pathLeg.isEgressLeg()) {
        // Map transit leg
        if (pathLeg.isTransitLeg()) {
            transitLeg = mapTransitLeg(transitLeg, pathLeg.asTransitLeg(), firstLeg);
            firstLeg = false;
            legs.add(transitLeg);
        } else // Map transfer leg
        if (pathLeg.isTransferLeg()) {
            legs.addAll(mapTransferLeg(pathLeg.asTransferLeg(), request.modes.transferMode == StreetMode.BIKE ? TraverseMode.BICYCLE : TraverseMode.WALK));
        }
        pathLeg = pathLeg.nextLeg();
    }
    // Map egress leg
    EgressPathLeg<TripSchedule> egressPathLeg = pathLeg.asEgressLeg();
    Itinerary mapped = mapEgressLeg(egressPathLeg);
    legs.addAll(mapped == null ? List.of() : mapped.legs);
    Itinerary itinerary = new Itinerary(legs);
    // Map general itinerary fields
    itinerary.generalizedCost = toOtpDomainCost(path.generalizedCost());
    itinerary.arrivedAtDestinationWithRentedVehicle = mapped != null && mapped.arrivedAtDestinationWithRentedVehicle;
    if (optimizedPath != null) {
        itinerary.waitTimeOptimizedCost = toOtpDomainCost(optimizedPath.generalizedCostWaitTimeOptimized());
        itinerary.transferPriorityCost = toOtpDomainCost(optimizedPath.transferPriorityCost());
    }
    return itinerary;
}
Also used : OptimizedPath(org.opentripplanner.routing.algorithm.transferoptimization.api.OptimizedPath) ArrayList(java.util.ArrayList) Itinerary(org.opentripplanner.model.plan.Itinerary) TripSchedule(org.opentripplanner.routing.algorithm.raptoradapter.transit.TripSchedule) Leg(org.opentripplanner.model.plan.Leg) TransitPathLeg(org.opentripplanner.transit.raptor.api.path.TransitPathLeg) TransferPathLeg(org.opentripplanner.transit.raptor.api.path.TransferPathLeg) StreetLeg(org.opentripplanner.model.plan.StreetLeg) FrequencyTransitLeg(org.opentripplanner.model.plan.FrequencyTransitLeg) AccessPathLeg(org.opentripplanner.transit.raptor.api.path.AccessPathLeg) ScheduledTransitLeg(org.opentripplanner.model.plan.ScheduledTransitLeg) PathLeg(org.opentripplanner.transit.raptor.api.path.PathLeg) EgressPathLeg(org.opentripplanner.transit.raptor.api.path.EgressPathLeg)

Example 24 with Leg

use of org.opentripplanner.model.plan.Leg in project OpenTripPlanner by opentripplanner.

the class RaptorPathToItineraryMapper method mapTransitLeg.

private Leg mapTransitLeg(Leg prevTransitLeg, TransitPathLeg<TripSchedule> pathLeg, boolean firstLeg) {
    TripSchedule tripSchedule = pathLeg.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
    // have to match stop and time.
    int boardStopIndexInPattern = tripSchedule.findDepartureStopPosition(pathLeg.fromTime(), pathLeg.fromStop());
    int alightStopIndexInPattern = tripSchedule.findArrivalStopPosition(pathLeg.toTime(), pathLeg.toStop());
    Leg leg;
    if (tripSchedule.isFrequencyBasedTrip()) {
        int frequencyHeadwayInSeconds = tripSchedule.frequencyHeadwayInSeconds();
        leg = new FrequencyTransitLeg(tripSchedule.getOriginalTripTimes(), tripSchedule.getOriginalTripPattern(), boardStopIndexInPattern, alightStopIndexInPattern, createCalendar(pathLeg.fromTime() + frequencyHeadwayInSeconds), createCalendar(pathLeg.toTime()), tripSchedule.getServiceDate(), transitSearchTimeZero.getZone().normalized(), (prevTransitLeg == null ? null : prevTransitLeg.getTransferToNextLeg()), (ConstrainedTransfer) pathLeg.getConstrainedTransferAfterLeg(), toOtpDomainCost(pathLeg.generalizedCost()), frequencyHeadwayInSeconds);
    } else {
        leg = new ScheduledTransitLeg(tripSchedule.getOriginalTripTimes(), tripSchedule.getOriginalTripPattern(), boardStopIndexInPattern, alightStopIndexInPattern, createCalendar(pathLeg.fromTime()), createCalendar(pathLeg.toTime()), tripSchedule.getServiceDate(), transitSearchTimeZero.getZone().normalized(), (prevTransitLeg == null ? null : prevTransitLeg.getTransferToNextLeg()), (ConstrainedTransfer) pathLeg.getConstrainedTransferAfterLeg(), toOtpDomainCost(pathLeg.generalizedCost()));
    }
    AlertToLegMapper.addTransitAlertPatchesToLeg(graph, leg, firstLeg);
    return leg;
}
Also used : ScheduledTransitLeg(org.opentripplanner.model.plan.ScheduledTransitLeg) FrequencyTransitLeg(org.opentripplanner.model.plan.FrequencyTransitLeg) ConstrainedTransfer(org.opentripplanner.model.transfer.ConstrainedTransfer) TripSchedule(org.opentripplanner.routing.algorithm.raptoradapter.transit.TripSchedule) Leg(org.opentripplanner.model.plan.Leg) TransitPathLeg(org.opentripplanner.transit.raptor.api.path.TransitPathLeg) TransferPathLeg(org.opentripplanner.transit.raptor.api.path.TransferPathLeg) StreetLeg(org.opentripplanner.model.plan.StreetLeg) FrequencyTransitLeg(org.opentripplanner.model.plan.FrequencyTransitLeg) AccessPathLeg(org.opentripplanner.transit.raptor.api.path.AccessPathLeg) ScheduledTransitLeg(org.opentripplanner.model.plan.ScheduledTransitLeg) PathLeg(org.opentripplanner.transit.raptor.api.path.PathLeg) EgressPathLeg(org.opentripplanner.transit.raptor.api.path.EgressPathLeg)

Example 25 with Leg

use of org.opentripplanner.model.plan.Leg in project OpenTripPlanner by opentripplanner.

the class TripPlanMapper method mapTripPlan.

public static TripPlan mapTripPlan(RoutingRequest request, List<Itinerary> itineraries) {
    Place from;
    Place to;
    if (itineraries.isEmpty()) {
        from = placeFromGeoLocation(request.from, new LocalizedString("origin"));
        to = placeFromGeoLocation(request.to, new LocalizedString("destination"));
    } else {
        List<Leg> legs = itineraries.get(0).legs;
        from = legs.get(0).getFrom();
        to = legs.get(legs.size() - 1).getTo();
    }
    return new TripPlan(from, to, Date.from(request.getDateTime()), itineraries);
}
Also used : TripPlan(org.opentripplanner.model.plan.TripPlan) LocalizedString(org.opentripplanner.util.LocalizedString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) Place(org.opentripplanner.model.plan.Place) Leg(org.opentripplanner.model.plan.Leg)

Aggregations

Leg (org.opentripplanner.model.plan.Leg)38 Itinerary (org.opentripplanner.model.plan.Itinerary)9 StreetLeg (org.opentripplanner.model.plan.StreetLeg)5 AccessPathLeg (org.opentripplanner.transit.raptor.api.path.AccessPathLeg)5 EgressPathLeg (org.opentripplanner.transit.raptor.api.path.EgressPathLeg)5 PathLeg (org.opentripplanner.transit.raptor.api.path.PathLeg)5 TransferPathLeg (org.opentripplanner.transit.raptor.api.path.TransferPathLeg)5 TransitPathLeg (org.opentripplanner.transit.raptor.api.path.TransitPathLeg)5 ArrayList (java.util.ArrayList)4 ScheduledTransitLeg (org.opentripplanner.model.plan.ScheduledTransitLeg)4 State (org.opentripplanner.routing.core.State)4 Calendar (java.util.Calendar)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Edge (org.opentripplanner.routing.graph.Edge)3 Instant (java.time.Instant)2 IntStream (java.util.stream.IntStream)2 Nullable (javax.annotation.Nullable)2 Test (org.junit.Test)2 FlexibleTransitLeg (org.opentripplanner.ext.flex.FlexibleTransitLeg)2