Search in sources :

Example 46 with StopEntry

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

the class BlockGeospatialServiceImpl method getActiveScheduledBlocksPassingThroughBounds.

@Override
public List<BlockInstance> getActiveScheduledBlocksPassingThroughBounds(CoordinateBounds bounds, long timeFrom, long timeTo) {
    List<StopEntry> stops = _transitGraphDao.getStopsByLocation(bounds);
    Set<AgencyAndId> blockIds = new HashSet<AgencyAndId>();
    for (StopEntry stop : stops) {
        List<BlockStopTimeIndex> stopTimeIndices = _blockIndexService.getStopTimeIndicesForStop(stop);
        Set<BlockConfigurationEntry> blockConfigs = new HashSet<BlockConfigurationEntry>();
        List<List<BlockConfigurationEntry>> blockConfigsList = MappingLibrary.map(stopTimeIndices, "blockConfigs");
        for (List<BlockConfigurationEntry> l : blockConfigsList) {
            blockConfigs.addAll(l);
        }
        List<AgencyAndId> stopBlockIds = MappingLibrary.map(blockConfigs, "block.id");
        blockIds.addAll(stopBlockIds);
    }
    Set<BlockTripIndex> blockIndices = new HashSet<BlockTripIndex>();
    for (AgencyAndId blockId : blockIds) {
        blockIndices.addAll(_blockIndexService.getBlockTripIndicesForBlock(blockId));
    }
    List<BlockLayoverIndex> layoverIndices = Collections.emptyList();
    List<FrequencyBlockTripIndex> frequencyIndices = Collections.emptyList();
    return _blockCalendarService.getActiveBlocksInTimeRange(blockIndices, layoverIndices, frequencyIndices, timeFrom, timeTo);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockLayoverIndex(org.onebusaway.transit_data_federation.services.blocks.BlockLayoverIndex) FrequencyBlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockTripIndex) BlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.BlockStopTimeIndex) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) List(java.util.List) ArrayList(java.util.ArrayList) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) FrequencyBlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockTripIndex) BlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.BlockTripIndex) HashSet(java.util.HashSet)

Example 47 with StopEntry

use of org.onebusaway.transit_data_federation.services.transit_graph.StopEntry 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 48 with StopEntry

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

the class RouteBeanServiceImpl method getStopsForRouteCollectionAndNarrative.

private StopsForRouteBean getStopsForRouteCollectionAndNarrative(RouteCollectionEntry routeCollection, RouteCollectionNarrative narrative) {
    StopsForRouteBean result = new StopsForRouteBean();
    AgencyAndId routeCollectionId = routeCollection.getId();
    result.setRoute(getRouteBeanForRouteCollection(routeCollectionId, narrative));
    result.setStops(getStopBeansForRoute(routeCollectionId));
    result.setPolylines(getEncodedPolylinesForRoute(routeCollection));
    StopGroupingBean directionGrouping = new StopGroupingBean();
    directionGrouping.setType(TransitDataConstants.STOP_GROUPING_TYPE_DIRECTION);
    List<StopGroupBean> directionGroups = new ArrayList<StopGroupBean>();
    directionGrouping.setStopGroups(directionGroups);
    directionGrouping.setOrdered(true);
    result.addGrouping(directionGrouping);
    List<BlockTripIndex> blockIndices = _blockIndexService.getBlockTripIndicesForRouteCollectionId(routeCollectionId);
    List<FrequencyBlockTripIndex> frequencyBlockIndices = _blockIndexService.getFrequencyBlockTripIndicesForRouteCollectionId(routeCollectionId);
    List<BlockTripEntry> blockTrips = new ArrayList<BlockTripEntry>();
    getBlockTripsForIndicesMatchingRouteCollection(blockIndices, routeCollectionId, blockTrips);
    getBlockTripsForIndicesMatchingRouteCollection(frequencyBlockIndices, routeCollectionId, blockTrips);
    List<StopSequence> sequences = _stopSequencesService.getStopSequencesForTrips(blockTrips);
    List<StopSequenceCollection> blocks = _stopSequenceBlocksService.getStopSequencesAsCollections(sequences);
    for (StopSequenceCollection block : blocks) {
        NameBean name = new NameBean(NameBeanTypes.DESTINATION, block.getDescription());
        List<StopEntry> stops = getStopsInOrder(block);
        List<String> groupStopIds = new ArrayList<String>();
        for (StopEntry stop : stops) groupStopIds.add(ApplicationBeanLibrary.getId(stop.getId()));
        Set<AgencyAndId> shapeIds = getShapeIdsForStopSequenceBlock(block);
        List<EncodedPolylineBean> polylines = _shapeBeanService.getMergedPolylinesForShapeIds(shapeIds);
        StopGroupBean group = new StopGroupBean();
        group.setId(block.getPublicId());
        group.setName(name);
        group.setStopIds(groupStopIds);
        group.setPolylines(polylines);
        directionGroups.add(group);
    }
    sortResult(result);
    return result;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence) FrequencyBlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockTripIndex) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) NameBean(org.onebusaway.transit_data.model.NameBean) EncodedPolylineBean(org.onebusaway.geospatial.model.EncodedPolylineBean) BlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.BlockTripIndex) FrequencyBlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockTripIndex) AbstractBlockTripIndex(org.onebusaway.transit_data_federation.services.blocks.AbstractBlockTripIndex) StopSequenceCollection(org.onebusaway.transit_data_federation.model.StopSequenceCollection)

