Search in sources :

Example 41 with RouteBean

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

the class SearchServiceImpl method tryAsRoute.

private void tryAsRoute(SearchResultCollection results, String routeQueryMixedCase, SearchResultFactory resultFactory) {
    String routeQuery = new String(routeQueryMixedCase);
    if (routeQuery == null || StringUtils.isEmpty(routeQuery)) {
        return;
    }
    routeQuery = routeQuery.toUpperCase().trim();
    if (routeQuery.length() < 1) {
        return;
    }
    // agency + route id matching (from direct links) as exact case
    if (_routeIdToRouteBeanMap.get(routeQueryMixedCase) != null) {
        RouteBean routeBean = _routeIdToRouteBeanMap.get(routeQueryMixedCase);
        results.addMatch(resultFactory.getRouteResult(routeBean));
        // if we've matched, assume no others
        return;
    }
    // agency + route id matching (from direct links) as upper case
    if (_routeIdToRouteBeanMap.get(routeQuery) != null) {
        RouteBean routeBean = _routeIdToRouteBeanMap.get(routeQuery);
        results.addMatch(resultFactory.getRouteResult(routeBean));
        // if we've matched, assume no others
        return;
    }
    // short name matching
    if (_routeShortNameToRouteBeanMap.get(routeQuery) != null) {
        for (RouteBean routeBean : _routeShortNameToRouteBeanMap.get(routeQuery)) {
            results.addMatch(resultFactory.getRouteResult(routeBean));
        }
    }
    for (String routeShortName : _routeShortNameToRouteBeanMap.keySet()) {
        // if the route short name ends or starts with our query, and
        // whatever's left over
        // matches the regex
        String leftOvers = routeShortName.replace(routeQuery, "");
        Matcher matcher = leftOverMatchPattern.matcher(leftOvers);
        Boolean leftOversAreDiscardable = matcher.find();
        if (!routeQuery.equals(routeShortName) && ((routeShortName.startsWith(routeQuery) && leftOversAreDiscardable) || (routeShortName.endsWith(routeQuery) && leftOversAreDiscardable))) {
            try {
                for (RouteBean routeBean : _routeShortNameToRouteBeanMap.get(routeShortName)) {
                    results.addSuggestion(resultFactory.getRouteResult(routeBean));
                }
                continue;
            } catch (OutOfServiceAreaServiceException oosase) {
            }
        }
    }
    // long name matching
    for (String routeLongName : _routeLongNameToRouteBeanMap.keySet()) {
        if (routeLongName.contains(routeQuery + " ") || routeLongName.contains(" " + routeQuery)) {
            try {
                for (RouteBean routeBean : _routeLongNameToRouteBeanMap.get(routeLongName)) {
                    results.addSuggestion(resultFactory.getRouteResult(routeBean));
                }
            } catch (OutOfServiceAreaServiceException oosase) {
            }
        }
    }
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) Matcher(java.util.regex.Matcher) OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException)

Example 42 with RouteBean

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

the class SearchServiceImpl method findRoutesStoppingWithinRegion.

@Override
public SearchResultCollection findRoutesStoppingWithinRegion(CoordinateBounds bounds, SearchResultFactory resultFactory) {
    SearchResultCollection results = new SearchResultCollection();
    SearchQueryBean queryBean = new SearchQueryBean();
    queryBean.setType(SearchQueryBean.EQueryType.BOUNDS_OR_CLOSEST);
    queryBean.setBounds(bounds);
    queryBean.setMaxCount(100);
    RoutesBean routes = null;
    try {
        routes = _transitDataService.getRoutes(queryBean);
    } catch (OutOfServiceAreaServiceException e) {
        return results;
    }
    Collections.sort(routes.getRoutes(), new RouteComparator());
    for (RouteBean route : routes.getRoutes()) {
        results.addMatch(resultFactory.getRouteResultForRegion(route));
    }
    return results;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) RoutesBean(org.onebusaway.transit_data.model.RoutesBean) RouteComparator(org.onebusaway.presentation.impl.RouteComparator) SearchResultCollection(org.onebusaway.presentation.model.SearchResultCollection) OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException)

Example 43 with RouteBean

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

the class SearchServiceImpl method refreshCachesIfNecessary.

// we keep an internal cache of route short/long names because if we moved
// this into the
// transit data federation, we'd also have to move the model factory and
// some other agency-specific
// conventions, which wouldn't be pretty.
// 
// long-term FIXME: figure out how to split apart the model creation a bit
// more from the actual
// search process.
public void refreshCachesIfNecessary() {
    String currentBundleId = _transitDataService.getActiveBundleId();
    if ((_bundleIdForCaches != null && _bundleIdForCaches.equals(currentBundleId)) || currentBundleId == null) {
        return;
    }
    _routeShortNameToRouteBeanMap.clear();
    _routeIdToRouteBeanMap.clear();
    _routeLongNameToRouteBeanMap.clear();
    _stopCodeToStopIdMap.clear();
    for (AgencyWithCoverageBean agency : _transitDataService.getAgenciesWithCoverage()) {
        for (RouteBean routeBean : _transitDataService.getRoutesForAgencyId(agency.getAgency().getId()).getList()) {
            if (routeBean.getShortName() != null)
                if (_routeShortNameToRouteBeanMap.containsKey(routeBean.getShortName().toUpperCase())) {
                    _routeShortNameToRouteBeanMap.get(routeBean.getShortName().toUpperCase()).add(routeBean);
                } else {
                    _routeShortNameToRouteBeanMap.put(routeBean.getShortName().toUpperCase(), new ArrayList<RouteBean>(Arrays.asList(routeBean)));
                }
            if (routeBean.getLongName() != null)
                if (_routeLongNameToRouteBeanMap.containsKey(routeBean.getLongName())) {
                    _routeLongNameToRouteBeanMap.get(routeBean.getLongName()).add(routeBean);
                } else {
                    _routeLongNameToRouteBeanMap.put(routeBean.getLongName(), new ArrayList<RouteBean>(Arrays.asList(routeBean)));
                }
            _routeIdToRouteBeanMap.put(routeBean.getId(), routeBean);
        }
        List<StopBean> stopsList = _transitDataService.getAllRevenueStops(agency);
        for (StopBean stop : stopsList) {
            _stopCodeToStopIdMap.put(agency.getAgency().getId() + "_" + stop.getCode().toUpperCase(), stop.getId());
        }
    }
    _bundleIdForCaches = currentBundleId;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) AgencyWithCoverageBean(org.onebusaway.transit_data.model.AgencyWithCoverageBean) ArrayList(java.util.ArrayList) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 44 with RouteBean

use of org.onebusaway.transit_data.model.RouteBean 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 45 with RouteBean

use of org.onebusaway.transit_data.model.RouteBean 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)

Aggregations

RouteBean (org.onebusaway.transit_data.model.RouteBean)63 ArrayList (java.util.ArrayList)35 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)24 StopBean (org.onebusaway.transit_data.model.StopBean)22 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)18 TripBean (org.onebusaway.transit_data.model.trips.TripBean)14 StopGroupBean (org.onebusaway.transit_data.model.StopGroupBean)12 StopGroupingBean (org.onebusaway.transit_data.model.StopGroupingBean)12 List (java.util.List)9 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)7 NameBean (org.onebusaway.transit_data.model.NameBean)7 Date (java.util.Date)6 Test (org.junit.Test)6 AgencyBean (org.onebusaway.transit_data.model.AgencyBean)6 ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)6 IOException (java.io.IOException)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SearchQueryBean (org.onebusaway.transit_data.model.SearchQueryBean)5