use of org.onebusaway.gtfs.model.AgencyAndId 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.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeTransformStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
String agencyId = context.getDefaultAgencyId();
AgencyAndId ashapeId = new AgencyAndId(agencyId, shapeId);
List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(ashapeId);
if (shapePoints.isEmpty()) {
// this cannot be a logger as it is BeanWrapped
System.err.println("no points found for shape: " + ashapeId);
return;
}
// Duplicate the list into something we can modify
shapePoints = new ArrayList<ShapePoint>(shapePoints);
List<ShapePoint> segment = decode(shape);
ShapePoint from = segment.get(0);
ShapePoint to = segment.get(segment.size() - 1);
int fromIndex = 0;
int toIndex = shapePoints.size() - 1;
if (matchStart)
fromIndex = closest(shapePoints, from, 0);
if (matchEnd)
toIndex = closest(shapePoints, to, fromIndex);
if (toIndex < fromIndex) {
// this cannot be a logger as it is BeanWrapped
System.err.println("segment match is out of order: fromIndex=" + fromIndex + " toIndex=" + toIndex);
return;
}
List<ShapePoint> subList = shapePoints.subList(fromIndex, toIndex + 1);
for (ShapePoint point : subList) dao.removeEntity(point);
subList.clear();
subList.addAll(segment);
int index = 0;
for (ShapePoint point : shapePoints) {
point.setDistTraveled(ShapePoint.MISSING_VALUE);
point.setSequence(index++);
point.setShapeId(ashapeId);
}
for (ShapePoint point : segment) dao.saveEntity(point);
UpdateLibrary.clearDaoCache(dao);
}
use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class StopSequencePattern method getPatternForStopTimes.
public static StopSequencePattern getPatternForStopTimes(List<StopTime> stopTimes) {
int n = stopTimes.size();
AgencyAndId[] stopIds = new AgencyAndId[n];
int[] arrivalTimes = new int[n];
int[] departureTimes = new int[n];
for (int i = 0; i < n; i++) {
StopTime stopTime = stopTimes.get(i);
stopIds[i] = stopTime.getStop().getId();
arrivalTimes[i] = stopTime.getArrivalTime();
departureTimes[i] = stopTime.getDepartureTime();
}
return new StopSequencePattern(stopIds, arrivalTimes, departureTimes);
}
use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class StopTimesFactoryStrategy method getStops.
private List<Stop> getStops(GtfsReaderContext context, GtfsMutableRelationalDao dao) {
List<Stop> stops = new ArrayList<Stop>();
for (String stopId : stopIds) {
String agencyId = context.getAgencyForEntity(Stop.class, stopId);
AgencyAndId id = new AgencyAndId(agencyId, stopId);
Stop stop = dao.getStopForId(id);
if (stop == null) {
throw new IllegalArgumentException("unknown stop: " + stopId);
}
stops.add(stop);
}
return stops;
}
use of org.onebusaway.gtfs.model.AgencyAndId 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);
}
}
}
Aggregations