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();
}
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);
}
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);
}
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);
}
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;
}
Aggregations