Search in sources :

Example 1 with ScheduledBlockLocation

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

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

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

the class BlockBeanServiceImpl method getScheduledBlockLocationFromScheduledTime.

@Override
public ScheduledBlockLocationBean getScheduledBlockLocationFromScheduledTime(AgencyAndId blockId, long serviceDate, int scheduledTime) {
    BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
    if (blockInstance == null)
        return null;
    ScheduledBlockLocation blockLocation = _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockInstance.getBlock(), scheduledTime);
    if (blockLocation == null)
        return null;
    return getBlockLocationAsBean(blockLocation);
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance)

Example 4 with ScheduledBlockLocation

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

Example 5 with ScheduledBlockLocation

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

the class CurrentVehicleEstimationServiceImpl method updateProbabilitiesWithScheduleDeviations.

private void updateProbabilitiesWithScheduleDeviations(List<Record> records, List<ScheduledBlockLocation> blockLocations, DoubleArrayList ps) {
    if (records.size() != blockLocations.size())
        throw new IllegalStateException();
    if (records.size() != ps.size())
        throw new IllegalStateException();
    for (int i = 1; i < records.size(); i++) {
        Record prevRecord = records.get(i - 1);
        Record nextRecord = records.get(i);
        long recordDeltaT = (nextRecord.getTimestamp() - prevRecord.getTimestamp());
        if (recordDeltaT <= 0)
            continue;
        int maxTravelBackwardsTime = computeMaxTravelBackwardsTime(recordDeltaT);
        ScheduledBlockLocation prevLocation = blockLocations.get(i - 1);
        ScheduledBlockLocation nextLocation = blockLocations.get(i);
        int locationDeltaT = nextLocation.getScheduledTime() - prevLocation.getScheduledTime();
        if (locationDeltaT < 0 && Math.abs(locationDeltaT) > maxTravelBackwardsTime)
            ps.set(i, 0.0);
    }
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

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