Search in sources :

Example 1 with ShapePoints

use of org.onebusaway.transit_data_federation.model.ShapePoints in project onebusaway-application-modules by camsys.

the class ShapeBeanServiceImpl method getMergedPolylinesForShapeIds.

@Cacheable
public List<EncodedPolylineBean> getMergedPolylinesForShapeIds(Collection<AgencyAndId> shapeIds) {
    List<EncodedPolylineBean> polylines = new ArrayList<EncodedPolylineBean>();
    if (shapeIds.isEmpty())
        return polylines;
    List<CoordinatePoint> currentLine = new ArrayList<CoordinatePoint>();
    Set<Edge> edges = new HashSet<Edge>();
    for (AgencyAndId shapeId : shapeIds) {
        ShapePoints shapePoints = _shapePointService.getShapePointsForShapeId(shapeId);
        if (shapePoints == null) {
            _log.warn("no shape points for shapeId=" + shapeId);
            continue;
        }
        double[] lats = shapePoints.getLats();
        double[] lons = shapePoints.getLons();
        CoordinatePoint prev = null;
        for (int i = 0; i < shapePoints.getSize(); i++) {
            CoordinatePoint loc = new CoordinatePoint(lats[i], lons[i]);
            if (prev != null && !prev.equals(loc)) {
                Edge edge = new Edge(prev, loc);
                if (!edges.add(edge)) {
                    if (currentLine.size() > 1)
                        polylines.add(PolylineEncoder.createEncodings(currentLine));
                    currentLine.clear();
                }
            }
            if (prev == null || !prev.equals(loc))
                currentLine.add(loc);
            prev = loc;
        }
        if (currentLine.size() > 1)
            polylines.add(PolylineEncoder.createEncodings(currentLine));
        currentLine.clear();
    }
    return polylines;
}
Also used : ShapePoints(org.onebusaway.transit_data_federation.model.ShapePoints) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) EncodedPolylineBean(org.onebusaway.geospatial.model.EncodedPolylineBean) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) HashSet(java.util.HashSet) Cacheable(org.onebusaway.container.cache.Cacheable)

Example 2 with ShapePoints

use of org.onebusaway.transit_data_federation.model.ShapePoints in project onebusaway-application-modules by camsys.

the class ScheduledBlockLocationServiceImpl method getLocationAlongShape.

private PointAndOrientation getLocationAlongShape(BlockTripEntry activeBlockTrip, double distanceAlongBlock, int shapePointIndexFrom, int shapePointIndexTo) {
    TripEntry activeTrip = activeBlockTrip.getTrip();
    AgencyAndId shapeId = activeTrip.getShapeId();
    if (shapeId == null)
        return null;
    ShapePoints shapePoints = _shapePointService.getShapePointsForShapeId(shapeId);
    if (shapePoints == null || shapePoints.isEmpty())
        return null;
    /**
     * We allow callers of this method to specify an arbitrarily high
     * shapePointIndexTo, knowing we'll bound it by the max number of points
     */
    shapePointIndexFrom = Math.min(shapePointIndexFrom, shapePoints.getSize());
    shapePointIndexTo = Math.min(shapePointIndexTo, shapePoints.getSize());
    double distanceAlongTrip = distanceAlongBlock - activeBlockTrip.getDistanceAlongBlock();
    ShapePointIndex shapePointIndexMethod = new DistanceTraveledShapePointIndex(distanceAlongTrip, shapePointIndexFrom, shapePointIndexTo);
    return shapePointIndexMethod.getPointAndOrientation(shapePoints);
}
Also used : ShapePoints(org.onebusaway.transit_data_federation.model.ShapePoints) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) DistanceTraveledShapePointIndex(org.onebusaway.transit_data_federation.impl.shapes.DistanceTraveledShapePointIndex) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ShapePointIndex(org.onebusaway.transit_data_federation.impl.shapes.ShapePointIndex) DistanceTraveledShapePointIndex(org.onebusaway.transit_data_federation.impl.shapes.DistanceTraveledShapePointIndex)

Example 3 with ShapePoints

