Search in sources :

Example 1 with BlockConfigurationEntry

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

the class BlockCalendarServiceImpl method findBlockTripsInRange.

private void findBlockTripsInRange(ServiceIntervalBlock intervals, Date serviceDate, Date timeFrom, Date timeTo, List<BlockTripEntry> trips, Collection<BlockInstance> instances) {
    int scheduledTimeFrom = (int) ((timeFrom.getTime() - serviceDate.getTime()) / 1000);
    int scheduledTimeTo = (int) ((timeTo.getTime() - serviceDate.getTime()) / 1000);
    int indexFrom = index(Arrays.binarySearch(intervals.getMaxDepartures(), scheduledTimeFrom));
    int indexTo = index(Arrays.binarySearch(intervals.getMinArrivals(), scheduledTimeTo));
    InstanceState state = new InstanceState(serviceDate.getTime());
    for (int in = indexFrom; in < indexTo; in++) {
        BlockTripEntry trip = trips.get(in);
        BlockConfigurationEntry block = trip.getBlockConfiguration();
        BlockInstance instance = new BlockInstance(block, state);
        instances.add(instance);
    }
}
Also used : InstanceState(org.onebusaway.transit_data_federation.services.blocks.InstanceState) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 2 with BlockConfigurationEntry

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

the class BlockCalendarServiceImpl method getTimeToBlockInstance.

/**
 **
 *
 ***
 */
private long getTimeToBlockInstance(BlockInstance instance, long time) {
    long serviceDate = instance.getServiceDate();
    BlockConfigurationEntry blockConfig = instance.getBlock();
    int n = blockConfig.getStopTimes().size();
    long from = serviceDate + blockConfig.getArrivalTimeForIndex(0) * 1000;
    long to = serviceDate + blockConfig.getDepartureTimeForIndex(n - 1) * 1000;
    return Math.abs((from + to) / 2 - time);
}
Also used : BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 3 with BlockConfigurationEntry

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

the class BlockGeospatialServiceImpl method getBestScheduledBlockLocationForLocation.

public ScheduledBlockLocation getBestScheduledBlockLocationForLocation(BlockInstance blockInstance, CoordinatePoint location, long timestamp, double blockDistanceFrom, double blockDistanceTo) {
    BlockConfigurationEntry block = blockInstance.getBlock();
    ProjectedPoint targetPoint = ProjectedPointFactory.forward(location);
    List<AgencyAndId> shapePointIds = MappingLibrary.map(block.getTrips(), "trip.shapeId");
    T2<List<XYPoint>, double[]> tuple = _projectedShapePointService.getProjectedShapePoints(shapePointIds, targetPoint.getSrid());
    if (tuple == null) {
        throw new IllegalStateException("block had no shape points: " + block.getBlock().getId());
    }
    List<XYPoint> projectedShapePoints = tuple.getFirst();
    double[] distances = tuple.getSecond();
    int fromIndex = 0;
    int toIndex = distances.length;
    if (blockDistanceFrom > 0) {
        fromIndex = Arrays.binarySearch(distances, blockDistanceFrom);
        if (fromIndex < 0) {
            fromIndex = -(fromIndex + 1);
            // Include the previous point if we didn't get an exact match
            if (fromIndex > 0)
                fromIndex--;
        }
    }
    if (blockDistanceTo < distances[distances.length - 1]) {
        toIndex = Arrays.binarySearch(distances, blockDistanceTo);
        if (toIndex < 0) {
            toIndex = -(toIndex + 1);
            // Include the previous point if we didn't get an exact match
            if (toIndex < distances.length)
                toIndex++;
        }
    }
    XYPoint xyPoint = new XYPoint(targetPoint.getX(), targetPoint.getY());
    List<PointAndIndex> assignments = _shapePointsLibrary.computePotentialAssignments(projectedShapePoints, distances, xyPoint, fromIndex, toIndex);
    Min<ScheduledBlockLocation> best = new Min<ScheduledBlockLocation>();
    for (PointAndIndex index : assignments) {
        double distanceAlongBlock = index.distanceAlongShape;
        if (distanceAlongBlock > block.getTotalBlockDistance())
            distanceAlongBlock = block.getTotalBlockDistance();
        ScheduledBlockLocation blockLocation = _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(block, distanceAlongBlock);
        if (blockLocation != null) {
            int scheduledTime = blockLocation.getScheduledTime();
            long scheduleTimestamp = blockInstance.getServiceDate() + scheduledTime * 1000;
            double delta = Math.abs(scheduleTimestamp - timestamp);
            best.add(delta, blockLocation);
        }
    }
    return best.getMinElement();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) Min(org.onebusaway.collections.Min) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint) List(java.util.List) ArrayList(java.util.ArrayList) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)

Example 4 with BlockConfigurationEntry

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

the class GtfsRealtimeTripLibrary method applyTripUpdatesToRecord.

