Search in sources :

Example 31 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class CurrentVehicleEstimationServiceImpl method getCurrentVehicleEstimates.

@Override
public ListBean<CurrentVehicleEstimateBean> getCurrentVehicleEstimates(CurrentVehicleEstimateQueryBean query) {
    long minT = SystemTime.currentTimeMillis() - _maxWindow * 60 * 1000;
    minT = 0;
    List<Record> records = getRecords(query.getRecords(), minT);
    if (records.isEmpty())
        return new ListBean<CurrentVehicleEstimateBean>();
    List<CurrentVehicleEstimateBean> beans = new ArrayList<CurrentVehicleEstimateBean>();
    if (tryDirectMatchAgainstVehicleId(query, records, beans))
        return new ListBean<CurrentVehicleEstimateBean>(beans, true);
    Map<Date, Record> recordsByTime = getRecordsByTimestamp(records);
    List<Date> timestamps = new ArrayList<Date>(recordsByTime.keySet());
    Collections.sort(timestamps);
    if (tryDirectMatchAgainstBlockId(query, records, recordsByTime, timestamps, query.getMinProbability(), beans))
        return new ListBean<CurrentVehicleEstimateBean>(beans, true);
    Set<BlockSequenceIndex> allIndices = getBlockSequenceIndicesForRecords(recordsByTime);
    for (BlockSequenceIndex index : allIndices) {
        Map<BlockInstance, List<List<BlockLocation>>> allLocations = _blockStatusService.getBlocksForIndex(index, timestamps);
        for (Map.Entry<BlockInstance, List<List<BlockLocation>>> entry : allLocations.entrySet()) {
            BlockInstance blockInstance = entry.getKey();
            List<List<BlockLocation>> realTimeLocations = entry.getValue();
            computeEstimatesForBlockInstance(records, recordsByTime, blockInstance, realTimeLocations, query.getMinProbability(), beans);
        }
    }
    Collections.sort(beans);
    return new ListBean<CurrentVehicleEstimateBean>(beans, false);
}
Also used : BlockSequenceIndex(org.onebusaway.transit_data_federation.services.blocks.BlockSequenceIndex) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date) CurrentVehicleEstimateBean(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateBean) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class CurrentVehicleEstimationServiceImpl method computeCumulativeProbabilityForScheduledBlockLocations.

private void computeCumulativeProbabilityForScheduledBlockLocations(List<Record> records, BlockInstance blockInstance, double minProbabilityForConsideration, List<CurrentVehicleEstimateBean> beans) {
    DoubleArrayList ps = new DoubleArrayList();
    List<ScheduledBlockLocation> blockLocations = new ArrayList<ScheduledBlockLocation>();
    Record firstRecord = records.get(0);
    ScheduledBlockLocation firstLocation = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, firstRecord.getLocation(), firstRecord.getTimestamp(), 0, Double.POSITIVE_INFINITY);
    blockLocations.add(firstLocation);
    ps.add(updateScheduledBlockLocationProbability(blockInstance, firstRecord, firstLocation));
    Record lastRecord = records.get(records.size() - 1);
    ScheduledBlockLocation lastLocation = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, lastRecord.getLocation(), lastRecord.getTimestamp(), 0, Double.POSITIVE_INFINITY);
    ps.add(updateScheduledBlockLocationProbability(blockInstance, lastRecord, lastLocation));
    if (Descriptive.mean(ps) < minProbabilityForConsideration)
        return;
    /**
     * If the vehicle is traveling backwards in time, we kill the prediction
     */
    int maxTravelBackwardsTime = computeMaxTravelBackwardsTime(lastRecord.getTimestamp() - firstRecord.getTimestamp());
    if (lastLocation.getScheduledTime() < firstLocation.getScheduledTime() - maxTravelBackwardsTime)
        return;
    double minDistanceAlongBlock = Math.min(firstLocation.getDistanceAlongBlock(), lastLocation.getDistanceAlongBlock()) - 500;
    double maxDistanceAlongBlock = Math.max(firstLocation.getDistanceAlongBlock(), lastLocation.getDistanceAlongBlock()) + 500;
    for (int i = 1; i < records.size() - 1; i++) {
        Record record = records.get(i);
        ScheduledBlockLocation location = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, record.getLocation(), record.getTimestamp(), minDistanceAlongBlock, maxDistanceAlongBlock);
        blockLocations.add(location);
        ps.add(updateScheduledBlockLocationProbability(blockInstance, record, location));
        if (Descriptive.mean(ps) < minProbabilityForConsideration)
            return;
    }
    blockLocations.add(lastLocation);
    updateProbabilitiesWithScheduleDeviations(records, blockLocations, ps);
    BlockLocation location = _blockLocationService.getLocationForBlockInstanceAndScheduledBlockLocation(blockInstance, lastLocation, lastRecord.getTimestamp());
    double mu = Descriptive.mean(ps);
    String debug = asString(ps);
    addResult(location, mu, debug, minProbabilityForConsideration, beans);
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) DoubleArrayList(cern.colt.list.DoubleArrayList) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 33 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class BlockStatusServiceImpl method getBlocksForBounds.

@Override
public List<BlockLocation> getBlocksForBounds(CoordinateBounds bounds, long time) {
    long timeFrom = time - _runningLateWindow * 1000;
    long timeTo = time + _runningEarlyWindow * 1000;
    List<BlockInstance> instances = _blockGeospatialService.getActiveScheduledBlocksPassingThroughBounds(bounds, timeFrom, timeTo);
    List<BlockLocation> locations = getAsLocations(instances, time);
    List<BlockLocation> inRange = new ArrayList<BlockLocation>();
    for (BlockLocation location : locations) {
        CoordinatePoint p = location.getLocation();
        if (p != null && bounds.contains(p))
            inRange.add(location);
    }
    return inRange;
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Aggregations

BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)33 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)19 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)15 ArrayList (java.util.ArrayList)14 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)12 Date (java.util.Date)11 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)10 List (java.util.List)8 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)8 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)8 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)8 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)7 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5