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