Search in sources :

Example 1 with SpatialIndex

use of com.vividsolutions.jts.index.SpatialIndex 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

Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 SpatialIndex (com.vividsolutions.jts.index.SpatialIndex)1 STRtree (com.vividsolutions.jts.index.strtree.STRtree)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1