Search in sources :

Example 1 with Point

use of com.github.davidmoten.rtree2.geometry.Point 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 Point

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

the class RTreeTest method testForMeiZhao.

@Test
public void testForMeiZhao() {
    for (int minChildren = 1; minChildren <= 2; minChildren++) {
        RTree<Integer, Point> tree = RTree.maxChildren(3).minChildren(minChildren).<Integer, Point>create().add(1, point(1, 9)).add(2, point(2, 10)).add(3, point(4, 8)).add(4, point(6, 7)).add(5, point(9, 10)).add(6, point(7, 5)).add(7, point(5, 6)).add(8, point(4, 3)).add(9, point(3, 2)).add(10, point(9, 1)).add(11, point(10, 4)).add(12, point(6, 2)).add(13, point(8, 3));
        System.out.println(tree.asString());
    }
}
Also used : Point(com.github.davidmoten.rtree2.geometry.Point) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) Test(org.junit.Test)

Example 3 with Point

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

the class RTreeTest method testVisualizerWithGreekData.

@Test
public void testVisualizerWithGreekData() {
    List<Entry<Object, Point>> entries = GreekEarthquakes.entriesList(Precision.DOUBLE);
    int maxChildren = 8;
    RTree<Object, Point> tree = RTree.maxChildren(maxChildren).<Object, Point>create().add(entries);
    tree.visualize(2000, 2000).save("target/greek.png");
    // do search
    long found = Iterables.size(tree.search(Geometries.rectangle(40, 27.0, 40.5, 27.5)));
    System.out.println("found=" + found);
    assertEquals(22, found);
    RTree<Object, Point> tree2 = RTree.maxChildren(maxChildren).star().<Object, Point>create().add(entries);
    tree2.visualize(2000, 2000).save("target/greek2.png");
    RTree<Object, Point> tree3 = RTree.maxChildren(maxChildren).create(entries);
    tree3.visualize(2000, 2000).save("target/greek3.png");
}
Also used : Point(com.github.davidmoten.rtree2.geometry.Point) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) Test(org.junit.Test)

Example 4 with Point

use of com.github.davidmoten.rtree2.geometry.Point 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 5 with Point

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

the class BenchmarksRTree method main.

public static void main(String[] args) {
    RTree<Object, Point> tree = RTree.maxChildren(28).star().<Object, Point>create(entries);
    Rectangle r = searchRectangle();
    if (longRun) {
        while (true) {
            if (Iterables.size(tree.search(r)) == 0) {
                System.out.println("unexpected");
            }
        }
    } else {
        long t = System.currentTimeMillis();
        long warmupTimeSeconds = 10;
        long benchmarkTimeSeconds = 10;
        long t2 = -1;
        long count = 0;
        while (true) {
            if (Iterables.size(tree.search(r)) == 0) {
                System.out.println("zero!!");
            }
            ;
            count++;
            if (count % 10000 == 0) {
                if (t2 == -1) {
                    if (System.currentTimeMillis() - t > TimeUnit.SECONDS.toMillis(warmupTimeSeconds)) {
                        t2 = System.currentTimeMillis();
                    }
                } else if (System.currentTimeMillis() - t2 > TimeUnit.SECONDS.toMillis(benchmarkTimeSeconds)) {
                    break;
                }
            }
        }
        double ratePerSecond = count * 1000.0 / (System.currentTimeMillis() - t2);
        DecimalFormat df = new DecimalFormat("0.000");
        System.out.println("ratePerSecond=" + df.format(ratePerSecond / 1000000.0) + "m");
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Point(com.github.davidmoten.rtree2.geometry.Point)

Aggregations

Point (com.github.davidmoten.rtree2.geometry.Point)11 Test (org.junit.Test)7 Geometries.point (com.github.davidmoten.rtree2.geometry.Geometries.point)6 Rectangle (com.github.davidmoten.rtree2.geometry.Rectangle)6 Geometry (com.github.davidmoten.rtree2.geometry.Geometry)4 HasGeometry (com.github.davidmoten.rtree2.geometry.HasGeometry)4 HashSet (java.util.HashSet)3 Position (com.github.davidmoten.grumpy.core.Position)2 ArrayList (java.util.ArrayList)2 Geometries (com.github.davidmoten.rtree2.geometry.Geometries)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 DecimalFormat (java.text.DecimalFormat)1 List (java.util.List)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Stream (org.davidmoten.kool.Stream)1