Search in sources :

Example 1 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class BlockGeospatialServiceImpl method getBestScheduledBlockLocationForLocation.

public ScheduledBlockLocation getBestScheduledBlockLocationForLocation(BlockInstance blockInstance, CoordinatePoint location, long timestamp, double blockDistanceFrom, double blockDistanceTo) {
    BlockConfigurationEntry block = blockInstance.getBlock();
    ProjectedPoint targetPoint = ProjectedPointFactory.forward(location);
    List<AgencyAndId> shapePointIds = MappingLibrary.map(block.getTrips(), "trip.shapeId");
    T2<List<XYPoint>, double[]> tuple = _projectedShapePointService.getProjectedShapePoints(shapePointIds, targetPoint.getSrid());
    if (tuple == null) {
        throw new IllegalStateException("block had no shape points: " + block.getBlock().getId());
    }
    List<XYPoint> projectedShapePoints = tuple.getFirst();
    double[] distances = tuple.getSecond();
    int fromIndex = 0;
    int toIndex = distances.length;
    if (blockDistanceFrom > 0) {
        fromIndex = Arrays.binarySearch(distances, blockDistanceFrom);
        if (fromIndex < 0) {
            fromIndex = -(fromIndex + 1);
            // Include the previous point if we didn't get an exact match
            if (fromIndex > 0)
                fromIndex--;
        }
    }
    if (blockDistanceTo < distances[distances.length - 1]) {
        toIndex = Arrays.binarySearch(distances, blockDistanceTo);
        if (toIndex < 0) {
            toIndex = -(toIndex + 1);
            // Include the previous point if we didn't get an exact match
            if (toIndex < distances.length)
                toIndex++;
        }
    }
    XYPoint xyPoint = new XYPoint(targetPoint.getX(), targetPoint.getY());
    List<PointAndIndex> assignments = _shapePointsLibrary.computePotentialAssignments(projectedShapePoints, distances, xyPoint, fromIndex, toIndex);
    Min<ScheduledBlockLocation> best = new Min<ScheduledBlockLocation>();
    for (PointAndIndex index : assignments) {
        double distanceAlongBlock = index.distanceAlongShape;
        if (distanceAlongBlock > block.getTotalBlockDistance())
            distanceAlongBlock = block.getTotalBlockDistance();
        ScheduledBlockLocation blockLocation = _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(block, distanceAlongBlock);
        if (blockLocation != null) {
            int scheduledTime = blockLocation.getScheduledTime();
            long scheduleTimestamp = blockInstance.getServiceDate() + scheduledTime * 1000;
            double delta = Math.abs(scheduleTimestamp - timestamp);
            best.add(delta, blockLocation);
        }
    }
    return best.getMinElement();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) Min(org.onebusaway.collections.Min) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint) List(java.util.List) ArrayList(java.util.ArrayList) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 2 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint 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)

Example 3 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class ShapePointsLibraryTest method test02.

@Test
public void test02() {
    ShapePointsLibrary spl = new ShapePointsLibrary();
    spl.setLocalMinimumThreshold(5);
    List<XYPoint> points = new ArrayList<XYPoint>();
    points.add(p(0, 0));
    points.add(p(10, 0));
    points.add(p(10, 1));
    points.add(p(0, 1));
    double[] shapePointDistances = shapePointDistances(points);
    XYPoint target = p(1, 0.5);
    List<PointAndIndex> result = spl.computePotentialAssignments(points, shapePointDistances, target, 0, points.size());
    assertEquals(2, result.size());
    PointAndIndex pi = result.get(0);
    assertEquals(1.0, pi.point.getX(), 0.0);
    assertEquals(0.0, pi.point.getY(), 0.0);
    pi = result.get(1);
    assertEquals(1.0, pi.point.getX(), 0.0);
    assertEquals(1.0, pi.point.getY(), 0.0);
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class ShapePointsLibraryTest method test01.

@Test
public void test01() {
    ShapePointsLibrary spl = new ShapePointsLibrary();
    spl.setLocalMinimumThreshold(10);
    List<XYPoint> points = new ArrayList<XYPoint>();
    points.add(p(0, 0));
    points.add(p(1, 0));
    points.add(p(2, 0));
    points.add(p(3, 0));
    points.add(p(4, 0));
    double[] shapePointDistances = shapePointDistances(points);
    XYPoint target = p(1, 1);
    List<PointAndIndex> result = spl.computePotentialAssignments(points, shapePointDistances, target, 0, points.size());
    assertEquals(1, result.size());
    PointAndIndex pi = result.get(0);
    assertEquals(1.0, pi.point.getX(), 0.0);
    assertEquals(0.0, pi.point.getY(), 0.0);
    target = p(2, 1);
    result = spl.computePotentialAssignments(points, shapePointDistances, target, 0, points.size());
    assertEquals(1, result.size());
    pi = result.get(0);
    assertEquals(2.0, pi.point.getX(), 0.0);
    assertEquals(0.0, pi.point.getY(), 0.0);
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class ShapePointsLibrary method getProjectedShapePoints.

public List<XYPoint> getProjectedShapePoints(ShapePoints shapePoints, UTMProjection projection) {
    List<XYPoint> projectedShapePoints = new ArrayList<XYPoint>();
    double[] lats = shapePoints.getLats();
    double[] lons = shapePoints.getLons();
    int n = lats.length;
    for (int i = 0; i < n; i++) projectedShapePoints.add(projection.forward(new CoordinatePoint(lats[i], lons[i])));
    return projectedShapePoints;
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ArrayList(java.util.ArrayList) XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Aggregations

XYPoint (org.onebusaway.geospatial.model.XYPoint)22 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)15 ArrayList (java.util.ArrayList)10 PointAndIndex (org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex)6 Test (org.junit.Test)5 UTMProjection (org.onebusaway.geospatial.services.UTMProjection)5 StopTimeEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl)4 List (java.util.List)3 Min (org.onebusaway.collections.Min)3 StopEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)3 ProjectedPoint (org.onebusaway.transit_data_federation.model.ProjectedPoint)3 ShapePoints (org.onebusaway.transit_data_federation.model.ShapePoints)2 Point (com.vividsolutions.jts.geom.Point)1 Cacheable (org.onebusaway.container.cache.Cacheable)1 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 ShapePointsFactory (org.onebusaway.transit_data_federation.model.ShapePointsFactory)1 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)1 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)1