Search in sources :

Example 6 with Point

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

the class GalleryMain method main.

public static void main(String[] args) {
    Stream<Entry<Object, Point>> entries = GreekEarthquakes.entries(Precision.DOUBLE).cache();
    List<Integer> sizes = Arrays.asList(100, 1000, 10000, 1000000);
    List<Integer> maxChildrenValues = Arrays.asList(4, 8, 16, 32, 64, 128);
    for (int size : sizes) for (int maxChildren : maxChildrenValues) {
        if (size > maxChildren) {
            System.out.println("saving " + size + " m=" + maxChildren);
            RTree<Object, Point> tree = RTree.maxChildren(maxChildren).<Object, Point>create().add(entries.take(size));
            tree.visualize(600, 600).save("target/greek-" + size + "-" + maxChildren + "-quad.png");
            RTree<Object, Point> tree2 = RTree.star().maxChildren(maxChildren).<Object, Point>create().add(entries.take(size));
            tree2.visualize(600, 600).save("target/greek-" + size + "-" + maxChildren + "-star.png");
        }
    }
}
Also used : Point(com.github.davidmoten.rtree2.geometry.Point) Point(com.github.davidmoten.rtree2.geometry.Point)

Example 7 with Point

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

the class GreekEarthquakes method entries.

public static Stream<Entry<Object, Point>> entries(final Precision precision) {
    return Stream.using(() -> new GZIPInputStream(GreekEarthquakes.class.getResourceAsStream("/greek-earthquakes-1964-2000.txt.gz")), in -> Stream.lines(new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)))).flatMap(line -> {
        if (line.trim().length() > 0) {
            String[] items = line.split(" ");
            double lat = Double.parseDouble(items[0]);
            double lon = Double.parseDouble(items[1]);
            Entry<Object, Point> entry;
            if (precision == Precision.DOUBLE)
                entry = Entries.entry(new Object(), Geometries.point(lat, lon));
            else
                entry = Entries.entry(new Object(), Geometries.point((float) lat, (float) lon));
            return Stream.of(entry);
        } else
            return Stream.empty();
    });
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) List(java.util.List) GZIPInputStream(java.util.zip.GZIPInputStream) Geometries(com.github.davidmoten.rtree2.geometry.Geometries) BufferedReader(java.io.BufferedReader) Stream(org.davidmoten.kool.Stream) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Point(com.github.davidmoten.rtree2.geometry.Point) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Point(com.github.davidmoten.rtree2.geometry.Point)

Example 8 with Point

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

the class LatLongExampleTest method search.

public static <T> Iterable<Entry<T, Point>> search(RTree<T, Point> tree, Point lonLat, final double distanceKm) {
    // First we need to calculate an enclosing lat long rectangle for this
    // distance then we refine on the exact distance
    final Position from = Position.create(lonLat.y(), lonLat.x());
    Rectangle bounds = createBounds(from, distanceKm);
    return Iterables.filter(tree.search(bounds), // refine using the exact distance
    entry -> {
        Point p = entry.geometry();
        Position position = Position.create(p.y(), p.x());
        return from.getDistanceToKm(position) < distanceKm;
    });
}
Also used : Position(com.github.davidmoten.grumpy.core.Position) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Point(com.github.davidmoten.rtree2.geometry.Point)

Example 9 with Point

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

the class RTreeTest method testStandardRTreeSearch.

@Test
public void testStandardRTreeSearch() {
    Rectangle r = rectangle(13.0, 23.0, 50.0, 80.0);
    Point[] points = { point(59.0, 91.0), point(86.0, 14.0), point(36.0, 60.0), point(57.0, 36.0), point(14.0, 37.0) };
    RTree<Integer, Geometry> tree = RTree.create();
    for (int i = 0; i < points.length; i++) {
        Point point = points[i];
        System.out.println("point(" + point.x() + "," + point.y() + "), value=" + (i + 1));
        tree = tree.add(i + 1, point);
    }
    System.out.println(tree.asString());
    System.out.println("searching " + r);
    Set<Integer> set = new HashSet<Integer>(Observable.from(tree.search(r)).map(RTreeTest.<Integer>toValue()).toList().toBlocking().single());
    assertEquals(new HashSet<Integer>(asList(3, 5)), set);
}
Also used : Geometry(com.github.davidmoten.rtree2.geometry.Geometry) HasGeometry(com.github.davidmoten.rtree2.geometry.HasGeometry) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Point(com.github.davidmoten.rtree2.geometry.Point) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Point

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

the class RTreeTest method testStandardRTreeSearch2.

@Test
public void testStandardRTreeSearch2() {
    Rectangle r = rectangle(10.0, 10.0, 50.0, 50.0);
    Point[] points = { point(28.0, 19.0), point(29.0, 4.0), point(10.0, 63.0), point(34.0, 85.0), point(62.0, 45.0) };
    RTree<Integer, Geometry> tree = RTree.create();
    for (int i = 0; i < points.length; i++) {
        Point point = points[i];
        System.out.println("point(" + point.x() + "," + point.y() + "), value=" + (i + 1));
        tree = tree.add(i + 1, point);
    }
    System.out.println(tree.asString());
    System.out.println("searching " + r);
    Set<Integer> set = new HashSet<Integer>(Observable.from(tree.search(r)).map(RTreeTest.<Integer>toValue()).toList().toBlocking().single());
    assertEquals(new HashSet<Integer>(asList(1)), set);
}
Also used : Geometry(com.github.davidmoten.rtree2.geometry.Geometry) HasGeometry(com.github.davidmoten.rtree2.geometry.HasGeometry) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) Point(com.github.davidmoten.rtree2.geometry.Point) Geometries.point(com.github.davidmoten.rtree2.geometry.Geometries.point) Point(com.github.davidmoten.rtree2.geometry.Point) HashSet(java.util.HashSet) Test(org.junit.Test)

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