use of org.onebusaway.geospatial.model.CoordinatePoint 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.geospatial.model.CoordinatePoint in project onebusaway-application-modules by camsys.
the class CurrentVehicleEstimationServiceImpl method computeCumulativeProbabilityForRealTimeBlockLocations.
private void computeCumulativeProbabilityForRealTimeBlockLocations(Map<Date, Record> recordsByTime, List<BlockLocation> locations, double minProbabilityForConsideration, List<CurrentVehicleEstimateBean> beans) {
DoubleArrayList ps = new DoubleArrayList();
for (BlockLocation location : locations) {
Date t = new Date(location.getTime());
Record record = recordsByTime.get(t);
CoordinatePoint userLocation = record.getLocation();
CoordinatePoint vehicleLocation = location.getLocation();
double d = SphericalGeometryLibrary.distance(userLocation, vehicleLocation);
double p = _realTimeLocationDeviationModel.probability(d);
ps.add(p);
}
BlockLocation last = locations.get(locations.size() - 1);
double mu = Descriptive.mean(ps);
String debug = asString(ps);
addResult(last, mu, debug, minProbabilityForConsideration, beans);
}
use of org.onebusaway.geospatial.model.CoordinatePoint in project onebusaway-application-modules by camsys.
the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationWhenAtStopTime.
private ScheduledBlockLocation getScheduledBlockLocationWhenAtStopTime(BlockStopTimeEntry blockStopTime, BlockStopTimeEntry previousBlockStopTime, StopTimeEntry stopTime, int scheduleTime, int stopTimeIndex) {
StopEntry stop = stopTime.getStop();
ScheduledBlockLocation result = new ScheduledBlockLocation();
int shapePointIndex = stopTime.getShapePointIndex();
PointAndOrientation po = getLocationAlongShape(blockStopTime.getTrip(), blockStopTime.getDistanceAlongBlock(), shapePointIndex, shapePointIndex + 1);
if (po != null) {
result.setLocation(po.getPoint());
result.setOrientation(po.getOrientation());
} else {
CoordinatePoint location = new CoordinatePoint(stop.getStopLat(), stop.getStopLon());
result.setLocation(location);
result.setOrientation(0);
}
result.setClosestStop(blockStopTime);
result.setClosestStopTimeOffset(0);
result.setNextStop(blockStopTime);
result.setNextStopTimeOffset(0);
result.setScheduledTime(scheduleTime);
result.setDistanceAlongBlock(blockStopTime.getDistanceAlongBlock());
result.setActiveTrip(blockStopTime.getTrip());
result.setInService(true);
result.setStopTimeIndex(stopTimeIndex);
// If there is more than 1 stop, grab the previous stop
if (blockStopTime.hasPreviousStop()) {
result.setPreviousStop(previousBlockStopTime);
}
return result;
}
use of org.onebusaway.geospatial.model.CoordinatePoint in project onebusaway-application-modules by camsys.
the class VehiclePositionsForAgencyActionTest method test.
@Test
public void test() {
long now = System.currentTimeMillis();
List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
RouteBean.Builder routeBuilder = RouteBean.builder();
routeBuilder.setId("1_r1");
RouteBean route = routeBuilder.create();
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(1234 * 1000);
vehicle.setVehicleId("1_v1");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
TripBean trip = new TripBean();
trip.setId("1_t0");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
vehicle.setLocation(new CoordinatePoint(47.0, -122.0));
}
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(5678 * 1000);
vehicle.setVehicleId("1_v2");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
TripBean trip = new TripBean();
trip.setId("1_t1");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
vehicle.setLocation(new CoordinatePoint(47.1, -122.1));
}
ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
bean.setList(vehicles);
Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
_action.setId("1");
_action.setTime(new Date(now));
_action.show();
ResponseBean model = _action.getModel();
FeedMessage feed = (FeedMessage) model.getData();
assertEquals(now / 1000, feed.getHeader().getTimestamp());
assertEquals(2, feed.getEntityCount());
{
FeedEntity entity = feed.getEntity(0);
assertEquals("1", entity.getId());
VehiclePosition vehiclePosition = entity.getVehicle();
assertEquals("t0", vehiclePosition.getTrip().getTripId());
assertEquals("r1", vehiclePosition.getTrip().getRouteId());
assertEquals("v1", vehiclePosition.getVehicle().getId());
assertEquals(1234, vehiclePosition.getTimestamp());
assertEquals(47.0, vehiclePosition.getPosition().getLatitude(), 0.01);
assertEquals(-122.0, vehiclePosition.getPosition().getLongitude(), 0.01);
}
{
FeedEntity entity = feed.getEntity(1);
assertEquals("2", entity.getId());
VehiclePosition vehiclePosition = entity.getVehicle();
assertEquals("t1", vehiclePosition.getTrip().getTripId());
assertEquals("r1", vehiclePosition.getTrip().getRouteId());
assertEquals("v2", vehiclePosition.getVehicle().getId());
assertEquals(5678, vehiclePosition.getTimestamp());
assertEquals(47.1, vehiclePosition.getPosition().getLatitude(), 0.01);
assertEquals(-122.1, vehiclePosition.getPosition().getLongitude(), 0.01);
}
}
use of org.onebusaway.geospatial.model.CoordinatePoint 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;
}
Aggregations