Search in sources :

Example 36 with CoordinateBounds

use of org.onebusaway.geospatial.model.CoordinateBounds in project onebusaway-application-modules by camsys.

the class BoundingBoxController method index.

@RequestMapping()
public ModelAndView index() {
    GeometryFactory gf = new GeometryFactory();
    List<Polygon> polygons = new ArrayList<Polygon>();
    Map<String, CoordinateBounds> agencies = _agencyService.getAgencyIdsAndCoverageAreas();
    for (CoordinateBounds cb : agencies.values()) {
        Envelope e = new Envelope(cb.getMinLon(), cb.getMaxLon(), cb.getMinLat(), cb.getMaxLat());
        Polygon p = (Polygon) gf.toGeometry(e);
        polygons.add(p);
    }
    MultiPolygon mp = gf.createMultiPolygon(polygons.toArray(new Polygon[0]));
    Geometry hull = mp.convexHull();
    Envelope env = hull.getEnvelopeInternal();
    ModelAndView mv = new ModelAndView("bounding-box.jspx");
    mv.addObject("minY", env.getMinY());
    mv.addObject("minX", env.getMinX());
    mv.addObject("maxY", env.getMaxY());
    mv.addObject("maxX", env.getMaxX());
    mv.addObject("hullWKT", hull.toText());
    return mv;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Envelope(com.vividsolutions.jts.geom.Envelope) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 37 with CoordinateBounds

use of org.onebusaway.geospatial.model.CoordinateBounds in project onebusaway-application-modules by camsys.

the class SimulateScheduledVehicleLocationsController method applyNoiseToLocation.

private CoordinatePoint applyNoiseToLocation(CoordinatePoint location, double noise) {
    CoordinateBounds b = SphericalGeometryLibrary.bounds(location, noise);
    double latSpan = b.getMaxLat() - b.getMinLat();
    double lonSpan = b.getMaxLon() - b.getMinLon();
    double lat = b.getMinLat() + Math.random() * latSpan;
    double lon = b.getMinLon() + Math.random() * lonSpan;
    return new CoordinatePoint(lat, lon);
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 38 with CoordinateBounds

use of org.onebusaway.geospatial.model.CoordinateBounds in project onebusaway-application-modules by camsys.

the class SimulateVehicleLocationsController method applyNoiseToLocation.

private CoordinatePoint applyNoiseToLocation(CoordinatePoint location, double noise) {
    CoordinateBounds b = SphericalGeometryLibrary.bounds(location, noise);
    double latSpan = b.getMaxLat() - b.getMinLat();
    double lonSpan = b.getMaxLon() - b.getMinLon();
    double lat = b.getMinLat() + Math.random() * latSpan;
    double lon = b.getMinLon() + Math.random() * lonSpan;
    return new CoordinatePoint(lat, lon);
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 39 with CoordinateBounds

use of org.onebusaway.geospatial.model.CoordinateBounds 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 40 with CoordinateBounds

use of org.onebusaway.geospatial.model.CoordinateBounds in project onebusaway-application-modules by camsys.

the class DefaultWebappConfigurationSource method getConfiguration.

@Override
public Map<String, Object> getConfiguration(String contextPath) {
    Map<String, Object> config = new HashMap<String, Object>();
    config.put("apiKey", "web");
    config.put("baseUrl", contextPath);
    config.put("apiUrl", contextPath + "/api");
    List<AgencyWithCoverageBean> agenciesWithCoverage = _transitDataService.getAgenciesWithCoverage();
    CoordinateBounds bounds = new CoordinateBounds();
    for (AgencyWithCoverageBean awc : agenciesWithCoverage) {
        if (awc.getLatSpan() <= 0 || awc.getLonSpan() <= 0)
            continue;
        bounds.addPoint(awc.getLat() + awc.getLatSpan() / 2, awc.getLon() + awc.getLonSpan() / 2);
        bounds.addPoint(awc.getLat() - awc.getLatSpan() / 2, awc.getLon() - awc.getLonSpan() / 2);
    }
    if (bounds.isEmpty()) {
        config.put("centerLat", 0.0);
        config.put("centerLon", 0.0);
        config.put("spanLat", 180.0);
        config.put("spanLon", 180.0);
    } else {
        config.put("centerLat", (bounds.getMinLat() + bounds.getMaxLat()) / 2);
        config.put("centerLon", (bounds.getMinLon() + bounds.getMaxLon()) / 2);
        config.put("spanLat", bounds.getMaxLat() - bounds.getMinLat());
        config.put("spanLon", bounds.getMaxLon() - bounds.getMinLon());
    }
    config.put("hasDefaultServiceArea", _serviceAreaService.hasDefaultServiceArea());
    config.put("googleMapsApiKey", _googleMapsApiKey);
    config.put("tracker", _googleAnalyticsAnalyticsKey);
    return config;
}
Also used : HashMap(java.util.HashMap) AgencyWithCoverageBean(org.onebusaway.transit_data.model.AgencyWithCoverageBean) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Aggregations

CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)70 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)16 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 List (java.util.List)14 SearchQueryBean (org.onebusaway.transit_data.model.SearchQueryBean)12 Map (java.util.Map)9 StopBean (org.onebusaway.transit_data.model.StopBean)9 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 RouteBean (org.onebusaway.transit_data.model.RouteBean)7 StopsBean (org.onebusaway.transit_data.model.StopsBean)7 Envelope (com.vividsolutions.jts.geom.Envelope)5 OutOfServiceAreaServiceException (org.onebusaway.exceptions.OutOfServiceAreaServiceException)5 STRtree (com.vividsolutions.jts.index.strtree.STRtree)4 File (java.io.File)4 IOException (java.io.IOException)4 Filters (org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters)4 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)4 RoutesBean (org.onebusaway.transit_data.model.RoutesBean)4