Search in sources :

Example 6 with ShapePoint

use of org.onebusaway.gtfs.model.ShapePoint 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)

Example 7 with ShapePoint

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

the class ShapePointMergeStrategy method saveElementsForKey.

@Override
protected void saveElementsForKey(GtfsMergeContext context, AgencyAndId shapeId) {
    GtfsRelationalDao source = context.getSource();
    GtfsMutableRelationalDao target = context.getTarget();
    for (ShapePoint shapePoint : source.getShapePointsForShapeId(shapeId)) {
        shapePoint.setId(0);
        shapePoint.setSequence(context.getNextSequenceCounter());
        target.saveEntity(shapePoint);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsRelationalDao(org.onebusaway.gtfs.services.GtfsRelationalDao) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 8 with ShapePoint

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

the class TrimTripTransformStrategy method getClosestShapePointToStop.

private int getClosestShapePointToStop(List<ShapePoint> points, Stop stop) {
    int minIndex = -1;
    double minDistance = Double.POSITIVE_INFINITY;
    for (int i = 0; i < points.size(); ++i) {
        ShapePoint point = points.get(i);
        double dx = point.getLon() - stop.getLon();
        double dy = point.getLat() - stop.getLat();
        double d = Math.sqrt(dx * dx + dy * dy);
        if (d < minDistance) {
            minIndex = i;
            minDistance = d;
        }
    }
    return minIndex;
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 9 with ShapePoint

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

the class TrimTripTransformStrategy method updateShape.

private void updateShape(GtfsMutableRelationalDao dao, Trip trip, List<StopTime> stopTimes, Set<AgencyAndId> newShapeIds) {
    if (stopTimes.size() < 2) {
        trip.setShapeId(null);
        return;
    }
    AgencyAndId shapeId = trip.getShapeId();
    if (shapeId == null || !shapeId.hasValues()) {
        return;
    }
    List<ShapePoint> points = dao.getShapePointsForShapeId(shapeId);
    if (points.isEmpty()) {
        return;
    }
    Stop firstStop = stopTimes.get(0).getStop();
    Stop lastStop = stopTimes.get(stopTimes.size() - 1).getStop();
    String id = shapeId.getId() + "-" + firstStop.getId().getId() + "-" + lastStop.getId().getId();
    AgencyAndId newShapeId = new AgencyAndId(shapeId.getAgencyId(), id);
    trip.setShapeId(newShapeId);
    if (!newShapeIds.add(newShapeId)) {
        return;
    }
    int shapePointFrom = getClosestShapePointToStop(points, firstStop);
    int shapePointTo = getClosestShapePointToStop(points, lastStop);
    for (int index = shapePointFrom; index <= shapePointTo; ++index) {
        ShapePoint point = new ShapePoint(points.get(index));
        point.setId(0);
        point.setShapeId(newShapeId);
        dao.saveEntity(point);
    }
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 10 with ShapePoint

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

the class TrimTripTransformStrategyTest method testShapeTrimming.

@Test
public void testShapeTrimming() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putLines("stops.txt", "stop_id,stop_name,stop_lat,stop_lon", "s0,S0,47.668422,-122.290149", "s1,S1,47.670300,-122.290235", "s2,S2,47.672172,-122.290213", "s3,S3,47.673985,-122.290277", "s4,S4,47.675791,-122.290213", "s5,S5,47.677626,-122.290320");
    _gtfs.putRoutes(1);
    _gtfs.putTrips(1, "r0", "sid0", "shape_id=shape0");
    _gtfs.putStopTimes("t0", "s0,s1,s2,s3,s4,s5");
    _gtfs.putLines("shapes.txt", "shape_id,shape_pt_sequence,shape_pt_lat,shape_pt_lon", "shape0,0,47.668422,-122.290149", "shape0,1,47.670300,-122.290235", "shape0,2,47.672172,-122.290213", "shape0,3,47.673985,-122.290277", "shape0,4,47.675791,-122.290213", "shape0,5,47.677626,-122.290320");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TrimOperation operation = new TrimOperation();
    operation.setMatch(new TypedEntityMatch(Trip.class, new AlwaysMatch()));
    operation.setToStopId("s0");
    operation.setFromStopId("s4");
    _strategy.addOperation(operation);
    _strategy.run(_context, dao);
    Trip trip = dao.getTripForId(new AgencyAndId("a0", "t0-s0-s4"));
    assertEquals(new AgencyAndId("a0", "shape0-s1-s3"), trip.getShapeId());
    List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(trip.getShapeId());
    assertEquals(3, shapePoints.size());
    assertEquals(47.670300, shapePoints.get(0).getLat(), 1e-6);
    assertEquals(47.673985, shapePoints.get(2).getLat(), 1e-6);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) Trip(org.onebusaway.gtfs.model.Trip) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) AlwaysMatch(org.onebusaway.gtfs_transformer.match.AlwaysMatch) Test(org.junit.Test)

Aggregations

ShapePoint (org.onebusaway.gtfs.model.ShapePoint)21 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)12 Trip (org.onebusaway.gtfs.model.Trip)10 Test (org.junit.Test)7 Stop (org.onebusaway.gtfs.model.Stop)7 Agency (org.onebusaway.gtfs.model.Agency)6 StopTime (org.onebusaway.gtfs.model.StopTime)6 Route (org.onebusaway.gtfs.model.Route)5 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)5 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)5 ArrayList (java.util.ArrayList)4 FareAttribute (org.onebusaway.gtfs.model.FareAttribute)4 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)4 FareRule (org.onebusaway.gtfs.model.FareRule)3 Frequency (org.onebusaway.gtfs.model.Frequency)3 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)3 HashSet (java.util.HashSet)2 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)2 Transfer (org.onebusaway.gtfs.model.Transfer)2 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)2