Search in sources :

Example 1 with STRtree

use of com.vividsolutions.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class BlockGeospatialServiceImpl method buildShapeSpatialIndex.

private void buildShapeSpatialIndex() throws IOException, ClassNotFoundException {
    File path = _bundle.getShapeGeospatialIndexDataPath();
    if (!path.exists()) {
        _tree = null;
        return;
    }
    _log.info("loading shape point geospatial index...");
    Map<CoordinateBounds, List<AgencyAndId>> shapeIdsByGridCell = ObjectSerializationLibrary.readObject(path);
    _log.info("block shape geospatial nodes: " + shapeIdsByGridCell.size());
    if (shapeIdsByGridCell.isEmpty()) {
        _tree = null;
        return;
    }
    _tree = new STRtree(shapeIdsByGridCell.size());
    for (Map.Entry<CoordinateBounds, List<AgencyAndId>> entry : shapeIdsByGridCell.entrySet()) {
        CoordinateBounds b = entry.getKey();
        Envelope env = new Envelope(b.getMinLon(), b.getMaxLon(), b.getMinLat(), b.getMaxLat());
        List<AgencyAndId> shapeIds = entry.getValue();
        _tree.insert(env, shapeIds);
    }
    _tree.build();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) STRtree(com.vividsolutions.jts.index.strtree.STRtree) List(java.util.List) ArrayList(java.util.ArrayList) Envelope(com.vividsolutions.jts.geom.Envelope) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 2 with STRtree

use of com.vividsolutions.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class RoutesBeanServiceImpl method getRoutesWithRouteNameQuery.

private RoutesBean getRoutesWithRouteNameQuery(SearchQueryBean query) throws ServiceException {
    SearchResult<AgencyAndId> result = searchForRoutes(query);
    List<RouteBean> routeBeans = new ArrayList<RouteBean>();
    CoordinateBounds bounds = query.getBounds();
    for (AgencyAndId id : result.getResults()) {
        STRtree tree = _stopTreesByRouteId.get(id);
        if (tree == null) {
            _log.warn("stop tree not found for routeId=" + id);
            continue;
        }
        Envelope env = new Envelope(bounds.getMinLon(), bounds.getMaxLon(), bounds.getMinLat(), bounds.getMaxLat());
        HasItemsVisitor v = new HasItemsVisitor();
        tree.query(env, v);
        if (v.hasItems()) {
            RouteBean routeBean = _routeBeanService.getRouteForId(id);
            routeBeans.add(routeBean);
        }
    }
    boolean limitExceeded = BeanServiceSupport.checkLimitExceeded(routeBeans, query.getMaxCount());
    return constructResult(routeBeans, limitExceeded);
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) STRtree(com.vividsolutions.jts.index.strtree.STRtree) Envelope(com.vividsolutions.jts.geom.Envelope) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 3 with STRtree

use of com.vividsolutions.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class HierarchicalSTRtreeFactory method add.

public void add(double lat, double lon, T element) {
    STRtree tree = null;
    for (Map.Entry<CoordinateBounds, STRtree> entry : _treesByBounds.entrySet()) {
        CoordinateBounds bounds = entry.getKey();
        if (bounds.contains(lat, lon)) {
            tree = entry.getValue();
            break;
        }
    }
    if (tree == null) {
        double gLat = Math.floor(lat / _latStep) * _latStep;
        double gLon = Math.floor(lon / _lonStep) * _lonStep;
        CoordinateBounds b = new CoordinateBounds(gLat, gLon, gLat + _latStep, gLon + _lonStep);
        tree = new STRtree();
        _treesByBounds.put(b, tree);
    }
    Envelope env = new Envelope(lon, lon, lat, lat);
    tree.insert(env, element);
}
Also used : STRtree(com.vividsolutions.jts.index.strtree.STRtree) Envelope(com.vividsolutions.jts.geom.Envelope) Map(java.util.Map) HashMap(java.util.HashMap) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 4 with STRtree

use of com.vividsolutions.jts.index.strtree.STRtree in project OpenTripPlanner by opentripplanner.

the class ShowGraph method buildSpatialIndex.

/*
     * Iterate through all vertices and their (outgoing) edges. If they are of 'interesting' types, 
     * add them to the corresponding spatial index.
     */
