use of org.opentripplanner.transit.raptor.api.path.PathLeg 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;
}
use of org.opentripplanner.transit.raptor.api.path.PathLeg 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;
}
use of org.opentripplanner.transit.raptor.api.path.PathLeg in project OpenTripPlanner by opentripplanner.
the class RaptorPathToItineraryMapper method createItinerary.
public Itinerary createItinerary(Path<TripSchedule> path) {
List<Leg> legs = new ArrayList<>();
// Map access leg
mapAccessLeg(legs, path.accessLeg());
// TODO: Add back this code when PathLeg interface contains object references
PathLeg<TripSchedule> pathLeg = path.accessLeg().nextLeg();
boolean firstLeg = true;
while (!pathLeg.isEgressLeg()) {
// Map transit leg
if (pathLeg.isTransitLeg()) {
Leg transitLeg = mapTransitLeg(request, pathLeg.asTransitLeg(), firstLeg);
firstLeg = false;
legs.add(transitLeg);
}
// Map transfer leg
if (pathLeg.isTransferLeg()) {
mapTransferLeg(legs, pathLeg.asTransferLeg());
}
pathLeg = pathLeg.nextLeg();
}
// Map egress leg
EgressPathLeg<TripSchedule> egressPathLeg = pathLeg.asEgressLeg();
mapEgressLeg(legs, egressPathLeg);
propagateStopPlaceNamesToWalkingLegs(legs);
Itinerary itinerary = new Itinerary(legs);
// Map general itinerary fields
itinerary.generalizedCost = path.cost();
itinerary.nonTransitLimitExceeded = itinerary.nonTransitDistanceMeters > request.maxWalkDistance;
return itinerary;
}
use of org.opentripplanner.transit.raptor.api.path.PathLeg 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);
}
}
}
Aggregations