Search in sources :

Example 6 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project urban-eureka by errir503.

the class BenchmarkJtsStrTreeBuild method buildRtree.

@Benchmark
@OperationsPerInvocation(1)
public void buildRtree(BenchmarkData data, Blackhole blackhole) {
    STRtree rtree = new STRtree();
    for (Envelope envelope : data.getBuildEnvelopes()) {
        rtree.insert(envelope, envelope);
    }
    rtree.build();
    blackhole.consume(rtree);
}
Also used : STRtree(org.locationtech.jts.index.strtree.STRtree) Envelope(org.locationtech.jts.geom.Envelope) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 7 with STRtree

use of org.locationtech.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(org.locationtech.jts.geom.Envelope) Random(java.util.Random) Coordinate(org.locationtech.jts.geom.Coordinate) SpatialIndex(org.locationtech.jts.index.SpatialIndex) STRtree(org.locationtech.jts.index.strtree.STRtree) HashSet(java.util.HashSet)

Example 8 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project OpenTripPlanner by opentripplanner.

the class StreetMatcher method createIndex.

STRtree createIndex() {
    STRtree edgeIndex = new STRtree();
    for (Vertex v : graph.getVertices()) {
        for (Edge e : v.getOutgoing()) {
            if (e instanceof StreetEdge) {
                Envelope envelope;
                Geometry geometry = e.getGeometry();
                envelope = geometry.getEnvelopeInternal();
                edgeIndex.insert(envelope, e);
            }
        }
    }
    log.debug("Created index");
    return edgeIndex;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Vertex(org.opentripplanner.routing.graph.Vertex) STRtree(org.locationtech.jts.index.strtree.STRtree) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Envelope(org.locationtech.jts.geom.Envelope) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 9 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class RoutesBeanServiceImpl method setup.

@Refreshable(dependsOn = { RefreshableResources.NARRATIVE_DATA })
@PostConstruct
public void setup() {
    _stopTreesByRouteId.clear();
    for (StopEntry stop : _graphDao.getAllStops()) {
        Set<AgencyAndId> routeIds = _routeService.getRouteCollectionIdsForStop(stop.getId());
        for (AgencyAndId routeId : routeIds) {
            STRtree tree = _stopTreesByRouteId.get(routeId);
            if (tree == null) {
                tree = new STRtree();
                _stopTreesByRouteId.put(routeId, tree);
            }
            double x = stop.getStopLon();
            double y = stop.getStopLat();
            Envelope env = new Envelope(x, x, y, y);
            tree.insert(env, routeId);
        }
    }
    for (STRtree tree : _stopTreesByRouteId.values()) tree.build();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) STRtree(org.locationtech.jts.index.strtree.STRtree) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) Envelope(org.locationtech.jts.geom.Envelope) Refreshable(org.onebusaway.container.refresh.Refreshable) PostConstruct(javax.annotation.PostConstruct)

Example 10 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class TransitGraphImpl method initialize.

public void initialize() {
    if (_stopLocationTree == null) {
        System.out.println("initializing transit graph...");
        if (_stops.size() == 0) {
            _log.warn("no stops found for graph");
        } else {
            _stopLocationTree = new STRtree(_stops.size());
            for (int i = 0; i < _stops.size(); i++) {
                StopEntry stop = _stops.get(i);
                double x = stop.getStopLon();
                double y = stop.getStopLat();
                Envelope r = new Envelope(x, x, y, y);
                _stopLocationTree.insert(r, stop);
            }
            _stopLocationTree.build();
        }
        System.out.println("  stops=" + _stops.size());
        System.out.println("  trips= " + _trips.size());
    }
    if (_agencyEntriesById == null || _agencyEntriesById.size() < _agencies.size()) {
        refreshAgencyMapping();
    }
    if (_tripEntriesById == null || _tripEntriesById.size() < _trips.size()) {
        refreshTripMapping();
    }
    if (_blockEntriesById == null || _blockEntriesById.size() < _blocks.size()) {
        refreshBlockMapping();
    }
    if (_stopEntriesById == null || _stopEntriesById.size() < _stops.size())
        refreshStopMapping();
    if (_routeCollectionEntriesById == null || _routeCollectionEntriesById.size() < _routeCollections.size())
        refreshRouteCollectionMapping();
    if (_routeEntriesById == null || _routeEntriesById.size() < _routes.size())
        refreshRouteMapping();
    int i = 0;
    for (StopEntryImpl stop : _stops) stop.setIndex(i++);
}
Also used : STRtree(org.locationtech.jts.index.strtree.STRtree) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) Envelope(org.locationtech.jts.geom.Envelope)

Aggregations

STRtree (org.locationtech.jts.index.strtree.STRtree)14 Envelope (org.locationtech.jts.geom.Envelope)13 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)3 PostConstruct (javax.annotation.PostConstruct)2 Geometry (org.locationtech.jts.geom.Geometry)2 Refreshable (org.onebusaway.container.refresh.Refreshable)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 OperationsPerInvocation (org.openjdk.jmh.annotations.OperationsPerInvocation)2 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)2 Edge (org.opentripplanner.routing.graph.Edge)2 Vertex (org.opentripplanner.routing.graph.Vertex)2 File (java.io.File)1 HashSet (java.util.HashSet)1 List (java.util.List)1