use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class TripTimeShortHelper method getTripTimeShortForFromPlace.
/**
* Find trip time short for the from place in transit leg, or null.
*/
public TripTimeShort getTripTimeShortForFromPlace(Leg leg, RoutingService routingService) {
if (!leg.isTransitLeg()) {
return null;
}
ServiceDate serviceDate = leg.serviceDate;
List<TripTimeShort> tripTimes = routingService.getTripTimesShort(leg.getTrip(), serviceDate);
long startTimeSeconds = (leg.startTime.toInstant().toEpochMilli() - serviceDate.getAsDate().getTime()) / 1000;
if (leg.realTime) {
return tripTimes.stream().filter(tripTime -> tripTime.realtimeDeparture == startTimeSeconds && matchesQuayOrSiblingQuay(leg.from.stopId, tripTime.stopId, routingService)).findFirst().orElse(null);
}
return tripTimes.stream().filter(tripTime -> tripTime.scheduledDeparture == startTimeSeconds && matchesQuayOrSiblingQuay(leg.from.stopId, tripTime.stopId, routingService)).findFirst().orElse(null);
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class CalendarServiceDataFactoryImpl method createData.
private CalendarServiceData createData() {
CalendarServiceData data = new CalendarServiceData();
setTimeZonesForAgencies(data);
int index = 0;
for (FeedScopedId serviceId : serviceIds) {
index++;
LOG.debug("serviceId=" + serviceId + " (" + index + "/" + serviceIds.size() + ")");
TimeZone serviceIdTimeZone = data.getTimeZoneForAgencyId(data.getAgencyIds().stream().filter(agency -> agency.getFeedId().equals(serviceId.getFeedId())).findAny().orElse(null));
if (serviceIdTimeZone == null) {
serviceIdTimeZone = TimeZone.getDefault();
}
Set<ServiceDate> activeDates = getServiceDatesForServiceId(serviceId, serviceIdTimeZone);
List<ServiceDate> serviceDates = new ArrayList<>(activeDates);
Collections.sort(serviceDates);
data.putServiceDatesForServiceId(serviceId, serviceDates);
}
return data;
}
use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class CalendarServiceDataFactoryImpl method addDatesFromCalendar.
private void addDatesFromCalendar(ServiceCalendar calendar, TimeZone timeZone, Set<ServiceDate> activeDates) {
// We calculate service dates relative to noon so as to avoid any weirdness
// relative to DST.
Date startDate = getServiceDateAsNoon(calendar.getPeriod().getStart(), timeZone);
Date endDate = getServiceDateAsNoon(calendar.getPeriod().getEnd(), timeZone);
java.util.Calendar c = java.util.Calendar.getInstance(timeZone);
c.setTime(startDate);
while (true) {
Date date = c.getTime();
if (date.after(endDate))
break;
int day = c.get(java.util.Calendar.DAY_OF_WEEK);
boolean active = false;
switch(day) {
case java.util.Calendar.MONDAY:
active = calendar.getMonday() == 1;
break;
case java.util.Calendar.TUESDAY:
active = calendar.getTuesday() == 1;
break;
case java.util.Calendar.WEDNESDAY:
active = calendar.getWednesday() == 1;
break;
case java.util.Calendar.THURSDAY:
active = calendar.getThursday() == 1;
break;
case java.util.Calendar.FRIDAY:
active = calendar.getFriday() == 1;
break;
case java.util.Calendar.SATURDAY:
active = calendar.getSaturday() == 1;
break;
case java.util.Calendar.SUNDAY:
active = calendar.getSunday() == 1;
break;
}
if (active) {
addServiceDate(activeDates, new ServiceDate(c));
}
c.add(java.util.Calendar.DAY_OF_YEAR, 1);
}
}
use of org.opentripplanner.model.calendar.ServiceDate 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.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.
the class StopTimesHelper method stopTimesForPatternAtStop.
/**
* Fetch upcoming vehicle departures from a stop for a single pattern, passing the stop for the previous, current and
* next service date. It uses a priority queue to keep track of the next departures. The queue is shared between all
* dates, as services from the previous service date can visit the stop later than the current service date's
* services.
*
* TODO: Add frequency based trips
* @param stop Stop object to perform the search for
* @param pattern Pattern object to perform the search for
* @param startTime Start time for the search. Seconds from UNIX epoch
* @param timeRange Searches forward for timeRange seconds from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param omitNonPickups If true, do not include vehicles that will not pick up passengers.
*/
public static List<TripTimeShort> stopTimesForPatternAtStop(RoutingService routingService, TimetableSnapshot timetableSnapshot, Stop stop, TripPattern pattern, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups) {
if (startTime == 0) {
startTime = System.currentTimeMillis() / 1000;
}
Date date = new Date(startTime * 1000);
ServiceDate[] serviceDates = { new ServiceDate(date).previous(), new ServiceDate(date), new ServiceDate(date).next() };
Queue<TripTimeShort> pq = listTripTimeShortsForPatternAtStop(routingService, timetableSnapshot, stop, pattern, startTime, timeRange, numberOfDepartures, omitNonPickups, serviceDates);
return new ArrayList<>(pq);
}
Aggregations