Search in sources :

Example 1 with Geometry

use of com.github.davidmoten.rtree2.geometry.Geometry in project rtree2 by davidmoten.

the class LatLongExampleTest method testSearchLatLongCircles.

@Test
public void testSearchLatLongCircles() {
    RTree<GeoCircleValue<String>, Rectangle> tree = RTree.star().create();
    // create circles around these major towns
    GeoCircleValue<String> sydneyCircle = createGeoCircleValue(sydney, 100, "Sydney");
    GeoCircleValue<String> canberraCircle = createGeoCircleValue(canberra, 50, "Canberra");
    GeoCircleValue<String> brisbaneCircle = createGeoCircleValue(brisbane, 200, "Brisbane");
    // add the circles to the RTree using the bounding box of the circle as
    // the geometry
    tree = add(tree, sydneyCircle);
    tree = add(tree, canberraCircle);
    tree = add(tree, brisbaneCircle);
    // now find the circles that contain bungendore (which is 30km from
    // Canberra)
    final Point location = bungendore;
    String result = Observable.from(tree.search(location)).filter(new Func1<Entry<GeoCircleValue<String>, Rectangle>, Boolean>() {

        Position from = Position.create(location.y(), location.x());

        @Override
        public Boolean call(Entry<GeoCircleValue<String>, Rectangle> entry) {
            Position centre = Position.create(entry.value().lat, entry.value().lon);
            return from.getDistanceToKm(centre) < entry.value().radiusKm;
        }
    }).toBlocking().single().value().value;
    assertEquals("Canberra", result);
}
Also used : Position(com.github.davidmoten.grumpy.core.Position) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Point(com.github.davidmoten.rtree2.geometry.Point) Test(org.junit.Test)

Example 2 with Geometry

use of com.github.davidmoten.rtree2.geometry.Geometry in project rtree2 by davidmoten.

the class RTreeTest method testDeleteOneFromLargeTreeThenDeleteAllAndEnsureEmpty.

@Test
public void testDeleteOneFromLargeTreeThenDeleteAllAndEnsureEmpty() {
    int n = 10000;
    RTree<Object, Geometry> tree = createRandomRTree(n).add(e(1)).add(e(2)).delete(e(1));
    assertEquals(n + 1, Iterables.size(tree.entries()));
    assertFalse(Observable.from(tree.entries()).contains(e(1)).toBlocking().single());
    assertTrue(Observable.from(tree.entries()).contains(e(2)).toBlocking().single());
    n++;
    assertEquals(n, tree.size());
    for (Entry<Object, Geometry> entry : tree.entries()) {
        tree = tree.delete(entry);
        n--;
        assertEquals(n, tree.size());
    }
    assertTrue(Iterables.isEmpty(tree.entries()));
    assertTrue(tree.isEmpty());
}
Also used : Geometry(com.github.davidmoten.rtree2.geometry.Geometry) HasGeometry(com.github.davidmoten.rtree2.geometry.HasGeometry) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) Test(org.junit.Test)

Example 3 with Geometry

use of com.github.davidmoten.rtree2.geometry.Geometry in project rtree2 by davidmoten.

the class RTreeTest method testBulkLoadingTreeAndStarTreeReturnsSameAsStandardRTree.