use of org.onebusaway.transit_data_federation.model.ShapePoints in project onebusaway-application-modules by camsys.

the class ShapeGeospatialIndexTask method buildShapeSpatialIndex.

private Map<CoordinateBounds, List<AgencyAndId>> buildShapeSpatialIndex() {
    Map<CoordinatePoint, Set<AgencyAndId>> shapeIdsByGridCellCorner = new FactoryMap<CoordinatePoint, Set<AgencyAndId>>(new HashSet<AgencyAndId>());
    CoordinateBounds fullBounds = new CoordinateBounds();
    for (StopEntry stop : _transitGraphDao.getAllStops()) {
        if (stop.getStopLat() > MIN_LAT_LON && stop.getStopLon() > MIN_LAT_LON && stop.getStopLat() < MAX_LAT_LON && stop.getStopLon() < MAX_LAT_LON) {
            fullBounds.addPoint(stop.getStopLat(), stop.getStopLon());
        } else {
            _log.error("rejecting stop " + stop + " for invalid (lat,lon)=" + stop.getStopLat() + ", " + stop.getStopLon());
        }
    }
    if (fullBounds.isEmpty()) {
        return Collections.emptyMap();
    }
    double centerLat = (fullBounds.getMinLat() + fullBounds.getMaxLat()) / 2;
    double centerLon = (fullBounds.getMinLon() + fullBounds.getMaxLon()) / 2;
    CoordinateBounds gridCellExample = SphericalGeometryLibrary.bounds(centerLat, centerLon, _gridSize / 2);
    double latStep = gridCellExample.getMaxLat() - gridCellExample.getMinLat();
    double lonStep = gridCellExample.getMaxLon() - gridCellExample.getMinLon();
    _log.info("generating shape point geospatial index...");
    Set<AgencyAndId> allShapeIds = getAllShapeIds();
    for (AgencyAndId shapeId : allShapeIds) {
        ShapePoints shapePoints = _shapePointHelper.getShapePointsForShapeId(shapeId);
        for (int i = 0; i < shapePoints.getSize(); i++) {
            double lat = shapePoints.getLatForIndex(i);
            double lon = shapePoints.getLonForIndex(i);
            addGridCellForShapePoint(shapeIdsByGridCellCorner, lat, lon, latStep, lonStep, shapeId);
            /**
             * If there is a particularly long stretch between shape points, we want
             * to fill in grid cells in-between
             */
            if (i > 0) {
                double prevLat = shapePoints.getLatForIndex(i - 1);
                double prevLon = shapePoints.getLonForIndex(i - 1);
                double totalDistance = SphericalGeometryLibrary.distance(prevLat, prevLon, lat, lon);
                for (double d = _gridSize; d < totalDistance; d += _gridSize) {
                    double r = d / totalDistance;
                    double latPart = (lat - prevLat) * r + prevLat;
                    double lonPart = (lon - prevLon) * r + prevLon;
                    addGridCellForShapePoint(shapeIdsByGridCellCorner, latPart, lonPart, latStep, lonStep, shapeId);
                }
            }
        }
    }
    _log.info("block shape geospatial nodes: " + shapeIdsByGridCellCorner.size());
    Map<CoordinateBounds, List<AgencyAndId>> shapeIdsByGridCell = new HashMap<CoordinateBounds, List<AgencyAndId>>();
    for (Map.Entry<CoordinatePoint, Set<AgencyAndId>> entry : shapeIdsByGridCellCorner.entrySet()) {
        CoordinatePoint p = entry.getKey();
        CoordinateBounds bounds = new CoordinateBounds(p.getLat(), p.getLon(), p.getLat() + latStep, p.getLon() + lonStep);
        List<AgencyAndId> shapeIds = new ArrayList<AgencyAndId>(entry.getValue());
        shapeIdsByGridCell.put(bounds, shapeIds);
    }
    return shapeIdsByGridCell;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) HashSet(java.util.HashSet) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ShapePoints(org.onebusaway.transit_data_federation.model.ShapePoints) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 4 with ShapePoints

use of org.onebusaway.transit_data_federation.model.ShapePoints in project onebusaway-application-modules by camsys.

