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);
}
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations