Search in sources :

Example 6 with StopsBean

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

the class StopsAction method execute.

@Override
@Actions({ @Action(value = "/where/iphone/stops") })
public String execute() throws ServiceException {
    CoordinateBounds bounds = getServiceArea();
    if (bounds == null) {
        pushNextAction("stops", "code", _code);
        return "query-default-search-location";
    }
    SearchQueryBean searchQuery = new SearchQueryBean();
    searchQuery.setBounds(bounds);
    searchQuery.setMaxCount(5);
    searchQuery.setType(EQueryType.BOUNDS_OR_CLOSEST);
    searchQuery.setQuery(_code);
    StopsBean stopsResult = _transitDataService.getStops(searchQuery);
    _stops = stopsResult.getStops();
    if (_stops.size() == 0) {
        return "noStopsFound";
    } else if (_stops.size() > 1) {
        return "multipleStopsFound";
    } else {
        _stop = _stops.get(0);
        return "singleStopFound";
    }
}
Also used : SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) StopsBean(org.onebusaway.transit_data.model.StopsBean) Actions(org.apache.struts2.convention.annotation.Actions)

Example 7 with StopsBean

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

the class SearchServiceImpl method tryAsStopName.

// use LUCENE index to search on stop name
private void tryAsStopName(SearchResultCollection results, String q, SearchResultFactory resultFactory) {
    StopsBean beans = _transitDataService.getStopsByName(q);
    int count = 0;
    if (beans == null || beans.getStops() == null)
        return;
    for (StopBean stopBean : beans.getStops()) {
        String agencyId = AgencyAndIdLibrary.convertFromString(stopBean.getId()).getAgencyId();
        // filter out stops not in service
        if (_transitDataService.stopHasRevenueService(agencyId, stopBean.getId())) {
            // this is a fuzzy match so just a suggestion
            results.addSuggestion(resultFactory.getStopResult(stopBean, results.getRouteFilter()));
            count++;
        }
        if (count > MAX_STOPS) {
            break;
        }
    }
    return;
}
Also used : StopBean(org.onebusaway.transit_data.model.StopBean) StopsBean(org.onebusaway.transit_data.model.StopsBean)

Example 8 with StopsBean

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

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

the class StopsForLocationAction method index.

public DefaultHttpHeaders index() throws IOException, ServiceException {
    int maxCount = _maxCount.getMaxCount();
    if (maxCount <= 0)
        addFieldError("maxCount", "must be greater than zero");
    if (hasErrors())
        return setValidationErrorsResponse();
    CoordinateBounds bounds = getSearchBounds();
    SearchQueryBean searchQuery = new SearchQueryBean();
    searchQuery.setBounds(bounds);
    searchQuery.setMaxCount(maxCount);
    searchQuery.setType(EQueryType.BOUNDS);
    if (_query != null) {
        searchQuery.setQuery(_query);
        searchQuery.setType(EQueryType.BOUNDS_OR_CLOSEST);
    }
    try {
        StopsBean result = _service.getStops(searchQuery);
        return transformResult(result);
    } catch (OutOfServiceAreaServiceException ex) {
        return transformOutOfRangeResult();
    }
}
Also used : SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) StopsBean(org.onebusaway.transit_data.model.StopsBean)

Example 10 with StopsBean

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

the class StopByNumberAction method execute.

@Override
public String execute() throws ServiceException {
    if (_text != null)
        _text.trim();
    if (_text == null || _text.length() == 0)
        return INPUT;
    String[] tokens = _text.trim().split("\\s+");
    if (tokens.length == 0)
        return INPUT;
    CoordinateBounds serviceArea = _serviceAreaService.getServiceArea();
    if (serviceArea == null) {
        pushNextAction("stop-by-number", "text", _text);
        return "query-default-search-location";
    }
    _stopQuery = tokens[0];
    SearchQueryBean searchQuery = new SearchQueryBean();
    searchQuery.setBounds(serviceArea);
    searchQuery.setMaxCount(5);
    searchQuery.setType(EQueryType.BOUNDS_OR_CLOSEST);
    searchQuery.setQuery(_stopQuery);
    StopsBean results = _transitDataService.getStops(searchQuery);
    _stops = results.getStops();
    int stopIndex = 0;
    if (_stops.isEmpty()) {
        return "noStopsFound";
    } else if (_stops.size() > 1) {
        if (0 <= _selectedIndex && _selectedIndex < _stops.size()) {
            stopIndex = _selectedIndex;
        } else {
            pushNextAction("stop-by-number", "text", _text);
            pushNextAction("handle-multi-selection");
            return "multipleStopsFound";
        }
    }
    StopBean stop = _stops.get(stopIndex);
    _stopId = stop.getId();
    _args = new String[tokens.length - 1];
    System.arraycopy(tokens, 1, _args, 0, _args.length);
    return "arrivals-and-departures";
}
Also used : SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) StopBean(org.onebusaway.transit_data.model.StopBean) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) StopsBean(org.onebusaway.transit_data.model.StopsBean)

Aggregations

StopsBean (org.onebusaway.transit_data.model.StopsBean)12 SearchQueryBean (org.onebusaway.transit_data.model.SearchQueryBean)9 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)7 StopBean (org.onebusaway.transit_data.model.StopBean)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)2 List (java.util.List)2 OutOfServiceAreaServiceException (org.onebusaway.exceptions.OutOfServiceAreaServiceException)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 ParseException (org.apache.lucene.queryParser.ParseException)1 Actions (org.apache.struts2.convention.annotation.Actions)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Filters (org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters)1 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)1 StopOnRoute (org.onebusaway.enterprise.webapp.actions.api.model.StopOnRoute)1 InvalidArgumentServiceException (org.onebusaway.exceptions.InvalidArgumentServiceException)1