use of com.github.davidmoten.rtree2.geometry.Rectangle 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;
});
}
use of com.github.davidmoten.rtree2.geometry.Rectangle 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);
}
use of com.github.davidmoten.rtree2.geometry.Rectangle 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);
}
use of com.github.davidmoten.rtree2.geometry.Rectangle in project rtree2 by davidmoten.
the class RTreeTest method testDepthWithMaxChildren3Entries8.
@Test
public void testDepthWithMaxChildren3Entries8() {
RTree<Object, Rectangle> tree = create(3, 8);
tree.visualize(800, 800).save(new File("target/treeLittle.png"), "PNG");
assertEquals(3, tree.calculateDepth());
}
use of com.github.davidmoten.rtree2.geometry.Rectangle 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);
}
Aggregations