the class ShapePointHelper method getShapePointsForShapeIdNonCached.

private ShapePoints getShapePointsForShapeIdNonCached(AgencyAndId shapeId) {
    List<ShapePoint> shapePoints = _gtfsDao.getShapePointsForShapeId(shapeId);
    shapePoints = deduplicateShapePoints(shapePoints);
    if (shapePoints.isEmpty())
        return null;
    int n = shapePoints.size();
    double[] lat = new double[n];
    double[] lon = new double[n];
    double[] distTraveled = new double[n];
    int i = 0;
    for (ShapePoint shapePoint : shapePoints) {
        lat[i] = shapePoint.getLat();
        lon[i] = shapePoint.getLon();
        i++;
    }
    ShapePoints result = new ShapePoints();
    result.setShapeId(shapeId);
    result.setLats(lat);
    result.setLons(lon);
    result.setDistTraveled(distTraveled);
    result.ensureDistTraveled();
    return result;
}
Also used : ShapePoints(org.onebusaway.transit_data_federation.model.ShapePoints) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint)

Example 5 with ShapePoints

use of org.onebusaway.transit_data_federation.model.ShapePoints in project onebusaway-application-modules by camsys.

the class ShapePointsLibraryTest method test04.

@Test
public void test04() {
    ShapePointsFactory factory = new ShapePointsFactory();
    factory.addPoint(47.66851509562011, -122.29019398384474);
    factory.addPoint(47.66486634286269, -122.29014033966445);
    factory.addPoint(47.66486634286269, -122.29560131721877);
    ShapePoints shapePoints = factory.create();
    UTMProjection projection = UTMLibrary.getProjectionForPoint(shapePoints.getLatForIndex(0), shapePoints.getLonForIndex(0));
    ShapePointsLibrary spl = new ShapePointsLibrary();
    List<XYPoint> projectedShapePoints = spl.getProjectedShapePoints(shapePoints, projection);
    XYPoint stopPoint = projection.forward(new CoordinatePoint(47.664922340500475, -122.29066873484038));
    double[] distanceAlongShape = { 0.0, 405.7, 814.0 };
    List<PointAndIndex> assignments = spl.computePotentialAssignments(projectedShapePoints, distanceAlongShape, stopPoint, 0, 3);
    assertEquals(2, assignments.size());
    PointAndIndex assignment = assignments.get(0);
    assertEquals(398.9, assignment.distanceAlongShape, 0.1);
    assertEquals(39.6, assignment.distanceFromTarget, 0.1);
    assignment = assignments.get(1);
    assertEquals(445.4, assignment.distanceAlongShape, 0.1);
    assertEquals(6.2, assignment.distanceFromTarget, 0.1);
}
Also used : ShapePoints(org.onebusaway.transit_data_federation.model.ShapePoints) XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ShapePointsFactory(org.onebusaway.transit_data_federation.model.ShapePointsFactory) UTMProjection(org.onebusaway.geospatial.services.UTMProjection) Test(org.junit.Test)

Aggregations

ShapePoints (org.onebusaway.transit_data_federation.model.ShapePoints)26 Test (org.junit.Test)13 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)12 StopTimeEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl)10 ShapePointsFactory (org.onebusaway.transit_data_federation.model.ShapePointsFactory)9 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)8 Stop (org.onebusaway.gtfs.model.Stop)6 Agency (org.onebusaway.gtfs.model.Agency)5 StopTime (org.onebusaway.gtfs.model.StopTime)5 Route (org.onebusaway.gtfs.model.Route)4 Trip (org.onebusaway.gtfs.model.Trip)4 DistanceAlongShapeLibrary (org.onebusaway.transit_data_federation.bundle.tasks.transit_graph.DistanceAlongShapeLibrary)4 PointAndIndex (org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex)4 TransitGraphImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TransitGraphImpl)4 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)4 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)4 ArrayList (java.util.ArrayList)3 Cacheable (org.onebusaway.container.cache.Cacheable)3 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)3 StopTimeEntriesFactory (org.onebusaway.transit_data_federation.bundle.tasks.transit_graph.StopTimeEntriesFactory)3