Search in sources :

Example 46 with StopBean

use of org.onebusaway.transit_data.model.StopBean in project onebusaway-application-modules by camsys.

the class SearchServiceImpl method findStopsNearPoint.

@Override
public SearchResultCollection findStopsNearPoint(Double latitude, Double longitude, SearchResultFactory resultFactory, Set<RouteBean> routeFilter) {
    CoordinateBounds bounds = SphericalGeometryLibrary.bounds(latitude, longitude, DISTANCE_TO_STOPS);
    SearchQueryBean queryBean = new SearchQueryBean();
    queryBean.setType(SearchQueryBean.EQueryType.BOUNDS_OR_CLOSEST);
    queryBean.setBounds(bounds);
    queryBean.setMaxCount(100);
    StopsBean stops = _transitDataService.getStops(queryBean);
    Collections.sort(stops.getStops(), new StopDistanceFromPointComparator(latitude, longitude));
    // A list of stops that will go in our search results
    List<StopBean> stopsForResults = new ArrayList<StopBean>();
    // Keep track of which routes are already in our search results by
    // direction
    Map<String, List<RouteBean>> routesByDirectionAlreadyInResults = new HashMap<String, List<RouteBean>>();
    // Cache stops by route so we don't need to call the transit data
    // service repeatedly for the same route
    Map<String, StopsForRouteBean> stopsForRouteLookup = new HashMap<String, StopsForRouteBean>();
    // direction to our final results.
    for (StopBean stopBean : stops.getStops()) {
        String agencyId = AgencyAndIdLibrary.convertFromString(stopBean.getId()).getAgencyId();
        if (!_transitDataService.stopHasRevenueService(agencyId, stopBean.getId())) {
            continue;
        }
        // Get the stop bean that is actually inside this search result. We
        // kept track of it earlier.
        // StopBean stopBean = stopBeanBySearchResult.get(stopResult);
        // Record of routes by direction id for this stop
        Map<String, List<RouteBean>> routesByDirection = new HashMap<String, List<RouteBean>>();
        for (RouteBean route : stopBean.getRoutes()) {
            // route is a route serving the current stopBeanForSearchResult
            // Query for all stops on this route
            StopsForRouteBean stopsForRoute = stopsForRouteLookup.get(route.getId());
            if (stopsForRoute == null) {
                stopsForRoute = _transitDataService.getStopsForRoute(route.getId());
                stopsForRouteLookup.put(route.getId(), stopsForRoute);
            }
            // corresponds to a GTFS direction id for this route.
            for (StopGroupingBean stopGrouping : stopsForRoute.getStopGroupings()) {
                for (StopGroupBean stopGroup : stopGrouping.getStopGroups()) {
                    String directionId = stopGroup.getId();
                    // direction. If so, record it.
                    if (stopGroup.getStopIds().contains(stopBean.getId())) {
                        if (!routesByDirection.containsKey(directionId)) {
                            routesByDirection.put(directionId, new ArrayList<RouteBean>());
                        }
                        routesByDirection.get(directionId).add(route);
                    }
                }
            }
        }
        // Iterate over routes binned by direction for this stop and compare
        // to routes by direction already in our search results
        boolean shouldAddStopToResults = false;
        for (Map.Entry<String, List<RouteBean>> entry : routesByDirection.entrySet()) {
            String directionId = entry.getKey();
            List<RouteBean> routesForThisDirection = entry.getValue();
            if (!routesByDirectionAlreadyInResults.containsKey(directionId)) {
                routesByDirectionAlreadyInResults.put(directionId, new ArrayList<RouteBean>());
            }
            @SuppressWarnings("unchecked") List<RouteBean> additionalRoutes = ListUtils.subtract(routesForThisDirection, routesByDirectionAlreadyInResults.get(directionId));
            if (additionalRoutes.size() > 0) {
                // This stop is contributing new routes in this direction,
                // so add these additional
                // stops to our record of stops by direction already in
                // search results and toggle
                // flag that tells to to add the stop to the search results.
                routesByDirectionAlreadyInResults.get(directionId).addAll(additionalRoutes);
                // We use this flag because we want to add new routes to our
                // record potentially for each
                // direction id, but we only want to add the stop to the
                // search results once. It happens below.
                shouldAddStopToResults = true;
            }
        }
        if (shouldAddStopToResults) {
            // Add the stop to our search results
            stopsForResults.add(stopBean);
        }
        // Break out of iterating through stops if we've reached our max
        if (stopsForResults.size() >= MAX_STOPS) {
            break;
        }
    }
    // Create our search results object, iterate through our stops, create
    // stop
    // results from each of those stops, and add them to the search results.
    SearchResultCollection results = new SearchResultCollection();
    results.addRouteFilters(routeFilter);
    for (StopBean stop : stopsForResults) {
        SearchResult result = resultFactory.getStopResult(stop, routeFilter);
        results.addMatch(result);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) SearchResult(org.onebusaway.presentation.model.SearchResult) RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) SearchResultCollection(org.onebusaway.presentation.model.SearchResultCollection) StopBean(org.onebusaway.transit_data.model.StopBean) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) StopsBean(org.onebusaway.transit_data.model.StopsBean)

