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