Search in sources :

Example 76 with BlockConfigurationEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry 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;
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheElement(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElement) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) TreeMap(java.util.TreeMap) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) ScheduleDeviationSamples(org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 77 with BlockConfigurationEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getScheduledBlockLocationForVehicleLocationRecord.

/**
 **
 * {@link ScheduledBlockLocation} Methods
 ***
 */
private ScheduledBlockLocation getScheduledBlockLocationForVehicleLocationRecord(VehicleLocationRecord record, BlockInstance blockInstance) {
    BlockConfigurationEntry blockConfig = blockInstance.getBlock();
    long serviceDate = blockInstance.getServiceDate();
    long targetTime = record.getTimeOfRecord();
    int scheduledTime = (int) ((targetTime - serviceDate) / 1000);
    if (record.isScheduleDeviationSet()) {
        /**
         * Effective scheduled time is the point that a transit vehicle is at on
         * its schedule, with schedule deviation taken into account. So if it's
         * 100 minutes into the current service date and the bus is running 10
         * minutes late, it's actually at the 90 minute point in its scheduled
         * operation.
         */
        int effectiveScheduledTime = (int) (scheduledTime - record.getScheduleDeviation());
        return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
    }
    if (record.isDistanceAlongBlockSet()) {
        return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(blockConfig, record.getDistanceAlongBlock());
    }
    return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, scheduledTime);
}
Also used : BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 78 with BlockConfigurationEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getVehicleLocationRecordAsBlockLocationRecord.

private List<BlockLocationRecord> getVehicleLocationRecordAsBlockLocationRecord(BlockInstance blockInstance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation) {
    BlockLocationRecord.Builder builder = BlockLocationRecord.builder();
    if (scheduledBlockLocation != null) {
        BlockTripEntry activeTrip = scheduledBlockLocation.getActiveTrip();
        builder.setTripId(activeTrip.getTrip().getId());
        builder.setBlockId(activeTrip.getBlockConfiguration().getBlock().getId());
        // store the vehicleType for later retrieval
        builder.setVehicleType(EVehicleType.toEnum(activeTrip.getTrip().getRoute().getType()));
        double distanceAlongBlock = scheduledBlockLocation.getDistanceAlongBlock();
        builder.setDistanceAlongBlock(distanceAlongBlock);
        double distanceAlongTrip = distanceAlongBlock - activeTrip.getDistanceAlongBlock();
        builder.setDistanceAlongTrip(distanceAlongTrip);
    }
    if (record.getBlockId() != null)
        builder.setBlockId(record.getBlockId());
    if (record.getTripId() != null)
        builder.setTripId(record.getTripId());
    builder.setTime(record.getTimeOfRecord());
    builder.setServiceDate(record.getServiceDate());
    if (record.isScheduleDeviationSet())
        builder.setScheduleDeviation(record.getScheduleDeviation());
    if (record.isDistanceAlongBlockSet()) {
        double distanceAlongBlock = record.getDistanceAlongBlock();
        builder.setDistanceAlongBlock(distanceAlongBlock);
        AgencyAndId tripId = record.getTripId();
        if (tripId != null) {
            BlockConfigurationEntry block = blockInstance.getBlock();
            for (BlockTripEntry blockTrip : block.getTrips()) {
                TripEntry trip = blockTrip.getTrip();
                if (trip.getId().equals(tripId)) {
                    double distanceAlongTrip = distanceAlongBlock - blockTrip.getDistanceAlongBlock();
                    builder.setDistanceAlongTrip(distanceAlongTrip);
                }
            }
        }
    }
    if (record.isCurrentLocationSet()) {
        builder.setLocationLat(record.getCurrentLocationLat());
        builder.setLocationLon(record.getCurrentLocationLon());
    }
    if (record.isCurrentOrientationSet())
        builder.setOrientation(record.getCurrentOrientation());
    builder.setPhase(record.getPhase());
    builder.setStatus(record.getStatus());
    builder.setVehicleId(record.getVehicleId());
    List<TimepointPredictionRecord> predictions = record.getTimepointPredictions();
    if (predictions == null || predictions.isEmpty())
        return Arrays.asList(builder.create());
    List<BlockLocationRecord> results = new ArrayList<BlockLocationRecord>();
    for (TimepointPredictionRecord tpr : predictions) {
        builder.setTimepointId(tpr.getTimepointId());
        builder.setTimepointScheduledTime(tpr.getTimepointScheduledTime());
        builder.setTimepointPredictedArrivalTime(tpr.getTimepointPredictedArrivalTime());
        builder.setTimepointPredictedDepartureTime(tpr.getTimepointPredictedDepartureTime());
        results.add(builder.create());
    }
    return results;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 79 with BlockConfigurationEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSource method getEffectiveScheduleTime.

private long getEffectiveScheduleTime(TripEntry trip, double lat, double lon, long timestamp, long serviceDate) {
    ServiceIdActivation serviceIds = new ServiceIdActivation(trip.getServiceId());
    // todo!
    BlockConfigurationEntry blockConfig = blockConfiguration(trip.getBlock(), serviceIds, trip);
    BlockInstance block = new BlockInstance(blockConfig, serviceDate);
    CoordinatePoint location = new CoordinatePoint(lat, lon);
    ScheduledBlockLocation loc = _blockGeospatialService.getBestScheduledBlockLocationForLocation(block, location, timestamp, 0, trip.getTotalTripDistance());
    return loc.getScheduledTime();
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ServiceIdActivation(org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 80 with BlockConfigurationEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method getBlockStartTimeForTripStartTime.

private int getBlockStartTimeForTripStartTime(BlockInstance instance, AgencyAndId tripId, int tripStartTime) {
    BlockConfigurationEntry block = instance.getBlock();
    Map<AgencyAndId, BlockTripEntry> blockTripsById = MappingLibrary.mapToValue(block.getTrips(), "trip.id");
    int rawBlockStartTime = block.getDepartureTimeForIndex(0);
    int rawTripStartTime = blockTripsById.get(tripId).getDepartureTimeForIndex(0);
    int adjustedBlockStartTime = rawBlockStartTime + (tripStartTime - rawTripStartTime);
    return adjustedBlockStartTime;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Aggregations

BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)89 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)36 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)34 Test (org.junit.Test)30 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)29 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)27 ArrayList (java.util.ArrayList)23 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)23 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)18 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)17 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)14 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)13 BlockEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry)13 Date (java.util.Date)12 List (java.util.List)11 StopEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)11 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)10 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)10 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)8 FactoryMap (org.onebusaway.collections.FactoryMap)8