use of org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method computePredictedDepartureTimeInterval.
private TimeIntervalBean computePredictedDepartureTimeInterval(ArrivalAndDepartureInstance instance, BlockLocation blockLocation, long targetTime) {
BlockStopTimeEntry blockStopTime = instance.getBlockStopTime();
StopTimeEntry stopTime = blockStopTime.getStopTime();
// interval
if (stopTime.getDepartureTime() <= blockLocation.getEffectiveScheduleTime())
return null;
ScheduleDeviationSamples samples = blockLocation.getScheduleDeviations();
if (samples == null || samples.isEmpty())
return null;
double mu = InterpolationLibrary.interpolate(samples.getScheduleTimes(), samples.getScheduleDeviationMus(), stopTime.getDepartureTime(), EOutOfRangeStrategy.LAST_VALUE, EInRangeStrategy.INTERPOLATE);
double sigma = InterpolationLibrary.interpolate(samples.getScheduleTimes(), samples.getScheduleDeviationSigmas(), stopTime.getDepartureTime(), EOutOfRangeStrategy.LAST_VALUE, EInRangeStrategy.INTERPOLATE);
long from = (long) (instance.getScheduledDepartureTime() + (mu - sigma) * 1000);
long to = (long) (instance.getScheduledDepartureTime() + (mu + sigma) * 1000);
return new TimeIntervalBean(from, to);
}
use of org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method computePredictedArrivalTimeInterval.
private TimeIntervalBean computePredictedArrivalTimeInterval(ArrivalAndDepartureInstance instance, BlockLocation blockLocation, long targetTime) {
BlockStopTimeEntry blockStopTime = instance.getBlockStopTime();
StopTimeEntry stopTime = blockStopTime.getStopTime();
// interval
if (stopTime.getArrivalTime() <= blockLocation.getEffectiveScheduleTime())
return null;
ScheduleDeviationSamples samples = blockLocation.getScheduleDeviations();
if (samples == null || samples.isEmpty())
return null;
double mu = InterpolationLibrary.interpolate(samples.getScheduleTimes(), samples.getScheduleDeviationMus(), stopTime.getArrivalTime(), EOutOfRangeStrategy.LAST_VALUE, EInRangeStrategy.INTERPOLATE);
double sigma = InterpolationLibrary.interpolate(samples.getScheduleTimes(), samples.getScheduleDeviationSigmas(), stopTime.getArrivalTime(), EOutOfRangeStrategy.LAST_VALUE, EInRangeStrategy.INTERPOLATE);
long from = (long) (instance.getScheduledArrivalTime() + (mu - sigma) * 1000);
long to = (long) (instance.getScheduledArrivalTime() + (mu + sigma) * 1000);
return new TimeIntervalBean(from, to);
}
use of org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples in project onebusaway-application-modules by camsys.
the class RealTimeHistoryServiceImpl method sampleScheduleDeviationsForVehicle.
@Override
public ScheduleDeviationSamples sampleScheduleDeviationsForVehicle(BlockInstance instance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation) {
if (scheduledBlockLocation == null)
return null;
BlockTripEntry blockTrip = scheduledBlockLocation.getActiveTrip();
TripEntry trip = blockTrip.getTrip();
ScheduleDeviationHistory history = _scheduleDeviationHistoryDao.getScheduleDeviationHistoryForTripId(trip.getId());
if (history == null)
return null;
ScheduleDeviationHistory resampledHistory = resampleHistory(history, scheduledBlockLocation.getScheduledTime(), record.getScheduleDeviation());
DoubleArrayList scheduleTimes = new DoubleArrayList();
DoubleArrayList mus = new DoubleArrayList();
DoubleArrayList sigmas = new DoubleArrayList();
for (int t = 0; t <= _predictionLookahead; t += 5 * 60) {
int scheduleTime = scheduledBlockLocation.getScheduledTime() + t;
double[] deviations = getScheduleDeviationsForScheduleTime(resampledHistory, scheduleTime);
deviations = noNans(deviations);
DoubleArrayList values = new DoubleArrayList(deviations);
double mu = Descriptive.mean(values);
double var = Descriptive.sampleVariance(values, mu);
double sigma = Descriptive.sampleStandardDeviation(values.size(), var);
scheduleTimes.add(scheduleTime);
mus.add(mu);
sigmas.add(sigma);
}
scheduleTimes.trimToSize();
mus.trimToSize();
sigmas.trimToSize();
return new ScheduleDeviationSamples(scheduleTimes.elements(), mus.elements(), sigmas.elements());
}
use of org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBlockLocation.
/**
* @param blockInstance
* @param cacheElements
* @param scheduledLocation
* @param targetTime
* @return null if the effective scheduled block location cannot be determined
*/
private BlockLocation getBlockLocation(BlockInstance blockInstance, VehicleLocationCacheElements cacheElements, ScheduledBlockLocation scheduledLocation, long targetTime) {
BlockLocation location = new BlockLocation();
location.setTime(targetTime);
location.setBlockInstance(blockInstance);
VehicleLocationCacheElement cacheElement = null;
if (cacheElements != null)
cacheElement = cacheElements.getElementForTimestamp(targetTime);
if (cacheElement != null) {
VehicleLocationRecord record = cacheElement.getRecord();
if (scheduledLocation == null)
scheduledLocation = getScheduledBlockLocationForVehicleLocationCacheRecord(blockInstance, cacheElement, targetTime);
if (scheduledLocation != null) {
location.setEffectiveScheduleTime(scheduledLocation.getScheduledTime());
location.setDistanceAlongBlock(scheduledLocation.getDistanceAlongBlock());
}
location.setBlockStartTime(record.getBlockStartTime());
location.setPredicted(true);
location.setLastUpdateTime(record.getTimeOfRecord());
location.setLastLocationUpdateTime(record.getTimeOfLocationUpdate());
location.setScheduleDeviation(record.getScheduleDeviation());
location.setScheduleDeviations(cacheElement.getScheduleDeviations());
if (record.isCurrentLocationSet()) {
CoordinatePoint p = new CoordinatePoint(record.getCurrentLocationLat(), record.getCurrentLocationLon());
location.setLastKnownLocation(p);
}
location.setOrientation(record.getCurrentOrientation());
location.setPhase(record.getPhase());
location.setStatus(record.getStatus());
location.setVehicleId(record.getVehicleId());
List<TimepointPredictionRecord> timepointPredictions = record.getTimepointPredictions();
location.setTimepointPredictions(timepointPredictions);
if (timepointPredictions != null && !timepointPredictions.isEmpty()) {
SortedMap<Integer, Double> scheduleDeviations = new TreeMap<Integer, Double>();
BlockConfigurationEntry blockConfig = blockInstance.getBlock();
int tprIndexCounter = 0;
for (TimepointPredictionRecord tpr : timepointPredictions) {
AgencyAndId stopId = tpr.getTimepointId();
long predictedTime;
if (tpr.getTimepointPredictedDepartureTime() != -1) {
predictedTime = tpr.getTimepointPredictedDepartureTime();
} else {
predictedTime = tpr.getTimepointPredictedArrivalTime();
}
if (stopId == null || predictedTime == 0)
continue;
for (BlockStopTimeEntry blockStopTime : blockConfig.getStopTimes()) {
StopTimeEntry stopTime = blockStopTime.getStopTime();
StopEntry stop = stopTime.getStop();
// StopSequence equals to -1 when there is no stop sequence in the GTFS-rt
if (stopId.equals(stop.getId()) && stopTime.getTrip().getId().equals(tpr.getTripId()) && (tpr.getStopSequence() == -1 || stopTime.getSequence() == tpr.getStopSequence())) {
if (tpr.getStopSequence() == -1 && isFirstOrLastStopInTrip(stopTime) && isLoopRoute(stopTime)) {
if (isSinglePredictionForTrip(timepointPredictions, tpr, tprIndexCounter)) {
continue;
}
// If this isn't the last prediction, and we're on the first stop, then apply it
if (isLastPrediction(stopTime, timepointPredictions, tpr, tprIndexCounter) && isFirstStopInRoute(stopTime)) {
// Do not calculate schedule deviation
continue;
}
// If this is the last prediction, and we're on the last stop, then apply it
if (isFirstPrediction(stopTime, timepointPredictions, tpr, tprIndexCounter) && isLastStopInRoute(stopTime)) {
// Do not calculate schedule deviation
continue;
}
}
int arrivalOrDepartureTime;
// We currently use the scheduled arrival time of the stop as the search index
// This MUST be consistent with the index search in ArrivalAndSepartureServiceImpl.getBestScheduleDeviation()
int index = stopTime.getArrivalTime();
if (tpr.getTimepointPredictedDepartureTime() != -1) {
// Prefer departure time, because if both exist departure deviations should be the ones propagated downstream
arrivalOrDepartureTime = stopTime.getDepartureTime();
} else {
arrivalOrDepartureTime = stopTime.getArrivalTime();
}
int deviation = (int) ((predictedTime - blockInstance.getServiceDate()) / 1000 - arrivalOrDepartureTime);
scheduleDeviations.put(index, (double) deviation);
}
}
tprIndexCounter++;
}
double[] scheduleTimes = new double[scheduleDeviations.size()];
double[] scheduleDeviationMus = new double[scheduleDeviations.size()];
double[] scheduleDeviationSigmas = new double[scheduleDeviations.size()];
int index = 0;
for (Map.Entry<Integer, Double> entry : scheduleDeviations.entrySet()) {
scheduleTimes[index] = entry.getKey();
scheduleDeviationMus[index] = entry.getValue();
index++;
}
ScheduleDeviationSamples samples = new ScheduleDeviationSamples(scheduleTimes, scheduleDeviationMus, scheduleDeviationSigmas);
location.setScheduleDeviations(samples);
}
} else {
if (scheduledLocation == null)
scheduledLocation = getScheduledBlockLocationForBlockInstance(blockInstance, targetTime);
}
/**
* Will be null in the following cases:
*
* 1) When the effective schedule time is beyond the last scheduled stop
* time for the block.
*
* 2) When the effective distance along block is outside the range of the
* block's shape.
*/
if (scheduledLocation == null)
return null;
// if we have route info, set the vehicleType
if (scheduledLocation.getActiveTrip() != null && scheduledLocation.getActiveTrip().getTrip() != null && scheduledLocation.getActiveTrip().getTrip().getRoute() != null) {
location.setVehicleType(EVehicleType.toEnum(scheduledLocation.getActiveTrip().getTrip().getRoute().getType()));
}
location.setInService(scheduledLocation.isInService());
location.setActiveTrip(scheduledLocation.getActiveTrip());
location.setLocation(scheduledLocation.getLocation());
location.setOrientation(scheduledLocation.getOrientation());
location.setScheduledDistanceAlongBlock(scheduledLocation.getDistanceAlongBlock());
location.setClosestStop(scheduledLocation.getClosestStop());
location.setClosestStopTimeOffset(scheduledLocation.getClosestStopTimeOffset());
location.setNextStop(scheduledLocation.getNextStop());
location.setNextStopTimeOffset(scheduledLocation.getNextStopTimeOffset());
location.setPreviousStop(scheduledLocation.getPreviousStop());
return location;
}
use of org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method handleVehicleLocationRecord.
/**
**
* {@link BlockVehicleLocationListener} Interface
***
*/
@Override
public void handleVehicleLocationRecord(VehicleLocationRecord record) {
BlockInstance instance = getVehicleLocationRecordAsBlockInstance(record);
if (instance != null) {
ScheduledBlockLocation scheduledBlockLocation = getScheduledBlockLocationForVehicleLocationRecord(record, instance);
if (!record.isScheduleDeviationSet()) {
int deviation = (int) ((record.getTimeOfRecord() - record.getServiceDate()) / 1000 - scheduledBlockLocation.getScheduledTime());
record.setScheduleDeviation(deviation);
}
ScheduleDeviationSamples samples = null;
if (_sampleScheduleDeviationHistory == true) {
samples = _realTimeHistoryService.sampleScheduleDeviationsForVehicle(instance, record, scheduledBlockLocation);
}
putBlockLocationRecord(instance, record, scheduledBlockLocation, samples);
}
}
Aggregations