Search in sources :

Example 1 with StopLocation

use of org.onebusaway.gtfs.model.StopLocation in project onebusaway-gtfs-modules by OneBusAway.

the class RouteStopsInCommonDuplicateScoringStrategy method getAllStopsForRoute.

private Set<StopLocation> getAllStopsForRoute(GtfsRelationalDao dao, Route route) {
    Set<StopLocation> stops = new HashSet<>();
    List<Trip> tripsForRoute = new ArrayList<Trip>();
    // make this thread safe
    tripsForRoute.addAll(dao.getTripsForRoute(route));
    for (Trip trip : tripsForRoute) {
        List<StopTime> stopTimesForTrip = new ArrayList<StopTime>();
        stopTimesForTrip.addAll(dao.getStopTimesForTrip(trip));
        for (StopTime stopTime : stopTimesForTrip) {
            stops.add(stopTime.getStop());
        }
    }
    return stops;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) ArrayList(java.util.ArrayList) StopLocation(org.onebusaway.gtfs.model.StopLocation) HashSet(java.util.HashSet) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 2 with StopLocation

use of org.onebusaway.gtfs.model.StopLocation in project onebusaway-gtfs-modules by OneBusAway.

the class TrimTripTransformStrategy method updateShape.

private void updateShape(GtfsMutableRelationalDao dao, Trip trip, List<StopTime> stopTimes, Set<AgencyAndId> newShapeIds) {
    if (stopTimes.size() < 2) {
        trip.setShapeId(null);
        return;
    }
    AgencyAndId shapeId = trip.getShapeId();
    if (shapeId == null || !shapeId.hasValues()) {
        return;
    }
    List<ShapePoint> points = dao.getShapePointsForShapeId(shapeId);
    if (points.isEmpty()) {
        return;
    }
    StopLocation firstStop = stopTimes.get(0).getStop();
    StopLocation lastStop = stopTimes.get(stopTimes.size() - 1).getStop();
    String id = shapeId.getId() + "-" + firstStop.getId().getId() + "-" + lastStop.getId().getId();
    AgencyAndId newShapeId = new AgencyAndId(shapeId.getAgencyId(), id);
    trip.setShapeId(newShapeId);
    if (!(firstStop instanceof Stop)) {
        // TODO Correct error type
        throw new Error(firstStop + " must be stop");
    }
    if (!(lastStop instanceof Stop)) {
        // TODO Correct error type
        throw new Error(firstStop + " must be stop");
    }
    if (!newShapeIds.add(newShapeId)) {
        return;
    }
    int shapePointFrom = getClosestShapePointToStop(points, (Stop) firstStop);
    int shapePointTo = getClosestShapePointToStop(points, (Stop) lastStop);
    for (int index = shapePointFrom; index <= shapePointTo; ++index) {
        ShapePoint point = new ShapePoint(points.get(index));
        point.setId(0);
        point.setShapeId(newShapeId);
        dao.saveEntity(point);
    }
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) StopLocation(org.onebusaway.gtfs.model.StopLocation) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 3 with StopLocation

use of org.onebusaway.gtfs.model.StopLocation in project onebusaway-gtfs-modules by OneBusAway.

the class InterpolateStopTimesFromTimePointsStrategy method interpolateArrivalAndDepartureTimes.

