Search in sources :

Example 1 with ShapePoint

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

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

the class ShapeTransformStrategy method decode.

private List<ShapePoint> decode(String pointString) {
    double lat = 0;
    double lon = 0;
    int strIndex = 0;
    List<ShapePoint> points = new ArrayList<ShapePoint>();
    while (strIndex < pointString.length()) {
        int[] rLat = decodeSignedNumberWithIndex(pointString, strIndex);
        lat = lat + rLat[0] * 1e-5;
        strIndex = rLat[1];
        int[] rLon = decodeSignedNumberWithIndex(pointString, strIndex);
        lon = lon + rLon[0] * 1e-5;
        strIndex = rLon[1];
        ShapePoint point = new ShapePoint();
        point.setLat(lat);
        point.setLon(lon);
        points.add(point);
    }
    return points;
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ArrayList(java.util.ArrayList) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 3 with ShapePoint

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

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

the class ShapeTransformStrategy method closest.

private int closest(List<ShapePoint> shapePoints, ShapePoint point, int index) {
    int minIndex = -1;
    double minValue = Double.POSITIVE_INFINITY;
    for (int i = index; i < shapePoints.size(); i++) {
        ShapePoint p = shapePoints.get(i);
        double dy = p.getLat() - point.getLat();
        double dx = p.getLon() - point.getLon();
        double d = Math.sqrt(dy * dy + dx * dx);
        if (d < minValue) {
            minIndex = i;
            minValue = d;
        }
    }
    return minIndex;
}
Also used : ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 5 with ShapePoint

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

the class SubsectionTripTransformStrategy 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)

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