Search in sources :

Example 1 with SearchResult

use of org.onebusaway.presentation.model.SearchResult in project onebusaway-application-modules by camsys.

the class SearchServiceImpl method tryAsLatLon.

private void tryAsLatLon(SearchResultCollection results, String rawQuery, SearchResultFactory resultFactory) {
    List<SearchResult> routesNearby = null;
    Matcher m = latLonPattern.matcher(rawQuery);
    if (m.find()) {
        String latStr = m.group(1) + m.group(2);
        String lonStr = m.group(5) + m.group(6);
        _log.info("parse lat/lon = " + latStr + ", " + lonStr);
        try {
            Double lat = Double.parseDouble(latStr);
            Double lon = Double.parseDouble(lonStr);
            EnterpriseGeocoderResult egr = new SimpleEnterpriseGeocoderResult(lat, lon);
            _log.info("found lat/lon");
            results.addMatch(resultFactory.getGeocoderResult(egr, results.getRouteFilter()));
        } catch (Exception any) {
            _log.info("no results, exception=" + any);
        }
    }
}
Also used : EnterpriseGeocoderResult(org.onebusaway.geocoder.enterprise.services.EnterpriseGeocoderResult) Matcher(java.util.regex.Matcher) SearchResult(org.onebusaway.presentation.model.SearchResult) OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException) NoSuchStopServiceException(org.onebusaway.exceptions.NoSuchStopServiceException)

Example 2 with SearchResult

use of org.onebusaway.presentation.model.SearchResult in project onebusaway-application-modules by camsys.

the class SearchServiceImpl method findRoutesStoppingNearPoint.

@Override
public SearchResultCollection findRoutesStoppingNearPoint(Double latitude, Double longitude, SearchResultFactory resultFactory) {
    CoordinateBounds bounds = SphericalGeometryLibrary.bounds(latitude, longitude, DISTANCE_TO_ROUTES);
    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 RouteDistanceFromPointComparator(latitude, longitude));
    for (RouteBean route : routes.getRoutes()) {
        SearchResult result = resultFactory.getRouteResult(route);
        results.addMatch(result);
        if (results.getMatches().size() > MAX_ROUTES) {
            break;
        }
    }
    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) SearchResultCollection(org.onebusaway.presentation.model.SearchResultCollection) OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException) SearchResult(org.onebusaway.presentation.model.SearchResult) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 3 with SearchResult

use of org.onebusaway.presentation.model.SearchResult in project onebusaway-application-modules by camsys.

the class IndexAction method getUniqueServiceAlertsForResults.

public Set<String> getUniqueServiceAlertsForResults() {
    Set<String> uniqueServiceAlerts = new HashSet<String>();
    for (SearchResult _result : _results.getMatches()) {
        if (_results.getResultType().equals("RouteResult")) {
            RouteResult result = (RouteResult) _result;
            uniqueServiceAlerts.addAll(result.getServiceAlerts());
        } else if (_results.getResultType().equals("StopResult")) {
            StopResult result = (StopResult) _result;
            for (RouteAtStop route : result.getAllRoutesAvailable()) {
                uniqueServiceAlerts.addAll(route.getServiceAlerts());
            }
        }
    }
    return uniqueServiceAlerts;
}
Also used : RouteResult(org.onebusaway.enterprise.webapp.actions.m.model.RouteResult) SearchResult(org.onebusaway.presentation.model.SearchResult) RouteAtStop(org.onebusaway.enterprise.webapp.actions.m.model.RouteAtStop) StopResult(org.onebusaway.enterprise.webapp.actions.m.model.StopResult) HashSet(java.util.HashSet)

Example 4 with SearchResult

use of org.onebusaway.presentation.model.SearchResult in project onebusaway-application-modules by camsys.

the class IndexAction method getRouteColors.

public String getRouteColors() {
    Set<String> routeColors = new HashSet<String>();
    for (SearchResult _result : _results.getMatches()) {
        RouteResult result = (RouteResult) _result;
        routeColors.add(result.getColor());
    }
    return StringUtils.join(routeColors, ",");
}
Also used : RouteResult(org.onebusaway.enterprise.webapp.actions.m.model.RouteResult) SearchResult(org.onebusaway.presentation.model.SearchResult) HashSet(java.util.HashSet)

Example 5 with SearchResult

use of org.onebusaway.presentation.model.SearchResult 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)

Aggregations

SearchResult (org.onebusaway.presentation.model.SearchResult)5 HashSet (java.util.HashSet)2 RouteResult (org.onebusaway.enterprise.webapp.actions.m.model.RouteResult)2 OutOfServiceAreaServiceException (org.onebusaway.exceptions.OutOfServiceAreaServiceException)2 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)2 SearchResultCollection (org.onebusaway.presentation.model.SearchResultCollection)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 SearchQueryBean (org.onebusaway.transit_data.model.SearchQueryBean)2 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 RouteAtStop (org.onebusaway.enterprise.webapp.actions.m.model.RouteAtStop)1 StopResult (org.onebusaway.enterprise.webapp.actions.m.model.StopResult)1 NoSuchStopServiceException (org.onebusaway.exceptions.NoSuchStopServiceException)1 EnterpriseGeocoderResult (org.onebusaway.geocoder.enterprise.services.EnterpriseGeocoderResult)1 RoutesBean (org.onebusaway.transit_data.model.RoutesBean)1 StopBean (org.onebusaway.transit_data.model.StopBean)1