private void interpolateArrivalAndDepartureTimes(GtfsMutableRelationalDao dao, List<StopTime> stopTimes, double[] distanceTraveled, int[] arrivalTimes, int[] departureTimes) {
    SortedMap<Double, Integer> scheduleTimesByDistanceTraveled = new TreeMap<Double, Integer>();
    populateArrivalAndDepartureTimesByDistanceTravelledForStopTimes(stopTimes, distanceTraveled, scheduleTimesByDistanceTraveled);
    for (int i = 0; i < stopTimes.size(); i++) {
        StopTime stopTime = stopTimes.get(i);
        double d = distanceTraveled[i];
        boolean hasDeparture = stopTime.isDepartureTimeSet();
        boolean hasArrival = stopTime.isArrivalTimeSet();
        int departureTime = stopTime.getDepartureTime();
        int arrivalTime = stopTime.getArrivalTime();
        if (hasDeparture && !hasArrival) {
            arrivalTime = departureTime;
        } else if (hasArrival && !hasDeparture) {
            departureTime = arrivalTime;
        } else if (!hasArrival && !hasDeparture) {
            int t = departureTimes[i] = (int) InterpolationLibrary.interpolate(scheduleTimesByDistanceTraveled, d);
            arrivalTime = t;
            departureTime = t;
        }
        departureTimes[i] = departureTime;
        arrivalTimes[i] = arrivalTime;
        if (departureTimes[i] < arrivalTimes[i])
            throw new IllegalStateException("departure time is less than arrival time for stop time with trip_id=" + stopTime.getTrip().getId() + " stop_sequence=" + stopTime.getStopSequence());
        if (i > 0 && arrivalTimes[i] < departureTimes[i - 1]) {
            /**
             * The previous stop time's departure time comes AFTER this stop time's
             * arrival time. That's bad.
             */
            StopTime prevStopTime = stopTimes.get(i - 1);
            StopLocation prevStop = prevStopTime.getStop();
            StopLocation stop = stopTime.getStop();
            if (prevStop.equals(stop) && arrivalTimes[i] == departureTimes[i - 1] - 1) {
                _log.info("fixing decreasing passingTimes: stopTimeA=" + prevStopTime.getId() + " stopTimeB=" + stopTime.getId());
                arrivalTimes[i] = departureTimes[i - 1];
                if (departureTimes[i] < arrivalTimes[i])
                    departureTimes[i] = arrivalTimes[i];
            } else if (isLenientMode() && i > 0 && (arrivalTimes[i] < arrivalTimes[i - 1]) && (departureTimes[i] < departureTimes[i - 1])) {
                // recalculate the last two stops and hope for the best!
                // we can correct small accuracy errors here
                int t0 = (int) InterpolationLibrary.interpolate(scheduleTimesByDistanceTraveled, distanceTraveled[i - 1]);
                int t1 = (int) InterpolationLibrary.interpolate(scheduleTimesByDistanceTraveled, distanceTraveled[i]);
                if (t1 < t0) {
                    // sometimes even the interpolation gets it wrong
                    int m = t0;
                    t0 = t1;
                    t1 = m;
                    _log.warn("interpolation error");
                }
                _log.warn("correcting arrival time of sequence " + (stopTime.getStopSequence() - 1) + ", " + stopTime.getStopSequence() + " of trip " + stopTime.getTrip().getId() + " as it was less than last departure time.  Arrival[" + (i - 1) + "] " + arrivalTimes[i - 1] + " now " + t0 + ", Arrival[" + i + "] " + arrivalTimes[i] + " now " + t1);
                arrivalTimes[i - 1] = departureTimes[i - 1] = t0;
                arrivalTimes[i] = departureTimes[i] = t1;
            } else {
                for (int x = 0; x < stopTimes.size(); x++) {
                    StopTime st = stopTimes.get(x);
                    final String msg = x + " " + st.getId() + " " + arrivalTimes[x] + " " + departureTimes[x];
                    _log.error(msg);
                    System.err.println(msg);
                }
                final String exceptionMessage = "arrival time is less than previous departure time for stop time " + " with isLenientArrivalDepartureTimes=" + this.isLenientArrivalDepartureTimes() + " and trip_id=" + stopTime.getTrip().getId() + " stop_sequence=" + stopTime.getStopSequence() + ", arrivalTime=" + arrivalTimes[i] + ", departureTime=" + departureTimes[i] + (i > 0 ? " arrivalTimes[" + (i - 1) + "]=" + arrivalTimes[i - 1] + ", departureTimes[" + (i - 1) + "]=" + departureTimes[i - 1] : " (i<1)");
                _log.error(exceptionMessage);
                throw new IllegalStateException(exceptionMessage);
            }
        }
    }
}
Also used : StopLocation(org.onebusaway.gtfs.model.StopLocation) TreeMap(java.util.TreeMap) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 4 with StopLocation

use of org.onebusaway.gtfs.model.StopLocation in project onebusaway-gtfs-modules by OneBusAway.

the class LastStopToHeadsignStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    for (Trip trip : dao.getAllTrips()) {
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        StopLocation lastStop = stopTimes.get(stopTimes.size() - 1).getStop();
        trip.setTripHeadsign(lastStop.getName());
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) StopLocation(org.onebusaway.gtfs.model.StopLocation) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 5 with StopLocation

use of org.onebusaway.gtfs.model.StopLocation in project onebusaway-gtfs-modules by OneBusAway.

the class TripKey method getTripKeyForTrip.

public static TripKey getTripKeyForTrip(GtfsMutableRelationalDao dao, Trip trip) {
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    StopLocation[] stops = new Stop[stopTimes.size()];
    int[] arrivalTimes = new int[stopTimes.size()];
    int[] departureTimes = new int[stopTimes.size()];
    for (int i = 0; i < stopTimes.size(); i++) {
        StopTime stopTime = stopTimes.get(i);
        stops[i] = stopTime.getStop();
        arrivalTimes[i] = stopTime.getArrivalTime();
        departureTimes[i] = stopTime.getDepartureTime();
    }
    return new TripKey(stops, arrivalTimes, departureTimes);
}
Also used : Stop(org.onebusaway.gtfs.model.Stop) StopLocation(org.onebusaway.gtfs.model.StopLocation) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

StopLocation (org.onebusaway.gtfs.model.StopLocation)7 StopTime (org.onebusaway.gtfs.model.StopTime)5 Stop (org.onebusaway.gtfs.model.Stop)4 Trip (org.onebusaway.gtfs.model.Trip)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1