Search in sources :

Example 31 with ScheduledBlockLocation

use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method handleVehicleLocationRecord.

/**
 **
 * {@link BlockVehicleLocationListener} Interface
 ***
 */
@Override
public void handleVehicleLocationRecord(VehicleLocationRecord record) {
    BlockInstance instance = getVehicleLocationRecordAsBlockInstance(record);
    if (instance != null) {
        ScheduledBlockLocation scheduledBlockLocation = getScheduledBlockLocationForVehicleLocationRecord(record, instance);
        if (!record.isScheduleDeviationSet()) {
            int deviation = (int) ((record.getTimeOfRecord() - record.getServiceDate()) / 1000 - scheduledBlockLocation.getScheduledTime());
            record.setScheduleDeviation(deviation);
        }
        ScheduleDeviationSamples samples = null;
        if (_sampleScheduleDeviationHistory == true) {
            samples = _realTimeHistoryService.sampleScheduleDeviationsForVehicle(instance, record, scheduledBlockLocation);
        }
        putBlockLocationRecord(instance, record, scheduledBlockLocation, samples);
    }
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ScheduleDeviationSamples(org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 32 with ScheduledBlockLocation

use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation 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 ScheduledBlockLocation

use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation 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 34 with ScheduledBlockLocation

use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.

the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationBetweenStopTimes.

private ScheduledBlockLocation getScheduledBlockLocationBetweenStopTimes(List<BlockStopTimeEntry> stopTimes, int scheduleTime, int stopTimeIndex) {
    BlockStopTimeEntry blockBefore = stopTimes.get(stopTimeIndex - 1);
    BlockStopTimeEntry blockAfter = stopTimes.get(stopTimeIndex);
    StopTimeEntry before = blockBefore.getStopTime();
    StopTimeEntry after = blockAfter.getStopTime();
    ScheduledBlockLocation result = new ScheduledBlockLocation();
    result.setScheduledTime(scheduleTime);
    result.setInService(true);
    result.setStopTimeIndex(stopTimeIndex);
    int fromTime = before.getDepartureTime();
    int toTime = after.getArrivalTime();
    int fromTimeOffset = fromTime - scheduleTime;
    int toTimeOffset = toTime - scheduleTime;
    if (Math.abs(fromTimeOffset) < Math.abs(toTimeOffset)) {
        result.setClosestStop(blockBefore);
        result.setClosestStopTimeOffset(fromTimeOffset);
    } else {
        result.setClosestStop(blockAfter);
        result.setClosestStopTimeOffset(toTimeOffset);
    }
    result.setPreviousStop(blockBefore);
    result.setNextStop(blockAfter);
    result.setNextStopTimeOffset(toTimeOffset);
    double ratio = (scheduleTime - fromTime) / ((double) (toTime - fromTime));
    double fromDistance = blockBefore.getDistanceAlongBlock();
    double toDistance = blockAfter.getDistanceAlongBlock();
    double distanceAlongBlock = ratio * (toDistance - fromDistance) + fromDistance;
    result.setDistanceAlongBlock(distanceAlongBlock);
    int shapePointIndexFrom = -1;
    int shapePointIndexTo = -1;
    /**
     * Are we between trips? Where is the transition point?
     */
    if (!before.getTrip().equals(after.getTrip())) {
        if (distanceAlongBlock >= blockAfter.getTrip().getDistanceAlongBlock()) {
            result.setActiveTrip(blockAfter.getTrip());
            shapePointIndexFrom = 0;
            shapePointIndexTo = nextShapePointIndex(after);
        } else {
            result.setActiveTrip(blockBefore.getTrip());
            shapePointIndexFrom = before.getShapePointIndex();
            shapePointIndexTo = Integer.MAX_VALUE;
        }
    } else {
        result.setActiveTrip(blockBefore.getTrip());
        shapePointIndexFrom = before.getShapePointIndex();
        shapePointIndexTo = nextShapePointIndex(after);
    }
    BlockTripEntry activeTrip = result.getActiveTrip();
    PointAndOrientation po = getLocationAlongShape(activeTrip, distanceAlongBlock, shapePointIndexFrom, shapePointIndexTo);
    if (po != null) {
        result.setLocation(po.getPoint());
        result.setOrientation(po.getOrientation());
        return result;
    }
    StopEntry beforeStop = before.getStop();
    StopEntry afterStop = after.getStop();
    double latFrom = beforeStop.getStopLat();
    double lonFrom = beforeStop.getStopLon();
    double latTo = afterStop.getStopLat();
    double lonTo = afterStop.getStopLon();
    double lat = (latTo - latFrom) * ratio + latFrom;
    double lon = (lonTo - lonFrom) * ratio + lonFrom;
    CoordinatePoint location = new CoordinatePoint(lat, lon);
    result.setLocation(location);
    double orientation = SphericalGeometryLibrary.getOrientation(latFrom, lonFrom, latTo, lonTo);
    result.setOrientation(orientation);
    return result;
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) PointAndOrientation(org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 35 with ScheduledBlockLocation

use of org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation in project onebusaway-application-modules by camsys.

the class ScheduledBlockLocationServiceImpl method interpolateLocation.

private ScheduledBlockLocation interpolateLocation(BlockStopTimeEntry blockPrevious, BlockStopTimeEntry blockFrom, BlockStopTimeEntry blockTo, double distanceAlongBlock, int stopTimeIndex) {
    if (distanceAlongBlock < 0.0)
        return null;
    StopTimeEntry from = blockFrom.getStopTime();
    StopTimeEntry to = blockTo.getStopTime();
    double r = (distanceAlongBlock - blockFrom.getDistanceAlongBlock()) / (blockTo.getDistanceAlongBlock() - blockFrom.getDistanceAlongBlock());
    int scheduledTime = (int) (r * (to.getArrivalTime() - from.getDepartureTime()) + from.getDepartureTime());
    if (r > 1)
        scheduledTime += to.getSlackTime();
    BlockTripEntry activeTrip = distanceAlongBlock < blockTo.getDistanceAlongBlock() ? blockFrom.getTrip() : blockTo.getTrip();
    BlockStopTimeEntry closestStop = r < 0.5 ? blockFrom : blockTo;
    BlockStopTimeEntry previousStop = null;
    BlockStopTimeEntry nextStop = null;
    int shapePointIndexFrom = -1;
    int shapePointIndexTo = -1;
    if (r <= 0) {
        /**
         * Location along the block is before the two stop times
         */
        previousStop = blockPrevious;
        nextStop = blockFrom;
        shapePointIndexFrom = 0;
        shapePointIndexTo = nextShapePointIndex(from);
    } else if (r <= 1.0) {
        /**
         * Location along the block is between the two stop times
         */
        previousStop = blockFrom;
        nextStop = blockTo;
        shapePointIndexFrom = from.getShapePointIndex();
        shapePointIndexTo = nextShapePointIndex(to);
    } else {
        /**
         * Location along the block is after the two stop times
         */
        shapePointIndexFrom = to.getShapePointIndex();
        shapePointIndexTo = Integer.MAX_VALUE;
    }
    ScheduledBlockLocation location = new ScheduledBlockLocation();
    location.setActiveTrip(activeTrip);
    location.setClosestStop(closestStop);
    location.setClosestStopTimeOffset(closestStop.getStopTime().getArrivalTime() - scheduledTime);
    location.setPreviousStop(previousStop);
    location.setNextStop(nextStop);
    if (nextStop != null)
        location.setNextStopTimeOffset(nextStop.getStopTime().getArrivalTime() - scheduledTime);
    location.setInService(nextStop != null);
    location.setStopTimeIndex(stopTimeIndex);
    location.setDistanceAlongBlock(distanceAlongBlock);
    location.setScheduledTime(scheduledTime);
    /**
     * In this case, distance along block and distance along trip are the same
     * because we are still in the first trip of the block
     */
    PointAndOrientation po = getLocationAlongShape(activeTrip, distanceAlongBlock, shapePointIndexFrom, shapePointIndexTo);
    if (po != null) {
        location.setLocation(po.getPoint());
        location.setOrientation(po.getOrientation());
    }
    return location;
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) PointAndOrientation(org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Aggregations

ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)35 Test (org.junit.Test)15 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)13 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)10 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)10 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)9 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)9 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)8 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)7 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)6 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)6 Date (java.util.Date)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)5 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)5 PointAndOrientation (org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation)4 StopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry)4 ArrayList (java.util.ArrayList)3 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)3