use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class RemoveEmptyBlockTripsStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
Map<String, List<Trip>> tripsByBlockId = MappingLibrary.mapToValueList(dao.getAllTrips(), "blockId", String.class);
int tripsRemoved = 0;
int blocksRemoved = 0;
for (Map.Entry<String, List<Trip>> entry : tripsByBlockId.entrySet()) {
String blockId = entry.getKey();
List<Trip> trips = entry.getValue();
if (blockId == null)
continue;
boolean hasStopTimes = false;
for (Trip trip : trips) {
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
if (!stopTimes.isEmpty())
hasStopTimes = true;
}
if (hasStopTimes)
continue;
blocksRemoved++;
tripsRemoved += trips.size();
_log.info("removing block " + blockId);
for (Trip trip : trips) dao.removeEntity(trip);
}
UpdateLibrary.clearDaoCache(dao);
_log.info("blocksRemoved=" + blocksRemoved + " tripsRemoved=" + tripsRemoved);
}
use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class StopTimesFactoryStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
GtfsReaderContext gtfsReaderContext = context.getReader().getGtfsReaderContext();
Trip trip = getTrip(gtfsReaderContext, dao);
List<Stop> stops = getStops(gtfsReaderContext, dao);
int[] times = getTimesForStops(stops);
for (int i = 0; i < stops.size(); ++i) {
StopTime stopTime = new StopTime();
stopTime.setStop(stops.get(i));
stopTime.setStopSequence(i);
stopTime.setArrivalTime(times[i]);
stopTime.setDepartureTime(times[i]);
stopTime.setTrip(trip);
dao.saveEntity(stopTime);
}
}
use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeDirectionTransformStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
List<Trip> tripsToTransform = new ArrayList<Trip>();
Collection<Trip> allTrips = dao.getAllTrips();
for (Trip t : allTrips) {
if (t.getShapeId().getId().equals(shapeId) && !t.getDirectionId().equals(shapeDirection)) {
tripsToTransform.add(t);
}
}
if (!tripsToTransform.isEmpty()) {
String agencyId = context.getDefaultAgencyId();
AgencyAndId inputShapeId = new AgencyAndId(agencyId, shapeId);
AgencyAndId newShapeId = new AgencyAndId(agencyId, shapeId + "R");
List<ShapePoint> shapePoints = new ArrayList<ShapePoint>(dao.getShapePointsForShapeId(inputShapeId));
Collections.reverse(shapePoints);
int newIndex = 1;
for (ShapePoint sp : shapePoints) {
ShapePoint nsp = new ShapePoint();
nsp.setShapeId(newShapeId);
nsp.setSequence(newIndex++);
nsp.setLat(sp.getLat());
nsp.setLon(sp.getLon());
dao.saveEntity(nsp);
}
for (Trip t : tripsToTransform) {
t.setShapeId(newShapeId);
}
}
}
use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class SubsectionTripTransformStrategy 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()) {
String routeId = trip.getRoute().getId().getId();
List<SubsectionOperation> operations = _operationsByRouteId.get(routeId);
if (operations == null) {
continue;
}
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
boolean removeOriginalTrip = false;
for (SubsectionOperation operation : operations) {
Integer fromIndex = stopToIndex.get(operation.getFromStopId());
Integer toIndex = stopToIndex.get(operation.getToStopId());
if (fromIndex == null && toIndex == null) {
if (operation.removeUnmatchedTrips) {
removeOriginalTrip = true;
}
continue;
}
removeOriginalTrip = true;
Trip newTrip = new Trip(trip);
String id = newTrip.getId().getId();
if (fromIndex != null) {
id += "-" + operation.getFromStopId();
}
if (toIndex != null) {
id += "-" + operation.getToStopId();
}
if (fromIndex == null) {
fromIndex = 0;
} else if (!operation.isIncludeFromStop()) {
fromIndex++;
}
if (toIndex == null) {
toIndex = stopTimes.size() - 1;
} else if (!operation.isIncludeToStop()) {
toIndex--;
}
newTrip.setId(new AgencyAndId("1", id));
tripsToAdd.add(newTrip);
List<StopTime> newStopTimes = new ArrayList<StopTime>();
for (int i = fromIndex; i <= toIndex; ++i) {
StopTime stopTime = new StopTime(stopTimes.get(i));
stopTime.setId(0);
stopTime.setTrip(newTrip);
newStopTimes.add(stopTime);
}
updateShape(dao, newTrip, newStopTimes, newShapeIds);
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);
}
((GtfsRelationalDaoImpl) dao).clearAllCaches();
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.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class InterpolateStopTimesFromTimePointsStrategyTest method run.
@Test
public void run() throws Exception {
_gtfs.putAgencies(1);
_gtfs.putStops(7);
_gtfs.putRoutes(1);
_gtfs.putTrips(1, "r0", "sid0");
_gtfs.putStopTimesWithDistances("t0", "s0,s1,s2,s3,s4,s5,s6", "0.0,1000.0,1100.0,1200.0,1250.0,1300.0,2000.0", "1,0,1,0,0,1,1");
GtfsMutableRelationalDao dao = _gtfs.read();
_strategy.run(_context, dao);
Collection<Trip> allTrips = dao.getAllTrips();
assertEquals(1, allTrips.size());
Trip trip = allTrips.iterator().next();
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
assertEquals(7, stopTimes.size());
assertEquals(1, stopTimes.get(0).getTimepoint());
assertTrue(stopTimes.get(0).isArrivalTimeSet());
assertTrue(stopTimes.get(0).isShapeDistTraveledSet());
assertEquals(0, stopTimes.get(0).getShapeDistTraveled(), 0.001);
assertEquals(time(9, 0), stopTimes.get(0).getArrivalTime());
assertEquals(0, stopTimes.get(1).getTimepoint());
assertTrue(stopTimes.get(1).isArrivalTimeSet());
assertEquals(time(9, 9, 5), stopTimes.get(1).getArrivalTime());
assertEquals(1, stopTimes.get(2).getTimepoint());
assertTrue(stopTimes.get(2).isArrivalTimeSet());
assertEquals(time(9, 10), stopTimes.get(2).getArrivalTime());
assertEquals(0, stopTimes.get(3).getTimepoint());
assertTrue(stopTimes.get(3).isArrivalTimeSet());
assertEquals(time(9, 17, 29), stopTimes.get(3).getArrivalTime());
assertEquals(0, stopTimes.get(4).getTimepoint());
assertTrue(stopTimes.get(4).isArrivalTimeSet());
assertEquals(time(9, 21, 14), stopTimes.get(4).getArrivalTime());
assertEquals(1, stopTimes.get(5).getTimepoint());
assertTrue(stopTimes.get(5).isArrivalTimeSet());
assertEquals(time(9, 25), stopTimes.get(5).getArrivalTime());
assertEquals(1, stopTimes.get(6).getTimepoint());
assertTrue(stopTimes.get(6).isArrivalTimeSet());
assertEquals(time(9, 30), stopTimes.get(6).getArrivalTime());
}
Aggregations