public synchronized void buildSpatialIndex() {
    vertexIndex = new STRtree();
    edgeIndex = new STRtree();
    Envelope env;
    // int xminx, xmax, ymin, ymax;
    for (Vertex v : graph.getVertices()) {
        Coordinate c = v.getCoordinate();
        env = new Envelope(c);
        vertexIndex.insert(env, v);
        for (Edge e : v.getOutgoing()) {
            if (e.getGeometry() == null)
                continue;
            if (e instanceof PatternEdge || e instanceof StreetTransitLink || e instanceof StreetEdge || e instanceof PathwayEdge || e instanceof SimpleTransfer) {
                env = e.getGeometry().getEnvelopeInternal();
                edgeIndex.insert(env, e);
            }
        }
    }
    vertexIndex.build();
    edgeIndex.build();
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) Coordinate(com.vividsolutions.jts.geom.Coordinate) STRtree(com.vividsolutions.jts.index.strtree.STRtree) StreetTransitLink(org.opentripplanner.routing.edgetype.StreetTransitLink) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) PathwayEdge(org.opentripplanner.routing.edgetype.PathwayEdge) SimpleTransfer(org.opentripplanner.routing.edgetype.SimpleTransfer) Envelope(com.vividsolutions.jts.geom.Envelope) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) PathwayEdge(org.opentripplanner.routing.edgetype.PathwayEdge) PatternEdge(org.opentripplanner.routing.edgetype.PatternEdge) Edge(org.opentripplanner.routing.graph.Edge) PatternEdge(org.opentripplanner.routing.edgetype.PatternEdge)

Example 5 with STRtree

use of com.vividsolutions.jts.index.strtree.STRtree in project OpenTripPlanner by opentripplanner.

the class HashGridTest method testHashGridRandom.

/**
 * We perform a non-regression random test. We insert many random-envelop objects
 * into both a hash grid (OTP) and STRtree (JTS) spatial indexes. We check with
 * many random query that the set of returned objects is the same (after pruning
 * because both could return false positives).
 */
@SuppressWarnings("unchecked")
public void testHashGridRandom() {
    final double X0 = -0.05;
    final double Y0 = 44.0;
    final double DX = 0.1;
    final double DY = 0.1;
    final int N_OBJS = 1000;
    final int N_QUERIES = 1000;
    Random rand = new Random(42);
    SpatialIndex hashGrid = new HashGridSpatialIndex<>();
    SpatialIndex strTree = new STRtree();
    for (int i = 0; i < N_OBJS; i++) {
        Coordinate a = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
        Coordinate b = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
        DummyObject obj = new DummyObject();
        obj.envelope = new Envelope(a, b);
        hashGrid.insert(obj.envelope, obj);
        strTree.insert(obj.envelope, obj);
    }
    for (int i = 0; i < N_QUERIES; i++) {
        Coordinate a = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
        Coordinate b = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
        Envelope searchEnv = new Envelope(a, b);
        List<DummyObject> hashGridObjs = hashGrid.query(searchEnv);
        // Need to remove non intersecting
        Set<DummyObject> hashGridObjs2 = new HashSet<>();
        for (DummyObject obj : hashGridObjs) {
            if (obj.envelope.intersects(searchEnv))
                hashGridObjs2.add(obj);
        }
        List<DummyObject> strtreeObjs = hashGrid.query(searchEnv);
        // Need to remove non intersecting
        Set<DummyObject> strtreeObjs2 = new HashSet<>();
        for (DummyObject obj : strtreeObjs) {
            if (obj.envelope.intersects(searchEnv))
                strtreeObjs2.add(obj);
        }
        boolean equals = hashGridObjs2.equals(strtreeObjs2);
        assertTrue(equals);
    }
}
Also used : Envelope(com.vividsolutions.jts.geom.Envelope) Random(java.util.Random) Coordinate(com.vividsolutions.jts.geom.Coordinate) SpatialIndex(com.vividsolutions.jts.index.SpatialIndex) STRtree(com.vividsolutions.jts.index.strtree.STRtree) HashSet(java.util.HashSet)

Aggregations

Envelope (com.vividsolutions.jts.geom.Envelope)11 STRtree (com.vividsolutions.jts.index.strtree.STRtree)11 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 PostConstruct (javax.annotation.PostConstruct)2 Refreshable (org.onebusaway.container.refresh.Refreshable)2 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)2 Edge (org.opentripplanner.routing.graph.Edge)2 Vertex (org.opentripplanner.routing.graph.Vertex)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 SpatialIndex (com.vividsolutions.jts.index.SpatialIndex)1 File (java.io.File)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1