Example 49 with StopEntry

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

the class RoutesBeanServiceImpl method setup.

@Refreshable(dependsOn = { RefreshableResources.NARRATIVE_DATA })
@PostConstruct
public void setup() {
    _stopTreesByRouteId.clear();
    for (StopEntry stop : _graphDao.getAllStops()) {
        Set<AgencyAndId> routeIds = _routeService.getRouteCollectionIdsForStop(stop.getId());
        for (AgencyAndId routeId : routeIds) {
            STRtree tree = _stopTreesByRouteId.get(routeId);
            if (tree == null) {
                tree = new STRtree();
                _stopTreesByRouteId.put(routeId, tree);
            }
            double x = stop.getStopLon();
            double y = stop.getStopLat();
            Envelope env = new Envelope(x, x, y, y);
            tree.insert(env, routeId);
        }
    }
    for (STRtree tree : _stopTreesByRouteId.values()) tree.build();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) STRtree(com.vividsolutions.jts.index.strtree.STRtree) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) Envelope(com.vividsolutions.jts.geom.Envelope) Refreshable(org.onebusaway.container.refresh.Refreshable) PostConstruct(javax.annotation.PostConstruct)

Example 50 with StopEntry

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

the class StopBeanServiceImpl method getStopForId.

@Cacheable
public StopBean getStopForId(AgencyAndId id) {
    StopEntry stop = _transitGraphDao.getStopEntryForId(id);
    StopNarrative narrative = _narrativeService.getStopForId(id);
    if (stop == null) {
        // try looking up consolidated id
        AgencyAndId consolidatedId = _consolidatedStopsService.getConsolidatedStopIdForHiddenStopId(id);
        if (consolidatedId != null)
            return getStopForId(consolidatedId);
        throw new NoSuchStopServiceException(AgencyAndIdLibrary.convertToString(id));
    }
    StopBean sb = new StopBean();
    fillStopBean(stop, narrative, sb);
    fillRoutesForStopBean(stop, sb);
    return sb;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) NoSuchStopServiceException(org.onebusaway.exceptions.NoSuchStopServiceException) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) StopBean(org.onebusaway.transit_data.model.StopBean) StopNarrative(org.onebusaway.transit_data_federation.model.narrative.StopNarrative) Cacheable(org.onebusaway.container.cache.Cacheable)

Aggregations

StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)54 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)28 ArrayList (java.util.ArrayList)15 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)14 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)12 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)12 StopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry)12 BlockStopTimeIndex (org.onebusaway.transit_data_federation.services.blocks.BlockStopTimeIndex)10 List (java.util.List)9 FactoryMap (org.onebusaway.collections.FactoryMap)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Test (org.junit.Test)8 StopNarrative (org.onebusaway.transit_data_federation.model.narrative.StopNarrative)8 HashSet (java.util.HashSet)7 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 StopEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)7 Cacheable (org.onebusaway.container.cache.Cacheable)6 Stop (org.onebusaway.gtfs.model.Stop)6 StopBean (org.onebusaway.transit_data.model.StopBean)4