private void applyTripUpdatesToRecord(MonitoredResult result, BlockDescriptor blockDescriptor, List<TripUpdate> tripUpdates, VehicleLocationRecord record, String vehicleId, String bestTripId) {
    BlockInstance instance = blockDescriptor.getBlockInstance();
    BlockConfigurationEntry blockConfiguration = instance.getBlock();
    List<BlockTripEntry> blockTrips = blockConfiguration.getTrips();
    Map<String, List<TripUpdate>> tripUpdatesByTripId = MappingLibrary.mapToValueList(tripUpdates, "trip.tripId");
    long t = currentTime();
    int currentTime = (int) ((t - instance.getServiceDate()) / 1000);
    BestScheduleDeviation best = new BestScheduleDeviation();
    long lastStopScheduleTime = Long.MIN_VALUE;
    boolean singleTimepointRecord = false;
    List<TimepointPredictionRecord> timepointPredictions = new ArrayList<TimepointPredictionRecord>();
    for (BlockTripEntry blockTrip : blockTrips) {
        TripEntry trip = blockTrip.getTrip();
        AgencyAndId tripId = trip.getId();
        List<TripUpdate> updatesForTrip = tripUpdatesByTripId.get(tripId.getId());
        boolean tripUpdateHasDelay = false;
        // onBestTrip is only relevant if bestTripId is set, which indicates that the TripUpdates
        // came from the vehicleId map (as opposed to block index).
        boolean onBestTrip = bestTripId == null || tripId.getId().equals(bestTripId);
        if (updatesForTrip != null) {
            for (TripUpdate tripUpdate : updatesForTrip) {
                /**
                 * TODO: delete this code once all upstream systems have been
                 * migrated the new "delay" and "timestamp" fields.
                 */
                if (tripUpdate.hasExtension(GtfsRealtimeOneBusAway.obaTripUpdate) && onBestTrip) {
                    OneBusAwayTripUpdate obaTripUpdate = tripUpdate.getExtension(GtfsRealtimeOneBusAway.obaTripUpdate);
                    if (obaTripUpdate.hasDelay()) {
                        /**
                         * TODO: Improved logic around picking the "best" schedule deviation
                         */
                        int delay = obaTripUpdate.getDelay();
                        best.delta = 0;
                        best.isInPast = false;
                        best.scheduleDeviation = delay;
                        best.tripId = tripId;
                        tripUpdateHasDelay = true;
                    }
                    if (obaTripUpdate.hasTimestamp() && onBestTrip) {
                        best.timestamp = obaTripUpdate.getTimestamp() * 1000;
                    }
                }
                if (tripUpdate.hasDelay() && onBestTrip) {
                    /**
                     * TODO: Improved logic around picking the "best" schedule deviation
                     */
                    best.delta = 0;
                    best.isInPast = false;
                    best.scheduleDeviation = tripUpdate.getDelay();
                    best.tripId = tripId;
                    tripUpdateHasDelay = true;
                }
                if (tripUpdate.hasTimestamp() && onBestTrip) {
                    best.timestamp = tripUpdate.getTimestamp() * 1000;
                }
                for (StopTimeUpdate stopTimeUpdate : tripUpdate.getStopTimeUpdateList()) {
                    BlockStopTimeEntry blockStopTime = getBlockStopTimeForStopTimeUpdate(result, tripUpdate, stopTimeUpdate, blockTrip.getStopTimes(), instance.getServiceDate());
                    // loop through and store last stop time on trip
                    List<BlockStopTimeEntry> stopTimes = blockTrip.getStopTimes();
                    for (BlockStopTimeEntry bste : stopTimes) {
                        long scheduleTime = instance.getServiceDate() + bste.getStopTime().getArrivalTime() * 1000;
                        if (scheduleTime > lastStopScheduleTime) {
                            lastStopScheduleTime = scheduleTime;
                        }
                    }
                    if (blockStopTime == null)
                        continue;
                    StopTimeEntry stopTime = blockStopTime.getStopTime();
                    TimepointPredictionRecord tpr = new TimepointPredictionRecord();
                    tpr.setTimepointId(stopTime.getStop().getId());
                    tpr.setTripId(stopTime.getTrip().getId());
                    tpr.setTimepointScheduledTime(instance.getServiceDate() + stopTime.getArrivalTime() * 1000);
                    if (stopTimeUpdate.hasStopSequence()) {
                        tpr.setStopSequence(stopTimeUpdate.getStopSequence());
                    }
                    int currentArrivalTime = computeArrivalTime(stopTime, stopTimeUpdate, instance.getServiceDate());
                    int currentDepartureTime = computeDepartureTime(stopTime, stopTimeUpdate, instance.getServiceDate());
                    if (currentArrivalTime >= 0) {
                        if (onBestTrip) {
                            updateBestScheduleDeviation(currentTime, stopTime.getArrivalTime(), currentArrivalTime, best, tripId, vehicleId);
                        }
                        long timepointPredictedTime = instance.getServiceDate() + (currentArrivalTime * 1000L);
                        tpr.setTimepointPredictedArrivalTime(timepointPredictedTime);
                    }
                    if (currentDepartureTime >= 0) {
                        if (onBestTrip) {
                            updateBestScheduleDeviation(currentTime, stopTime.getDepartureTime(), currentDepartureTime, best, tripId, vehicleId);
                        }
                        long timepointPredictedTime = instance.getServiceDate() + (currentDepartureTime * 1000L);
                        tpr.setTimepointPredictedDepartureTime(timepointPredictedTime);
                    }
                    if (tpr.getTimepointPredictedArrivalTime() != -1 || tpr.getTimepointPredictedDepartureTime() != -1) {
                        timepointPredictions.add(tpr);
                    }
                }
            }
        }
        if (timepointPredictions.size() == 1 && tripUpdates.get(0).getStopTimeUpdateList().size() == 1) {
            singleTimepointRecord = true;
        }
        // tripUpdateHasDelay = true => best.scheduleDeviation is TripUpdate delay
        if ((timepointPredictions.size() > 0 && tripUpdateHasDelay) || singleTimepointRecord) {
            Set<AgencyAndId> records = new HashSet<AgencyAndId>();
            for (TimepointPredictionRecord tpr : timepointPredictions) {
                records.add(tpr.getTimepointId());
            }
            long tprStartTime = getEarliestTimeInRecords(timepointPredictions);
            for (StopTimeEntry stopTime : trip.getStopTimes()) {
                if (records.contains(stopTime.getStop().getId())) {
                    continue;
                }
                long predictionOffset = instance.getServiceDate() + (best.scheduleDeviation * 1000L);
                long predictedDepartureTime = (stopTime.getDepartureTime() * 1000L) + predictionOffset;
                long predictedArrivalTime = (stopTime.getArrivalTime() * 1000L) + predictionOffset;
                long scheduledArrivalTime = instance.getServiceDate() + stopTime.getArrivalTime() * 1000;
                long time = best.timestamp != 0 ? best.timestamp : currentTime();
                /*
             * if the timpepointrecord needs interpolated (one before, one after),
             * OR
             * we have a single Timepoint record and the arrival is
              * in the future and before the last stop
             */
                if ((predictedDepartureTime > time && predictedDepartureTime < tprStartTime) || (singleTimepointRecord && (predictedDepartureTime > time && scheduledArrivalTime <= lastStopScheduleTime))) {
                    TimepointPredictionRecord tpr = new TimepointPredictionRecord();
                    tpr.setTimepointId(stopTime.getStop().getId());
                    tpr.setTripId(stopTime.getTrip().getId());
                    tpr.setStopSequence(stopTime.getGtfsSequence());
                    tpr.setTimepointPredictedArrivalTime(predictedArrivalTime);
                    tpr.setTimepointPredictedDepartureTime(predictedDepartureTime);
                    tpr.setTimepointScheduledTime(scheduledArrivalTime);
                    timepointPredictions.add(tpr);
                }
            }
        }
    }
    record.setServiceDate(instance.getServiceDate());
    if (blockDescriptor.getStartTime() != null) {
        record.setBlockStartTime(blockDescriptor.getStartTime());
    }
    record.setScheduleDeviation(best.scheduleDeviation);
    if (best.timestamp != 0) {
        record.setTimeOfRecord(best.timestamp);
    }
    record.setTimepointPredictions(timepointPredictions);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) List(java.util.List) ArrayList(java.util.ArrayList) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) HashSet(java.util.HashSet) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate)