Example 47 with StopBean

use of org.onebusaway.transit_data.model.StopBean in project onebusaway-application-modules by camsys.

the class ServiceAlertsHelper method addSituationExchangeToSiriForStops.

public void addSituationExchangeToSiriForStops(ServiceDelivery serviceDelivery, List<MonitoredStopVisitStructure> visits, TransitDataService transitDataService, List<AgencyAndId> stopIds) {
    Map<String, PtSituationElementStructure> ptSituationElements = new HashMap<String, PtSituationElementStructure>();
    for (MonitoredStopVisitStructure visit : visits) {
        if (visit.getMonitoredVehicleJourney() != null)
            addSituationElement(transitDataService, ptSituationElements, visit.getMonitoredVehicleJourney().getSituationRef());
    }
    if (stopIds != null && stopIds.size() > 0) {
        for (AgencyAndId stopId : stopIds) {
            String stopIdString = stopId.toString();
            // First get service alerts for the stop
            SituationQueryBean query = new SituationQueryBean();
            List<String> stopIdStrings = new ArrayList<String>();
            stopIdStrings.add(stopIdString);
            SituationQueryBean.AffectsBean affects = new SituationQueryBean.AffectsBean();
            query.getAffects().add(affects);
            affects.setStopId(stopIdString);
            addFromQuery(transitDataService, ptSituationElements, query);
            // Now also add service alerts for (route+direction)s of the stop
            query = new SituationQueryBean();
            StopBean stopBean = transitDataService.getStop(stopIdString);
            List<RouteBean> routes = stopBean.getRoutes();
            for (RouteBean route : routes) {
                StopsForRouteBean stopsForRoute = transitDataService.getStopsForRoute(route.getId());
                List<StopGroupingBean> stopGroupings = stopsForRoute.getStopGroupings();
                for (StopGroupingBean stopGrouping : stopGroupings) {
                    if (!stopGrouping.getType().equalsIgnoreCase("direction"))
                        continue;
                    for (StopGroupBean stopGroup : stopGrouping.getStopGroups()) {
                        handleStopGroupBean(stopIdString, query, route, stopGroup);
                    }
                }
            }
            addFromQuery(transitDataService, ptSituationElements, query);
        }
    }
    addPtSituationElementsToServiceDelivery(serviceDelivery, ptSituationElements);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) PtSituationElementStructure(uk.org.siri.siri.PtSituationElementStructure) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) MonitoredStopVisitStructure(uk.org.siri.siri.MonitoredStopVisitStructure) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) SituationQueryBean(org.onebusaway.transit_data.model.service_alerts.SituationQueryBean) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 48 with StopBean

