use of org.onebusaway.presentation.model.SearchResultCollection 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;
}
use of org.onebusaway.presentation.model.SearchResultCollection in project onebusaway-application-modules by camsys.
the class SearchServiceImpl method getSearchResults.
@Override
public SearchResultCollection getSearchResults(String query, SearchResultFactory resultFactory) {
refreshCachesIfNecessary();
/*
* This method now makes a series of assumptions!
* - using a ',' means our query is a lat/lon or a mailing address
* - using a ';' means you are searching for multiple routes
* - entering 3 tokens means you are not looking for routes
*
* Combined with the above, this is the search order
* 1) lat/lon (if its matches regex)
* 2) route (if no comma, tokens < 2
* 3) routes (has semicolon)
* 4) stop (if no comma, numeric query, query contains '_')
* 5) stop name (no comma)
* 6) geocode
*/
SearchResultCollection results = new SearchResultCollection();
boolean hasComma = query.indexOf(',') > 0;
boolean hasSemiColon = query.indexOf(';') > 0;
tryAsLatLon(results, query, resultFactory);
String normalizedQuery = normalizeQuery(results, query);
int normalizedTokens = normalizedQuery.length() - normalizedQuery.replaceAll(" ", "").length() + 1;
// if we have a comma, we are not a single route
if (results.isEmpty() && !hasComma) {
tryAsRoute(results, normalizedQuery, resultFactory);
}
if (results.isEmpty() && hasSemiColon) {
tryAsRoutes(results, normalizedQuery, resultFactory);
}
// results does not support mixed types -- it can only be a route or a stop
if (results.isEmpty() && !hasComma && (StringUtils.isNumeric(normalizedQuery) || normalizedQuery.contains("_"))) {
tryAsStop(results, normalizedQuery, resultFactory);
}
if (results.isEmpty() && !hasComma) {
tryAsStopName(results, query, resultFactory);
}
if (results.isEmpty()) {
tryAsGeocode(results, query, resultFactory);
}
return results;
}
use of org.onebusaway.presentation.model.SearchResultCollection 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;
}
use of org.onebusaway.presentation.model.SearchResultCollection 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;
}
Aggregations