use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project onebusaway-application-modules by camsys.
the class GtfsRealtimeAlertLibrary method getEntitySelectorAsAffects.
private Affects.Builder getEntitySelectorAsAffects(EntitySelector selector, Map agencyIdMap) {
Affects.Builder affects = Affects.newBuilder();
if (selector.hasAgencyId()) {
String agencyId = selector.getAgencyId();
if (agencyIdMap != null && agencyIdMap.get(agencyId) != null) {
agencyId = (String) agencyIdMap.get(agencyId);
}
affects.setAgencyId(agencyId);
}
if (selector.hasRouteId()) {
Id routeId = _entitySource.getRouteId(selector.getRouteId());
affects.setRouteId(routeId);
}
if (selector.hasStopId()) {
Id stopId = _entitySource.getStopId(selector.getStopId());
affects.setStopId(stopId);
}
if (selector.hasTrip()) {
TripDescriptor trip = selector.getTrip();
if (trip.hasTripId())
affects.setTripId(_entitySource.getTripId(trip.getTripId()));
else if (trip.hasRouteId())
affects.setRouteId(_entitySource.getRouteId(trip.getRouteId()));
}
return affects;
}
use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method groupTripUpdatesAndVehiclePositions.
public List<CombinedTripUpdatesAndVehiclePosition> groupTripUpdatesAndVehiclePositions(MonitoredResult result, FeedMessage tripUpdateMessage, FeedMessage vehiclePositionsMessage) {
List<CombinedTripUpdatesAndVehiclePosition> updates = new ArrayList<CombinedTripUpdatesAndVehiclePosition>();
Map<String, TripUpdate> bestTripByVehicleId = new HashMap<String, TripUpdate>();
ListMultimap<String, TripUpdate> tripUpdatesByVehicleId = ArrayListMultimap.create();
Map<String, VehiclePosition> vehiclePositionsByVehicleId = new HashMap<String, VehiclePosition>();
ListMultimap<BlockDescriptor, TripUpdate> anonymousTripUpdatesByBlock = ArrayListMultimap.<BlockDescriptor, TripUpdate>create();
Map<BlockDescriptor, VehiclePosition> anonymousVehiclePositionsByBlock = new HashMap<BlockDescriptor, VehiclePosition>();
Set<BlockDescriptor> badAnonymousVehiclePositions = new HashSet<BlockDescriptor>();
for (FeedEntity fe : tripUpdateMessage.getEntityList()) {
if (!fe.hasTripUpdate()) {
continue;
}
TripUpdate tu = fe.getTripUpdate();
if (tu.hasVehicle() && tu.getVehicle().hasId() && StringUtils.isNotBlank(tu.getVehicle().getId())) {
// Trip update has a vehicle ID - index by vehicle ID
String vehicleId = tu.getVehicle().getId();
tripUpdatesByVehicleId.put(vehicleId, tu);
if (!bestTripByVehicleId.containsKey(vehicleId)) {
bestTripByVehicleId.put(vehicleId, tu);
} else {
// upcoming merge will fix this
_log.debug("Multiple TripUpdates for vehicle {}; taking best.", vehicleId);
if (tripMoreAppropriate(tu, bestTripByVehicleId.get(vehicleId), vehicleId)) {
bestTripByVehicleId.put(vehicleId, tu);
}
}
} else {
/*
* Trip update does not have a vehicle ID - index by TripDescriptor
* (includes start date and time).
*/
TripDescriptor td = tu.getTrip();
long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
if (bd == null) {
continue;
}
if (!anonymousTripUpdatesByBlock.containsKey(bd)) {
anonymousTripUpdatesByBlock.put(bd, tu);
} else {
_log.warn("Multiple anonymous TripUpdates for trip {}; will not map to VehiclePosition.", td.getTripId());
anonymousTripUpdatesByBlock.put(bd, tu);
}
}
}
for (FeedEntity fe : vehiclePositionsMessage.getEntityList()) {
if (!fe.hasVehicle()) {
continue;
}
VehiclePosition vp = fe.getVehicle();
if (vp.hasVehicle() && vp.getVehicle().hasId()) {
// Vehicle position has a vehicle ID - index by vehicle ID
String vehicleId = vp.getVehicle().getId();
if (!vehiclePositionsByVehicleId.containsKey(vehicleId)) {
vehiclePositionsByVehicleId.put(vehicleId, vp);
} else {
_log.warn("Multiple updates for vehicle {}; taking newest.", vehicleId);
VehiclePosition otherUpdate = vehiclePositionsByVehicleId.get(vehicleId);
long otherTimestamp = otherUpdate.getTimestamp();
if (vp.getTimestamp() > otherTimestamp) {
vehiclePositionsByVehicleId.put(vehicleId, vp);
}
}
} else if (vp.hasTrip()) {
/*
* Vehicle position does not have vehicle ID but has TripDescriptor, so
* use that, but only if there is only one.
*/
TripDescriptor td = vp.getTrip();
long time = vp.hasTimestamp() ? vp.getTimestamp() * 1000 : currentTime();
BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
if (bd == null) {
continue;
}
if (!anonymousVehiclePositionsByBlock.containsKey(bd)) {
anonymousVehiclePositionsByBlock.put(bd, vp);
} else {
/*
* When we have multiple VehiclePositions for a block but no way to
* uniquely distinguish them there is nothing useful or reasonable we
* can do with the data.
*/
_log.warn("Multiple anonymous VehiclePositions for trip {}; giving up.", td.getTripId());
badAnonymousVehiclePositions.add(bd);
}
} else {
/*
* Pathological VehiclePosition contains no identifying information;
* skip.
*/
continue;
}
}
// a block
for (BlockDescriptor bd : badAnonymousVehiclePositions) {
anonymousVehiclePositionsByBlock.remove(bd);
}
// Map updates by vehicle ID
for (Map.Entry<String, Collection<TripUpdate>> e : tripUpdatesByVehicleId.asMap().entrySet()) {
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
String vehicleId = e.getKey();
Collection<TripUpdate> tripUpdates = e.getValue();
TripUpdate tu = bestTripByVehicleId.get(vehicleId);
long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
update.block = getTripDescriptorAsBlockDescriptor(result, tu.getTrip(), time);
update.tripUpdates = new ArrayList<TripUpdate>(tripUpdates);
update.bestTrip = tu.getTrip().getTripId();
if (vehiclePositionsByVehicleId.containsKey(vehicleId)) {
update.vehiclePosition = vehiclePositionsByVehicleId.get(vehicleId);
}
updates.add(update);
}
// Map anonymous updates by block descriptor
for (Entry<BlockDescriptor, Collection<TripUpdate>> e : anonymousTripUpdatesByBlock.asMap().entrySet()) {
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
BlockDescriptor bd = e.getKey();
update.block = bd;
update.tripUpdates = new ArrayList<TripUpdate>(e.getValue());
if (update.tripUpdates.size() == 1 && anonymousVehiclePositionsByBlock.containsKey(bd)) {
update.vehiclePosition = anonymousVehiclePositionsByBlock.get(bd);
}
updates.add(update);
}
// Set vehicle ID in block if possible
for (CombinedTripUpdatesAndVehiclePosition update : updates) {
String vehicleId = null;
for (TripUpdate tu : update.tripUpdates) {
if (tu.hasVehicle() && tu.getVehicle().hasId()) {
vehicleId = tu.getVehicle().getId();
break;
}
}
if (vehicleId == null && update.vehiclePosition != null && update.vehiclePosition.hasVehicle() && update.vehiclePosition.getVehicle().hasId()) {
vehicleId = update.vehiclePosition.getVehicle().getId();
}
if (vehicleId != null && update.block != null) {
update.block.setVehicleId(vehicleId);
}
}
return updates;
}
Aggregations