use of org.onebusaway.transit_data.model.StopBean in project onebusaway-application-modules by camsys.

the class StopSelectionServiceImpl method groupByDirection.

private void groupByDirection(StopSelectionTreeBean tree, StopsForRouteBean stopsForRoute, StopGroupingBean byDirection, Map<String, StopBean> stopsById) {
    List<StopGroupBean> groups = byDirection.getStopGroups();
    if (groups.isEmpty()) {
        groupByStop(tree, stopsForRoute.getStops());
        return;
    }
    for (StopGroupBean group : groups) {
        StopSelectionTreeBean subTree = tree.getSubTree(group.getName());
        List<StopBean> stops = new ArrayList<StopBean>();
        for (String stopId : group.getStopIds()) stops.add(stopsById.get(stopId));
        groupByStop(subTree, stops);
    }
}
Also used : StopSelectionTreeBean(org.onebusaway.presentation.model.StopSelectionTreeBean) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 49 with StopBean

use of org.onebusaway.transit_data.model.StopBean in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesModel method updateCurrentUser.

private void updateCurrentUser() {
    // Save the last selected stop id
    _currentUserService.setLastSelectedStopIds(_stopIds);
    _user = _currentUserService.getCurrentUser();
    if (_user == null || !_user.hasDefaultLocation()) {
        List<StopBean> stops = _result.getStops();
        StopBean stop = stops.get(0);
        _defaultSearchLocationService.setDefaultLocationForCurrentUser(stop.getName(), stop.getLat(), stop.getLon());
    }
}
Also used : StopBean(org.onebusaway.transit_data.model.StopBean)

Example 50 with StopBean

use of org.onebusaway.transit_data.model.StopBean in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesBeanServiceImplTest method test.

