use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry in project onebusaway-application-modules by camsys.
the class TripStatusBeanServiceImpl method getBlockLocationAsStatusBean.
@Override
public TripStatusBean getBlockLocationAsStatusBean(BlockLocation blockLocation, long time) {
TripStatusBean bean = new TripStatusBean();
bean.setStatus("default");
BlockInstance blockInstance = blockLocation.getBlockInstance();
long serviceDate = blockInstance.getServiceDate();
bean.setServiceDate(serviceDate);
bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
bean.setLastLocationUpdateTime(blockLocation.getLastLocationUpdateTime());
bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
bean.setLocation(blockLocation.getLocation());
bean.setOrientation(blockLocation.getOrientation());
bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
if (blockLocation.isLastKnownOrientationSet())
bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
bean.setScheduleDeviation(blockLocation.getScheduleDeviation());
BlockTripInstance activeTripInstance = blockLocation.getActiveTripInstance();
if (activeTripInstance != null) {
BlockTripEntry activeBlockTrip = activeTripInstance.getBlockTrip();
bean.setScheduledDistanceAlongTrip(blockLocation.getScheduledDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
bean.setDistanceAlongTrip(blockLocation.getDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
TripEntry activeTrip = activeBlockTrip.getTrip();
bean.setTotalDistanceAlongTrip(activeTrip.getTotalTripDistance());
TripBean activeTripBean = _tripBeanService.getTripForId(activeTrip.getId());
bean.setActiveTrip(activeTripBean);
bean.setBlockTripSequence(activeBlockTrip.getSequence());
if (blockLocation.isLastKnownDistanceAlongBlockSet()) {
bean.setLastKnownDistanceAlongTrip(blockLocation.getLastKnownDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
}
FrequencyEntry frequencyLabel = activeTripInstance.getFrequencyLabel();
if (frequencyLabel != null) {
FrequencyBean fb = FrequencyBeanLibrary.getBeanForFrequency(serviceDate, frequencyLabel);
bean.setFrequency(fb);
}
} else {
_log.warn("no active block trip for block location: blockInstance=" + blockLocation.getBlockInstance() + " time=" + time);
}
BlockStopTimeEntry closestStop = blockLocation.getClosestStop();
if (closestStop != null) {
StopTimeEntry stopTime = closestStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setClosestStop(stopBean);
bean.setClosestStopTimeOffset(blockLocation.getClosestStopTimeOffset());
}
BlockStopTimeEntry nextStop = blockLocation.getNextStop();
if (nextStop != null) {
StopTimeEntry stopTime = nextStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setNextStop(stopBean);
bean.setNextStopTimeOffset(blockLocation.getNextStopTimeOffset());
bean.setNextStopDistanceFromVehicle(blockLocation.getNextStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
}
BlockStopTimeEntry previousStop = blockLocation.getPreviousStop();
if (previousStop != null) {
StopTimeEntry stopTime = previousStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setPreviousStop(stopBean);
bean.setPreviousStopTimeOffset(blockLocation.getPreviousStopTimeOffset());
bean.setPreviousStopDistanceFromVehicle(blockLocation.getPreviousStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
}
EVehiclePhase phase = blockLocation.getPhase();
if (phase != null)
bean.setPhase(phase.toLabel());
String status = blockLocation.getStatus();
if (status != null)
bean.setStatus(status);
if (blockLocation.getVehicleType() != null)
bean.setVehicleType(blockLocation.getVehicleType().toLabel());
bean.setPredicted(blockLocation.isPredicted());
AgencyAndId vid = blockLocation.getVehicleId();
if (vid != null)
bean.setVehicleId(ApplicationBeanLibrary.getId(vid));
if (activeTripInstance != null) {
List<ServiceAlertBean> situations = _serviceAlertBeanService.getServiceAlertsForVehicleJourney(time, activeTripInstance, blockLocation.getVehicleId());
if (!situations.isEmpty())
bean.setSituations(situations);
}
if (blockLocation.getTimepointPredictions() != null && blockLocation.getTimepointPredictions().size() > 0) {
List<TimepointPredictionBean> timepointPredictions = new ArrayList<TimepointPredictionBean>();
for (TimepointPredictionRecord tpr : blockLocation.getTimepointPredictions()) {
TimepointPredictionBean tpb = new TimepointPredictionBean();
tpb.setTimepointId(tpr.getTimepointId().toString());
tpb.setTripId(tpr.getTripId().toString());
tpb.setStopSequence(tpr.getStopSequence());
tpb.setTimepointPredictedArrivalTime(tpr.getTimepointPredictedArrivalTime());
tpb.setTimepointPredictedDepartureTime(tpr.getTimepointPredictedDepartureTime());
timepointPredictions.add(tpb);
}
bean.setTimepointPredictions(timepointPredictions);
}
return bean;
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry 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.BlockStopTimeEntry 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.BlockStopTimeEntry in project onebusaway-application-modules by camsys.
the class BlockStatusBeanServiceImpl method bean.
private BlockStatusBean bean(BlockLocation blockLocation) {
if (blockLocation == null)
return null;
BlockInstance instance = blockLocation.getBlockInstance();
BlockConfigurationEntry block = instance.getBlock();
long serviceDate = instance.getServiceDate();
BlockStatusBean bean = new BlockStatusBean();
bean.setBlock(_blockBeanService.getBlockForId(block.getBlock().getId()));
bean.setStatus("default");
bean.setServiceDate(serviceDate);
bean.setTotalDistanceAlongBlock(block.getTotalBlockDistance());
bean.setInService(blockLocation.isInService());
CoordinatePoint location = blockLocation.getLocation();
bean.setLocation(location);
bean.setScheduledDistanceAlongBlock(blockLocation.getScheduledDistanceAlongBlock());
bean.setDistanceAlongBlock(blockLocation.getDistanceAlongBlock());
BlockTripEntry activeTrip = blockLocation.getActiveTrip();
if (activeTrip != null) {
BlockTripBean activeTripBean = _blockBeanService.getBlockTripAsBean(activeTrip);
bean.setActiveTrip(activeTripBean);
}
BlockStopTimeEntry stop = blockLocation.getClosestStop();
if (stop != null) {
StopBean stopBean = _stopBeanService.getStopForId(stop.getStopTime().getStop().getId());
bean.setClosestStop(stopBean);
bean.setClosestStopTimeOffset(blockLocation.getClosestStopTimeOffset());
}
bean.setPredicted(blockLocation.isPredicted());
bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
bean.setScheduleDeviation(blockLocation.getScheduleDeviation());
AgencyAndId vid = blockLocation.getVehicleId();
if (vid != null)
bean.setVehicleId(ApplicationBeanLibrary.getId(vid));
return bean;
}
use of org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry 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++;
}
}
}
Aggregations