@Test
public void testBulkLoadingTreeAndStarTreeReturnsSameAsStandardRTree() {
    RTree<Integer, Geometry> tree1 = RTree.create();
    RTree<Integer, Geometry> tree2 = RTree.star().create();
    Rectangle[] testRects = { rectangle(0, 0, 0, 0), rectangle(0, 0, 100, 100), rectangle(0, 0, 10, 10), rectangle(0.12, 0.25, 50.356, 50.756), rectangle(1, 0.252, 50, 69.23), rectangle(13.12, 23.123, 50.45, 80.9), rectangle(10, 10, 50, 50) };
    List<Entry<Integer, Geometry>> entries = new ArrayList<Entry<Integer, Geometry>>(10000);
    for (int i = 1; i <= 10000; i++) {
        Point point = nextPoint();
        // System.out.println("point(" + point.x() + "," + point.y() +
        // "),");
        tree1 = tree1.add(i, point);
        tree2 = tree2.add(i, point);
        entries.add(new EntryDefault<Integer, Geometry>(i, point));
    }
    RTree<Integer, Geometry> tree3 = RTree.create(entries);
    for (Rectangle r : testRects) {
        Set<Integer> res1 = new HashSet<Integer>(Observable.from(tree1.search(r)).map(RTreeTest.<Integer>toValue()).toList().toBlocking().single());
        Set<Integer> res2 = new HashSet<Integer>(Observable.from(tree2.search(r)).map(RTreeTest.<Integer>toValue()).toList().toBlocking().single());
        Set<Integer> res3 = new HashSet<Integer>(Observable.from(tree3.search(r)).map(RTreeTest.<Integer>toValue()).toList().toBlocking().single());
        System.out.println("searchRect= rectangle(" + r.x1() + "," + r.y1() + "," + r.x2() + "," + r.y2() + ")");
        System.out.println("res1.size=" + res1.size() + ",res2.size=" + res2.size() + ",res3.size=" + res3.size());
        // System.out.println("res1=" + res1 + ",res2=" + res2 + ",res3=" + res3);
        assertEquals(res1.size(), res2.size());
        assertEquals(res1.size(), res3.size());
    }
}
Also used : Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) ArrayList(java.util.ArrayList) Point(com.github.davidmoten.rtree2.geometry.Point) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) Geometry(com.github.davidmoten.rtree2.geometry.Geometry) HasGeometry(com.github.davidmoten.rtree2.geometry.HasGeometry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Geometry

use of com.github.davidmoten.rtree2.geometry.Geometry in project rtree2 by davidmoten.

the class SplitterQuadratic method assignRemaining.

private <T extends HasGeometry> void assignRemaining(final List<T> group1, final List<T> group2, final List<T> remaining, final int minGroupSize) {
    final Rectangle mbr1 = Util.mbr(group1);
    final Rectangle mbr2 = Util.mbr(group2);
    final T item1 = getBestCandidateForGroup(remaining, group1, mbr1);
    final T item2 = getBestCandidateForGroup(remaining, group2, mbr2);
    final boolean area1LessThanArea2 = item1.geometry().mbr().add(mbr1).area() <= item2.geometry().mbr().add(mbr2).area();
    if (area1LessThanArea2 && (group2.size() + remaining.size() - 1 >= minGroupSize) || !area1LessThanArea2 && (group1.size() + remaining.size() == minGroupSize)) {
        group1.add(item1);
        remaining.remove(item1);
    } else {
        group2.add(item2);
        remaining.remove(item2);
    }
}
Also used : Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle)

Example 5 with Geometry

use of com.github.davidmoten.rtree2.geometry.Geometry in project rtree2 by davidmoten.

the class LeafTest method testMbr.

@Test
public void testMbr() {
    Rectangle r1 = Geometries.rectangle(0d, 1, 3, 5);
    Rectangle r2 = Geometries.rectangle(1d, 2, 4, 6);
    Rectangle r = new LeafDefault<Object, Rectangle>(Arrays.asList(Entries.entry(new Object(), r1), Entries.entry(new Object(), r2)), context).geometry().mbr();
    assertEquals(r1.add(r2), r);
}
Also used : LeafDefault(com.github.davidmoten.rtree2.internal.LeafDefault) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 Point (com.github.davidmoten.rtree2.geometry.Point)7 Geometries.point (com.github.davidmoten.rtree2.geometry.Geometries.point)6 Geometry (com.github.davidmoten.rtree2.geometry.Geometry)6 HasGeometry (com.github.davidmoten.rtree2.geometry.HasGeometry)6 Rectangle (com.github.davidmoten.rtree2.geometry.Rectangle)6 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 Position (com.github.davidmoten.grumpy.core.Position)1 LeafDefault (com.github.davidmoten.rtree2.internal.LeafDefault)1