@Test
public void test() {
    long t = dateAsLong("2010-10-05 16:30");
    long serviceDate = dateAsLong("2010-10-05 00:00");
    int minutesBefore = 5;
    int minutesAfter = 30;
    StopEntryImpl stopA = stop("stopA", 47.0, -122.0);
    StopEntryImpl stopB = stop("stopB", 47.0, -122.0);
    Mockito.when(_transitGraphDao.getStopEntryForId(stopA.getId(), true)).thenReturn(stopA);
    Mockito.when(_transitGraphDao.getStopEntryForId(stopB.getId(), true)).thenReturn(stopB);
    /**
     **
     * Block A
     ***
     */
    BlockEntryImpl blockA = block("blockA");
    TripEntryImpl tripA = trip("tripA", "sA", 3000);
    stopTime(0, stopA, tripA, time(16, 30), time(16, 35), 1000);
    StopTimeEntryImpl stopTimeAB = stopTime(1, stopB, tripA, time(16, 40), time(16, 45), 2000);
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds(lsids("sA"), lsids()), tripA);
    BlockStopTimeEntry bstAA = blockConfigA.getStopTimes().get(0);
    BlockStopTimeEntry bstAB = blockConfigA.getStopTimes().get(1);
    /**
     **
     * Block B
     ***
     */
    BlockEntryImpl blockB = block("blockB");
    TripEntryImpl tripB = trip("tripB", "sA", 3000);
    stopTime(2, stopA, tripB, time(16, 40), time(16, 45), 1000);
    StopTimeEntryImpl stopTimeBB = stopTime(3, stopB, tripB, time(16, 50), time(16, 55), 2000);
    BlockConfigurationEntry blockConfigB = blockConfiguration(blockB, serviceIds(lsids("sA"), lsids()), tripB);
    BlockStopTimeEntry bstBA = blockConfigB.getStopTimes().get(0);
    BlockStopTimeEntry bstBB = blockConfigB.getStopTimes().get(1);
    /**
     **
     *
     ***
     */
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, serviceDate);
    long lastUpdateTime = dateAsLong("2010-10-05 16:15");
    BlockLocation blockLocationA = new BlockLocation();
    blockLocationA.setActiveTrip(bstAA.getTrip());
    blockLocationA.setBlockInstance(blockInstanceA);
    blockLocationA.setClosestStop(bstAA);
    blockLocationA.setDistanceAlongBlock(500);
    blockLocationA.setInService(true);
    blockLocationA.setLastUpdateTime(lastUpdateTime);
    blockLocationA.setNextStop(bstAA);
    blockLocationA.setPredicted(true);
    blockLocationA.setScheduledDistanceAlongBlock(600);
    blockLocationA.setScheduleDeviation(10 * 60);
    blockLocationA.setVehicleId(aid("vehicle"));
    /**
     **
     *
     ***
     */
    BlockInstance blockInstanceB = new BlockInstance(blockConfigB, serviceDate);
    BlockLocation blockLocationB = new BlockLocation();
    blockLocationB.setActiveTrip(bstBA.getTrip());
    blockLocationB.setBlockInstance(blockInstanceA);
    blockLocationB.setClosestStop(bstBA);
    blockLocationB.setDistanceAlongBlock(400);
    blockLocationB.setInService(true);
    blockLocationB.setNextStop(bstAA);
    blockLocationB.setPredicted(false);
    blockLocationB.setScheduledDistanceAlongBlock(400);
    /**
     **
     *
     ***
     */
    long stopTimeFrom = t - minutesBefore * 60 * 1000;
    long stopTimeTo = t + minutesAfter * 60 * 1000;
    StopTimeInstance sti1 = new StopTimeInstance(bstAB, blockInstanceA.getState());
    ArrivalAndDepartureInstance in1 = new ArrivalAndDepartureInstance(sti1);
    in1.setBlockLocation(blockLocationA);
    in1.setPredictedArrivalTime((long) (in1.getScheduledArrivalTime() + 5 * 60 * 1000));
    in1.setPredictedDepartureTime((long) (in1.getScheduledDepartureTime()));
    StopTimeInstance sti2 = new StopTimeInstance(bstBB, blockInstanceB.getState());
    ArrivalAndDepartureInstance in2 = new ArrivalAndDepartureInstance(sti2);
    in2.setBlockLocation(blockLocationB);
    TargetTime target = new TargetTime(t, t);
    Mockito.when(_arrivalAndDepartureService.getArrivalsAndDeparturesForStopInTimeRange(stopB, target, stopTimeFrom, stopTimeTo)).thenReturn(Arrays.asList(in1, in2));
    /**
     **
     *
     ***
     */
    Builder stopTimeNarrative = StopTimeNarrative.builder();
    stopTimeNarrative.setStopHeadsign("Downtown");
    Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeAB)).thenReturn(stopTimeNarrative.create());
    stopTimeNarrative = StopTimeNarrative.builder();
    stopTimeNarrative.setRouteShortName("XX");
    Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeBB)).thenReturn(stopTimeNarrative.create());
    /**
     **
     *
     ***
     */
    StopBean stopABean = new StopBean();
    stopABean.setId("1_stopA");
    Mockito.when(_stopBeanService.getStopForId(stopA.getId())).thenReturn(stopABean);
    StopBean stopBBean = new StopBean();
    stopBBean.setId("1_stopB");
    Mockito.when(_stopBeanService.getStopForId(stopB.getId())).thenReturn(stopBBean);
    /**
     **
     *
     ***
     */
    TripBean tripABean = new TripBean();
    Mockito.when(_tripBeanService.getTripForId(aid("tripA"))).thenReturn(tripABean);
    TripBean tripBBean = new TripBean();
    Mockito.when(_tripBeanService.getTripForId(aid("tripB"))).thenReturn(tripBBean);
    /**
     **
     *
     ***
     */
    TripStatusBean tripStatusBeanA = new TripStatusBean();
    TripStatusBean tripStatusBeanB = new TripStatusBean();
    Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationA, t)).thenReturn(tripStatusBeanA);
    Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationB, t)).thenReturn(tripStatusBeanB);
    /**
     **
     *
     ***
     */
    ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
    query.setTime(t);
    query.setMinutesBefore(minutesBefore);
    query.setMinutesAfter(minutesAfter);
    query.setFrequencyMinutesBefore(minutesBefore);
    query.setFrequencyMinutesAfter(minutesAfter);
    List<ArrivalAndDepartureBean> arrivalsAndDepartures = _service.getArrivalsAndDeparturesByStopId(stopB.getId(), query);
    assertEquals(2, arrivalsAndDepartures.size());
    ArrivalAndDepartureBean bean = arrivalsAndDepartures.get(0);
    assertEquals(1500, bean.getDistanceFromStop(), 0.0);
    assertEquals(lastUpdateTime, bean.getLastUpdateTime().longValue());
    assertEquals(1, bean.getNumberOfStopsAway());
    assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedArrivalTime());
    assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedDepartureTime());
    assertNull(bean.getRouteShortName());
    assertEquals(dateAsLong("2010-10-05 16:40"), bean.getScheduledArrivalTime());
    assertEquals(dateAsLong("2010-10-05 16:45"), bean.getScheduledDepartureTime());
    assertEquals(serviceDate, bean.getServiceDate());
    assertEquals("default", bean.getStatus());
    assertSame(stopBBean, bean.getStop());
    assertSame(tripABean, bean.getTrip());
    assertSame(tripStatusBeanA, bean.getTripStatus());
    assertEquals("Downtown", bean.getTripHeadsign());
    assertEquals("1_vehicle", bean.getVehicleId());
    bean = arrivalsAndDepartures.get(1);
    assertEquals(1600, bean.getDistanceFromStop(), 0.0);
    assertNull(bean.getLastUpdateTime());
    assertEquals(1, bean.getNumberOfStopsAway());
    assertEquals(0L, bean.getPredictedArrivalTime());
    assertEquals(0L, bean.getPredictedDepartureTime());
    assertEquals("XX", bean.getRouteShortName());
    assertEquals(dateAsLong("2010-10-05 16:50"), bean.getScheduledArrivalTime());
    assertEquals(dateAsLong("2010-10-05 16:55"), bean.getScheduledDepartureTime());
    assertEquals(serviceDate, bean.getServiceDate());
    assertEquals("default", bean.getStatus());
    assertSame(stopBBean, bean.getStop());
    assertSame(tripBBean, bean.getTrip());
    assertSame(tripStatusBeanB, bean.getTripStatus());
    assertNull(bean.getTripHeadsign());
    assertNull(bean.getVehicleId());
}
Also used : StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) StopTimeEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl) Builder(org.onebusaway.transit_data_federation.model.narrative.StopTimeNarrative.Builder) TripBean(org.onebusaway.transit_data.model.trips.TripBean) ArrivalsAndDeparturesQueryBean(org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) StopBean(org.onebusaway.transit_data.model.StopBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) Test(org.junit.Test)

Aggregations

StopBean (org.onebusaway.transit_data.model.StopBean)69 ArrayList (java.util.ArrayList)34 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)23 RouteBean (org.onebusaway.transit_data.model.RouteBean)22 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)17 StopGroupBean (org.onebusaway.transit_data.model.StopGroupBean)15 StopGroupingBean (org.onebusaway.transit_data.model.StopGroupingBean)15 HashMap (java.util.HashMap)12 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)9 ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)9 NameBean (org.onebusaway.transit_data.model.NameBean)9 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)8 TripBean (org.onebusaway.transit_data.model.trips.TripBean)8 HashSet (java.util.HashSet)7 StopsBean (org.onebusaway.transit_data.model.StopsBean)7 List (java.util.List)6 BlockTripBean (org.onebusaway.transit_data.model.blocks.BlockTripBean)6 Test (org.junit.Test)5 SearchQueryBean (org.onebusaway.transit_data.model.SearchQueryBean)5 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)5