Search in sources :

Example 1 with VehiclePosition

use of com.google.transit.realtime.GtfsRealtime.VehiclePosition 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);
    }
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) Position(com.google.transit.realtime.GtfsRealtime.Position)

Example 2 with VehiclePosition

use of com.google.transit.realtime.GtfsRealtime.VehiclePosition in project onebusaway-application-modules by camsys.

the class VehiclePositionsForAgencyActionTest method test.

@Test
public void test() {
    long now = System.currentTimeMillis();
    List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
    RouteBean.Builder routeBuilder = RouteBean.builder();
    routeBuilder.setId("1_r1");
    RouteBean route = routeBuilder.create();
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(1234 * 1000);
        vehicle.setVehicleId("1_v1");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        TripBean trip = new TripBean();
        trip.setId("1_t0");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        vehicle.setLocation(new CoordinatePoint(47.0, -122.0));
    }
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(5678 * 1000);
        vehicle.setVehicleId("1_v2");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        TripBean trip = new TripBean();
        trip.setId("1_t1");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        vehicle.setLocation(new CoordinatePoint(47.1, -122.1));
    }
    ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
    bean.setList(vehicles);
    Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
    _action.setId("1");
    _action.setTime(new Date(now));
    _action.show();
    ResponseBean model = _action.getModel();
    FeedMessage feed = (FeedMessage) model.getData();
    assertEquals(now / 1000, feed.getHeader().getTimestamp());
    assertEquals(2, feed.getEntityCount());
    {
        FeedEntity entity = feed.getEntity(0);
        assertEquals("1", entity.getId());
        VehiclePosition vehiclePosition = entity.getVehicle();
        assertEquals("t0", vehiclePosition.getTrip().getTripId());
        assertEquals("r1", vehiclePosition.getTrip().getRouteId());
        assertEquals("v1", vehiclePosition.getVehicle().getId());
        assertEquals(1234, vehiclePosition.getTimestamp());
        assertEquals(47.0, vehiclePosition.getPosition().getLatitude(), 0.01);
        assertEquals(-122.0, vehiclePosition.getPosition().getLongitude(), 0.01);
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        VehiclePosition vehiclePosition = entity.getVehicle();
        assertEquals("t1", vehiclePosition.getTrip().getTripId());
        assertEquals("r1", vehiclePosition.getTrip().getRouteId());
        assertEquals("v2", vehiclePosition.getVehicle().getId());
        assertEquals(5678, vehiclePosition.getTimestamp());
        assertEquals(47.1, vehiclePosition.getPosition().getLatitude(), 0.01);
        assertEquals(-122.1, vehiclePosition.getPosition().getLongitude(), 0.01);
    }
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) Date(java.util.Date) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) ResponseBean(org.onebusaway.api.model.ResponseBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Example 3 with VehiclePosition

use of com.google.transit.realtime.GtfsRealtime.VehiclePosition in project onebusaway-application-modules by camsys.

the class VehiclePositionsForAgencyAction method fillFeedMessage.

@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
    ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(agencyId, timestamp);
    for (VehicleStatusBean vehicle : vehicles.getList()) {
        FeedEntity.Builder entity = feed.addEntityBuilder();
        entity.setId(Integer.toString(feed.getEntityCount()));
        VehiclePosition.Builder vehiclePosition = entity.getVehicleBuilder();
        TripStatusBean tripStatus = vehicle.getTripStatus();
        if (tripStatus != null) {
            TripBean activeTrip = tripStatus.getActiveTrip();
            RouteBean route = activeTrip.getRoute();
            TripDescriptor.Builder tripDesc = vehiclePosition.getTripBuilder();
            tripDesc.setTripId(normalizeId(activeTrip.getId()));
            tripDesc.setRouteId(normalizeId(route.getId()));
        }
        VehicleDescriptor.Builder vehicleDesc = vehiclePosition.getVehicleBuilder();
        vehicleDesc.setId(normalizeId(vehicle.getVehicleId()));
        CoordinatePoint location = vehicle.getLocation();
        if (location != null) {
            Position.Builder position = vehiclePosition.getPositionBuilder();
            position.setLatitude((float) location.getLat());
            position.setLongitude((float) location.getLon());
        }
        vehiclePosition.setTimestamp(vehicle.getLastUpdateTime() / 1000);
    }
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) Position(com.google.transit.realtime.GtfsRealtime.Position) TripBean(org.onebusaway.transit_data.model.trips.TripBean) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)

Example 4 with VehiclePosition

use of com.google.transit.realtime.GtfsRealtime.VehiclePosition in project onebusaway-application-modules by camsys.

the class GtfsRealtimeRetrieverImpl method writeVehiclePosition.

private VehiclePosition writeVehiclePosition(VehiclePositionModel model) {
    VehiclePosition.Builder vp = VehiclePosition.newBuilder();
    if (model.getVehicleId() != null) {
        VehicleDescriptor.Builder v = VehicleDescriptor.newBuilder();
        v.setId(model.getVehicleId());
        if (!StringUtils.isEmpty(model.getVehicleLabel())) {
            v.setLabel(model.getVehicleLabel());
        }
        if (!StringUtils.isEmpty(model.getVehicleLicensePlate())) {
            v.setLicensePlate(model.getVehicleLicensePlate());
        }
        vp.setVehicle(v);
    }
    Position.Builder p = Position.newBuilder();
    if (model.getBearing() != null) {
        p.setBearing(model.getBearing());
    }
    if (model.getLat() != null) {
        p.setLatitude(model.getLat());
    }
    if (model.getLon() != null) {
        p.setLongitude(model.getLon());
    }
    if (model.getSpeed() != null) {
        p.setSpeed(model.getSpeed());
    }
    vp.setPosition(p);
    if (!StringUtils.isEmpty(model.getTripId())) {
        TripDescriptor.Builder td = TripDescriptor.newBuilder();
        td.setTripId(parseId(model.getTripId()));
        if (!StringUtils.isEmpty(model.getRouteId())) {
            td.setRouteId(parseId(model.getRouteId()));
        }
        if (model.getTripStart() != null) {
            Date date = model.getTripStart();
            String startDate = new SimpleDateFormat(GTFS_RT_DATE_FORMAT).format(date);
            String startTime = new SimpleDateFormat(GTFS_RT_TIME_FORMAT).format(date);
            td.setStartDate(startDate);
            td.setStartTime(startTime);
        }
        vp.setTrip(td);
    }
    if (!StringUtils.isEmpty(model.getStopId())) {
        vp.setStopId(parseId(model.getStopId()));
    }
    return vp.build();
}
Also used : VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Position(com.google.transit.realtime.GtfsRealtime.Position) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with VehiclePosition

use of com.google.transit.realtime.GtfsRealtime.VehiclePosition 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;
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Collection(java.util.Collection) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

VehiclePosition (com.google.transit.realtime.GtfsRealtime.VehiclePosition)5 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)3 Position (com.google.transit.realtime.GtfsRealtime.Position)3 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)3 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)3 VehicleDescriptor (com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)2 TripBean (org.onebusaway.transit_data.model.trips.TripBean)2 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)2 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)1 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)1 OneBusAwayTripUpdate (com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1