use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation 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.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method applyVehiclePositionToRecord.
private void applyVehiclePositionToRecord(MonitoredResult result, BlockDescriptor blockDescriptor, VehiclePosition vehiclePosition, VehicleLocationRecord record) {
Position position = vehiclePosition.getPosition();
if (vehiclePosition.hasTimestamp()) {
// vehicle timestamp is in seconds
record.setTimeOfLocationUpdate(TimeUnit.SECONDS.toMillis(vehiclePosition.getTimestamp()));
}
record.setCurrentLocationLat(position.getLatitude());
record.setCurrentLocationLon(position.getLongitude());
if (result != null) {
result.addLatLon(position.getLatitude(), position.getLongitude());
}
if (_scheduleAdherenceFromLocation) {
CoordinatePoint location = new CoordinatePoint(position.getLatitude(), position.getLongitude());
double totalDistance = blockDescriptor.getBlockInstance().getBlock().getTotalBlockDistance();
long timestamp = vehiclePosition.hasTimestamp() ? record.getTimeOfLocationUpdate() : record.getTimeOfRecord();
ScheduledBlockLocation loc = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockDescriptor.getBlockInstance(), location, timestamp, 0, totalDistance);
long serviceDateTime = record.getServiceDate();
long effectiveScheduleTime = loc.getScheduledTime() + (serviceDateTime / 1000);
double deviation = timestamp / 1000 - effectiveScheduleTime;
record.setScheduleDeviation(deviation);
}
}
use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.
the class BlockBeanServiceImpl method getScheduledBlockLocationFromScheduledTime.
@Override
public ScheduledBlockLocationBean getScheduledBlockLocationFromScheduledTime(AgencyAndId blockId, long serviceDate, int scheduledTime) {
BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
if (blockInstance == null)
return null;
ScheduledBlockLocation blockLocation = _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockInstance.getBlock(), scheduledTime);
if (blockLocation == null)
return null;
return getBlockLocationAsBean(blockLocation);
}
use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getScheduledBlockLocationForVehicleLocationCacheRecord.
private ScheduledBlockLocation getScheduledBlockLocationForVehicleLocationCacheRecord(BlockInstance blockInstance, VehicleLocationCacheElement cacheElement, long targetTime) {
VehicleLocationRecord record = cacheElement.getRecord();
ScheduledBlockLocation scheduledBlockLocation = cacheElement.getScheduledBlockLocation();
BlockConfigurationEntry blockConfig = blockInstance.getBlock();
long serviceDate = blockInstance.getServiceDate();
int scheduledTime = (int) ((targetTime - serviceDate) / 1000);
/**
* If location interpolation has been turned off, then we assume the vehicle
* is at its last known location, so we return that if it's been stored with
* the cache element.
*/
if (!_locationInterpolation && scheduledBlockLocation != null) {
return scheduledBlockLocation;
}
if (record.isScheduleDeviationSet()) {
/**
* Effective scheduled time is the point that a transit vehicle is at on
* its schedule, with schedule deviation taken into account. So if it's
* 100 minutes into the current service date and the bus is running 10
* minutes late, it's actually at the 90 minute point in its scheduled
* operation.
*/
int effectiveScheduledTime = (int) (scheduledTime - record.getScheduleDeviation());
if (scheduledBlockLocation != null && scheduledBlockLocation.getScheduledTime() <= effectiveScheduledTime) {
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(scheduledBlockLocation, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
}
if (record.isDistanceAlongBlockSet()) {
if ((_locationInterpolation || _distanceAlongBlockLocationInterpolation) && scheduledBlockLocation != null && scheduledBlockLocation.getDistanceAlongBlock() <= record.getDistanceAlongBlock()) {
int ellapsedTime = (int) ((targetTime - record.getTimeOfRecord()) / 1000);
if (ellapsedTime >= 0) {
int effectiveScheduledTime = scheduledBlockLocation.getScheduledTime() + ellapsedTime;
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(scheduledBlockLocation, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(blockConfig, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, scheduledTime);
}
use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.
the class CurrentVehicleEstimationServiceImpl method updateProbabilitiesWithScheduleDeviations.
private void updateProbabilitiesWithScheduleDeviations(List<Record> records, List<ScheduledBlockLocation> blockLocations, DoubleArrayList ps) {
if (records.size() != blockLocations.size())
throw new IllegalStateException();
if (records.size() != ps.size())
throw new IllegalStateException();
for (int i = 1; i < records.size(); i++) {
Record prevRecord = records.get(i - 1);
Record nextRecord = records.get(i);
long recordDeltaT = (nextRecord.getTimestamp() - prevRecord.getTimestamp());
if (recordDeltaT <= 0)
continue;
int maxTravelBackwardsTime = computeMaxTravelBackwardsTime(recordDeltaT);
ScheduledBlockLocation prevLocation = blockLocations.get(i - 1);
ScheduledBlockLocation nextLocation = blockLocations.get(i);
int locationDeltaT = nextLocation.getScheduledTime() - prevLocation.getScheduledTime();
if (locationDeltaT < 0 && Math.abs(locationDeltaT) > maxTravelBackwardsTime)
ps.set(i, 0.0);
}
}
Aggregations