Search in sources :

Example 21 with StopTime

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

the class SubsectionTripTransformStrategy method getStopIndices.

private Map<String, Integer> getStopIndices(List<StopTime> stopTimes) {
    Map<String, Integer> indices = new HashMap<String, Integer>();
    int index = 0;
    for (StopTime stopTime : stopTimes) {
        String id = stopTime.getStop().getId().getId();
        if (!indices.containsKey(id)) {
            indices.put(id, index);
        }
        index++;
    }
    return indices;
}
Also used : HashMap(java.util.HashMap) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 22 with StopTime

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

the class TrimTripTransformStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    List<Trip> tripsToAdd = new ArrayList<Trip>();
    List<StopTime> stopTimesToAdd = new ArrayList<StopTime>();
    List<Trip> tripsToRemove = new ArrayList<Trip>();
    List<StopTime> stopTimesToRemove = new ArrayList<StopTime>();
    Set<AgencyAndId> newShapeIds = new HashSet<AgencyAndId>();
    for (Trip trip : dao.getAllTrips()) {
        List<TrimOperation> operations = getMatchingOperations(trip);
        if (operations.isEmpty()) {
            continue;
        }
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
        boolean removeOriginalTrip = false;
        for (TrimOperation operation : operations) {
            Integer preIndex = stopToIndex.get(operation.getToStopId());
            Integer postIndex = stopToIndex.get(operation.getFromStopId());
            if (postIndex == null && preIndex == null) {
                continue;
            }
            removeOriginalTrip = true;
            Trip newTrip = new Trip(trip);
            String id = newTrip.getId().getId();
            if (preIndex != null) {
                id += "-" + operation.getToStopId();
            }
            if (postIndex != null) {
                id += "-" + operation.getFromStopId();
            }
            if (preIndex == null) {
                preIndex = 0;
            } else {
                preIndex++;
            }
            if (postIndex == null) {
                postIndex = stopTimes.size() - 1;
            } else {
                postIndex--;
            }
            newTrip.setId(new AgencyAndId(trip.getId().getAgencyId(), id));
            List<StopTime> newStopTimes = new ArrayList<StopTime>();
            for (int i = preIndex; i <= postIndex; ++i) {
                StopTime stopTime = new StopTime(stopTimes.get(i));
                stopTime.setId(0);
                stopTime.setTrip(newTrip);
                newStopTimes.add(stopTime);
            }
            /**
         * If the entire trip was trimmed, we just drop it.
         */
            if (newStopTimes.isEmpty()) {
                continue;
            }
            updateShape(dao, newTrip, newStopTimes, newShapeIds);
            tripsToAdd.add(newTrip);
            stopTimesToAdd.addAll(newStopTimes);
        }
        if (removeOriginalTrip) {
            tripsToRemove.add(trip);
            stopTimesToRemove.addAll(stopTimes);
        }
    }
    for (StopTime stopTime : stopTimesToRemove) {
        dao.removeEntity(stopTime);
    }
    for (Trip trip : tripsToRemove) {
        dao.removeEntity(trip);
    }
    for (Trip trip : tripsToAdd) {
        dao.saveEntity(trip);
    }
    for (StopTime stopTime : stopTimesToAdd) {
        dao.saveEntity(stopTime);
    }
    UpdateLibrary.clearDaoCache(dao);
    Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>(dao.getAllShapeIds());
    for (Trip trip : dao.getAllTrips()) {
        shapeIds.remove(trip.getShapeId());
    }
    for (AgencyAndId shapeId : shapeIds) {
        for (ShapePoint point : dao.getShapePointsForShapeId(shapeId)) {
            dao.removeEntity(point);
        }
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) StopTime(org.onebusaway.gtfs.model.StopTime) HashSet(java.util.HashSet)

Example 23 with StopTime

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

the class TripsByBlockInSortedOrder method getTripsByBlockInSortedOrder.

