Search in sources :

Example 11 with AgencyAndId

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);
        }
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 12 with AgencyAndId

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);
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 13 with AgencyAndId

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);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 14 with AgencyAndId

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) ArrayList(java.util.ArrayList)

Example 15 with AgencyAndId

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);
        }
    }
}
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) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) StopTime(org.onebusaway.gtfs.model.StopTime) HashSet(java.util.HashSet)

Aggregations

AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)102 Test (org.junit.Test)63 Trip (org.onebusaway.gtfs.model.Trip)37 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)25 Stop (org.onebusaway.gtfs.model.Stop)24 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)17 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)16 ArrayList (java.util.ArrayList)15 Route (org.onebusaway.gtfs.model.Route)15 StopTime (org.onebusaway.gtfs.model.StopTime)15 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)15 Agency (org.onebusaway.gtfs.model.Agency)13 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)12 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)11 HashSet (java.util.HashSet)10 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)10 List (java.util.List)9 Frequency (org.onebusaway.gtfs.model.Frequency)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)7 Set (java.util.Set)6