Example 5 with BlockConfigurationEntry

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

the class BlockLocationServiceImpl method getScheduledBlockLocationForVehicleLocationCacheRecord.

private ScheduledBlockLocation getScheduledBlockLocationForVehicleLocationCacheRecord(BlockInstance blockInstance, VehicleLocationCacheElement cacheElement, long targetTime) {
    VehicleLocationRecord record = cacheElement.getRecord();
    ScheduledBlockLocation scheduledBlockLocation = cacheElement.getScheduledBlockLocation();
    BlockConfigurationEntry blockConfig = blockInstance.getBlock();
    long serviceDate = blockInstance.getServiceDate();
    int scheduledTime = (int) ((targetTime - serviceDate) / 1000);
    /**
     * If location interpolation has been turned off, then we assume the vehicle
     * is at its last known location, so we return that if it's been stored with
     * the cache element.
     */
    if (!_locationInterpolation && scheduledBlockLocation != null) {
        return scheduledBlockLocation;
    }
    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());
        if (scheduledBlockLocation != null && scheduledBlockLocation.getScheduledTime() <= effectiveScheduledTime) {
            return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(scheduledBlockLocation, effectiveScheduledTime);
        }
        return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
    }
    if (record.isDistanceAlongBlockSet()) {
        if ((_locationInterpolation || _distanceAlongBlockLocationInterpolation) && scheduledBlockLocation != null && scheduledBlockLocation.getDistanceAlongBlock() <= record.getDistanceAlongBlock()) {
            int ellapsedTime = (int) ((targetTime - record.getTimeOfRecord()) / 1000);
            if (ellapsedTime >= 0) {
                int effectiveScheduledTime = scheduledBlockLocation.getScheduledTime() + ellapsedTime;
                return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
            }
            return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(scheduledBlockLocation, record.getDistanceAlongBlock());
        }
        return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(blockConfig, record.getDistanceAlongBlock());
    }
    return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, scheduledTime);
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) 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