public static Map<String, List<Trip>> getTripsByBlockInSortedOrder(GtfsMutableRelationalDao dao) {
    Map<String, List<Trip>> tripsByBlockId = new HashMap<String, List<Trip>>();
    Map<Trip, Integer> averageStopTimeByTrip = new HashMap<Trip, Integer>();
    int totalTrips = 0;
    int tripsWithoutStopTimes = 0;
    for (Trip trip : dao.getAllTrips()) {
        totalTrips++;
        String blockId = trip.getBlockId();
        // Generate a random block id if none is present so we get no collisions
        if (blockId == null)
            blockId = trip.getId() + "-" + Math.random();
        List<Trip> trips = tripsByBlockId.get(blockId);
        if (trips == null) {
            trips = new ArrayList<Trip>();
            tripsByBlockId.put(blockId, trips);
        }
        trips.add(trip);
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        if (stopTimes.isEmpty()) {
            tripsWithoutStopTimes++;
        } else {
            int arrivalTimes = 0;
            int arrivalTimeCount = 0;
            for (StopTime stopTime : stopTimes) {
                if (stopTime.isArrivalTimeSet()) {
                    arrivalTimes += stopTime.getArrivalTime();
                    arrivalTimeCount++;
                }
            }
            if (arrivalTimeCount > 0) {
                int averageArrivalTime = arrivalTimes / arrivalTimeCount;
                averageStopTimeByTrip.put(trip, averageArrivalTime);
            }
        }
    }
    _log.info("trips=" + totalTrips + " withoutStopTimes=" + tripsWithoutStopTimes);
    TripComparator c = new TripComparator(averageStopTimeByTrip);
    for (List<Trip> tripsInBlock : tripsByBlockId.values()) {
        Collections.sort(tripsInBlock, c);
    }
    return tripsByBlockId;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 24 with StopTime

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

the class TripMergeStrategy method save.

@Override
protected void save(GtfsMergeContext context, IdentityBean<?> entity) {
    GtfsRelationalDao source = context.getSource();
    GtfsMutableRelationalDao target = context.getTarget();
    Trip trip = (Trip) entity;
    // save them out; when the trip is renamed stop time refs will be lost
    List<StopTime> stopTimes = source.getStopTimesForTrip(trip);
    super.save(context, entity);
    for (StopTime stopTime : stopTimes) {
        stopTime.setId(0);
        stopTime.setTrip(trip);
        target.saveEntity(stopTime);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsRelationalDao(org.onebusaway.gtfs.services.GtfsRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 25 with StopTime

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

the class TripMergeStrategy method rejectDuplicateOverDifferences.

/**
   * Even if we have detected that two trips are duplicates, they might have
   * slight differences that prevent them from being represented as one merged
   * trip. For example, if a trip in a subsequent feed adds, removes, or
   * modifies a stop time, we might avoid merging the two trips such that the
   * schedule is correct in the merged feed.
   * 
   * TODO: Think about how this should be applied in relation to the service
   * calendars of the two trips.
   */
@Override
protected boolean rejectDuplicateOverDifferences(GtfsMergeContext context, Trip sourceEntity, Trip targetDuplicate) {
    GtfsRelationalDao source = context.getSource();
    GtfsRelationalDao target = context.getTarget();
    List<StopTime> sourceStopTimes = source.getStopTimesForTrip(sourceEntity);
    List<StopTime> targetStopTimes = target.getStopTimesForTrip(targetDuplicate);
    if (sourceStopTimes.size() != targetStopTimes.size()) {
        return true;
    }
    for (int i = 0; i < sourceStopTimes.size(); ++i) {
        StopTime sourceStopTime = sourceStopTimes.get(i);
        StopTime targetStopTime = targetStopTimes.get(i);
        if (!sourceStopTime.getStop().equals(targetStopTime.getStop())) {
            return true;
        }
        if (sourceStopTime.getArrivalTime() != targetStopTime.getArrivalTime() || sourceStopTime.getDepartureTime() != targetStopTime.getDepartureTime()) {
            return true;
        }
    }
    return false;
}
Also used : GtfsRelationalDao(org.onebusaway.gtfs.services.GtfsRelationalDao) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

StopTime (org.onebusaway.gtfs.model.StopTime)37 Trip (org.onebusaway.gtfs.model.Trip)22 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 Test (org.junit.Test)12 Stop (org.onebusaway.gtfs.model.Stop)11 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)8 ArrayList (java.util.ArrayList)7 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)7 List (java.util.List)6 Agency (org.onebusaway.gtfs.model.Agency)6 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)5 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)5 HashSet (java.util.HashSet)4 Frequency (org.onebusaway.gtfs.model.Frequency)4 Route (org.onebusaway.gtfs.model.Route)4 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)4 HashMap (java.util.HashMap)3 FareAttribute (org.onebusaway.gtfs.model.FareAttribute)3 FareRule (org.onebusaway.gtfs.model.FareRule)3 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)3