use of org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry 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.transit_graph.StopTimeEntry in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method isFirstPrediction.
/**
* @param stopTime is the current stop
* @param timepointPredictions is the all time-point predictions in the block
* @param timepointPredictionRecord is the current tpr for the stop
* @param index is the index of the current tpr in timepointPredictions
* @return true if the given tpr is the first prediction for the trip
*/
private boolean isFirstPrediction(StopTimeEntry stopTime, List<TimepointPredictionRecord> timepointPredictions, TimepointPredictionRecord timepointPredictionRecord, int index) {
List<StopTimeEntry> stopTimes = stopTime.getTrip().getStopTimes();
AgencyAndId firstStopId = stopTimes.get(0).getStop().getId();
if (firstStopId.equals(timepointPredictionRecord.getTimepointId()) && stopTime.getTrip().getId().equals(timepointPredictionRecord.getTripId())) {
return index == 0 || (index > 0 && !timepointPredictions.get(index - 1).getTripId().equals(timepointPredictionRecord.getTripId()));
}
return false;
}
use of org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method getBlockStopTimeForStopTimeUpdate.
private BlockStopTimeEntry getBlockStopTimeForStopTimeUpdate(MonitoredResult result, TripUpdate tripUpdate, StopTimeUpdate stopTimeUpdate, List<BlockStopTimeEntry> stopTimes, long serviceDate) {
if (stopTimeUpdate.hasStopSequence()) {
int stopSequence = stopTimeUpdate.getStopSequence();
Map<Integer, BlockStopTimeEntry> sequenceToStopTime = MappingLibrary.mapToValue(stopTimes, "stopTime.gtfsSequence");
if (sequenceToStopTime.containsKey(stopSequence)) {
BlockStopTimeEntry blockStopTime = sequenceToStopTime.get(stopSequence);
if (!stopTimeUpdate.hasStopId()) {
if (result != null) {
result.addMatchedStopId(blockStopTime.getStopTime().getStop().getId().getId());
}
return blockStopTime;
}
String stopTimeUpdateStopId = convertStopId(stopTimeUpdate.getStopId());
if (blockStopTime.getStopTime().getStop().getId().getId().equals(stopTimeUpdateStopId)) {
if (result != null) {
result.addMatchedStopId(blockStopTime.getStopTime().getStop().getId().getId());
}
return blockStopTime;
}
// The stop sequence and stop id didn't match, so we fall through to
// match by stop id if possible
// we do not log this as it still may match later
} else {
_log.debug("StopTimeSequence is out of bounds: stopSequence=" + stopSequence + " tripUpdate=\n" + tripUpdate);
// sadly we can't report an invalid stop sequence -- we need a stopId
}
}
if (stopTimeUpdate.hasStopId()) {
int time = getTimeForStopTimeUpdate(stopTimeUpdate, serviceDate);
String stopId = convertStopId(stopTimeUpdate.getStopId());
// There could be loops, meaning a stop could appear multiple times along
// a trip. To get around this.
Min<BlockStopTimeEntry> bestMatches = new Min<BlockStopTimeEntry>();
for (BlockStopTimeEntry blockStopTime : stopTimes) {
if (blockStopTime.getStopTime().getStop().getId().getId().equals(stopId)) {
StopTimeEntry stopTime = blockStopTime.getStopTime();
int departureDelta = Math.abs(stopTime.getDepartureTime() - time);
int arrivalDelta = Math.abs(stopTime.getArrivalTime() - time);
bestMatches.add(departureDelta, blockStopTime);
bestMatches.add(arrivalDelta, blockStopTime);
}
}
if (!bestMatches.isEmpty()) {
if (result != null) {
result.addMatchedStopId(convertStopId(stopId));
}
return bestMatches.getMinElement();
}
}
if (result != null) {
// if we are here, the stop did not fall on that block
result.addUnmatchedStopId(convertStopId(stopTimeUpdate.getStopId()));
}
return null;
}
use of org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry in project onebusaway-application-modules by camsys.
the class BlockIndexServiceImpl method loadStopTripIndices.
private void loadStopTripIndices() {
// Clear any existing indices
for (StopEntry stop : _graphDao.getAllStops()) {
StopEntryImpl stopImpl = (StopEntryImpl) stop;
stopImpl.getStopTripIndices().clear();
stopImpl.getFrequencyStopTripIndices().clear();
}
for (BlockSequenceIndex index : _blockSequenceIndices) {
BlockSequence sequence = index.getSequences().get(0);
int offset = 0;
for (BlockStopTimeEntry bst : sequence.getStopTimes()) {
StopTimeEntry stopTime = bst.getStopTime();
StopEntryImpl stop = (StopEntryImpl) stopTime.getStop();
BlockStopSequenceIndex blockStopTripIndex = new BlockStopSequenceIndex(index, offset);
stop.addBlockStopTripIndex(blockStopTripIndex);
offset++;
}
}
for (FrequencyBlockTripIndex index : _frequencyBlockTripIndices) {
BlockTripEntry trip = index.getTrips().get(0);
int offset = 0;
for (BlockStopTimeEntry bst : trip.getStopTimes()) {
StopTimeEntry stopTime = bst.getStopTime();
StopEntryImpl stop = (StopEntryImpl) stopTime.getStop();
FrequencyStopTripIndex stopTripIndex = new FrequencyStopTripIndex(index, offset);
stop.addFrequencyStopTripIndex(stopTripIndex);
offset++;
}
}
}
use of org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry in project onebusaway-application-modules by camsys.
the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationBetweenStopTimes.
private ScheduledBlockLocation getScheduledBlockLocationBetweenStopTimes(List<BlockStopTimeEntry> stopTimes, int scheduleTime, int stopTimeIndex) {
BlockStopTimeEntry blockBefore = stopTimes.get(stopTimeIndex - 1);
BlockStopTimeEntry blockAfter = stopTimes.get(stopTimeIndex);
StopTimeEntry before = blockBefore.getStopTime();
StopTimeEntry after = blockAfter.getStopTime();
ScheduledBlockLocation result = new ScheduledBlockLocation();
result.setScheduledTime(scheduleTime);
result.setInService(true);
result.setStopTimeIndex(stopTimeIndex);
int fromTime = before.getDepartureTime();
int toTime = after.getArrivalTime();
int fromTimeOffset = fromTime - scheduleTime;
int toTimeOffset = toTime - scheduleTime;
if (Math.abs(fromTimeOffset) < Math.abs(toTimeOffset)) {
result.setClosestStop(blockBefore);
result.setClosestStopTimeOffset(fromTimeOffset);
} else {
result.setClosestStop(blockAfter);
result.setClosestStopTimeOffset(toTimeOffset);
}
result.setPreviousStop(blockBefore);
result.setNextStop(blockAfter);
result.setNextStopTimeOffset(toTimeOffset);
double ratio = (scheduleTime - fromTime) / ((double) (toTime - fromTime));
double fromDistance = blockBefore.getDistanceAlongBlock();
double toDistance = blockAfter.getDistanceAlongBlock();
double distanceAlongBlock = ratio * (toDistance - fromDistance) + fromDistance;
result.setDistanceAlongBlock(distanceAlongBlock);
int shapePointIndexFrom = -1;
int shapePointIndexTo = -1;
/**
* Are we between trips? Where is the transition point?
*/
if (!before.getTrip().equals(after.getTrip())) {
if (distanceAlongBlock >= blockAfter.getTrip().getDistanceAlongBlock()) {
result.setActiveTrip(blockAfter.getTrip());
shapePointIndexFrom = 0;
shapePointIndexTo = nextShapePointIndex(after);
} else {
result.setActiveTrip(blockBefore.getTrip());
shapePointIndexFrom = before.getShapePointIndex();
shapePointIndexTo = Integer.MAX_VALUE;
}
} else {
result.setActiveTrip(blockBefore.getTrip());
shapePointIndexFrom = before.getShapePointIndex();
shapePointIndexTo = nextShapePointIndex(after);
}
BlockTripEntry activeTrip = result.getActiveTrip();
PointAndOrientation po = getLocationAlongShape(activeTrip, distanceAlongBlock, shapePointIndexFrom, shapePointIndexTo);
if (po != null) {
result.setLocation(po.getPoint());
result.setOrientation(po.getOrientation());
return result;
}
StopEntry beforeStop = before.getStop();
StopEntry afterStop = after.getStop();
double latFrom = beforeStop.getStopLat();
double lonFrom = beforeStop.getStopLon();
double latTo = afterStop.getStopLat();
double lonTo = afterStop.getStopLon();
double lat = (latTo - latFrom) * ratio + latFrom;
double lon = (lonTo - lonFrom) * ratio + lonFrom;
CoordinatePoint location = new CoordinatePoint(lat, lon);
result.setLocation(location);
double orientation = SphericalGeometryLibrary.getOrientation(latFrom, lonFrom, latTo, lonTo);
result.setOrientation(orientation);
return result;
}
Aggregations