use of com.github.davidmoten.rtree2.geometry.Rectangle 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);
}
use of com.github.davidmoten.rtree2.geometry.Rectangle 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());
}
}
use of com.github.davidmoten.rtree2.geometry.Rectangle 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);
}
}
use of com.github.davidmoten.rtree2.geometry.Rectangle in project rtree2 by davidmoten.
the class Visualizer method drawNode.
private void drawNode(Graphics2D g, List<RectangleDepth> nodes) {
for (final RectangleDepth node : nodes) {
final Color color = Color.getHSBColor(node.getDepth() / (maxDepth + 1f), 1f, 1f);
g.setStroke(new BasicStroke(Math.max(0.5f, maxDepth - node.getDepth() + 1 - 1)));
g.setColor(color);
final Rectangle r = node.getRectangle();
drawRectangle(g, r);
}
}
use of com.github.davidmoten.rtree2.geometry.Rectangle in project rtree2 by davidmoten.
the class Util method mbr.
/**
* Returns the minimum bounding rectangle of a number of items. Benchmarks below
* indicate that when the number of items is >1 this method is more
* performant than one using {@link Rectangle#add(Rectangle)}.
*
* <pre>
* Benchmark Mode Samples Score Score error Units
* c.g.d.r.BenchmarksMbr.mbrList1 thrpt 10 48450492.301 436127.960 ops/s
* c.g.d.r.BenchmarksMbr.mbrList2 thrpt 10 46658242.728 987901.581 ops/s
* c.g.d.r.BenchmarksMbr.mbrList3 thrpt 10 40357809.306 937827.660 ops/s
* c.g.d.r.BenchmarksMbr.mbrList4 thrpt 10 35930532.557 605535.237 ops/s
* c.g.d.r.BenchmarksMbr.mbrOldList1 thrpt 10 55848118.198 1342997.309 ops/s
* c.g.d.r.BenchmarksMbr.mbrOldList2 thrpt 10 25171873.903 395127.918 ops/s
* c.g.d.r.BenchmarksMbr.mbrOldList3 thrpt 10 19222116.139 246965.178 ops/s
* c.g.d.r.BenchmarksMbr.mbrOldList4 thrpt 10 14891862.638 198765.157 ops/s
* </pre>
*
* @param items
* items to bound
* @return the minimum bounding rectangle containings items
*/
public static Rectangle mbr(Collection<? extends HasGeometry> items) {
Preconditions.checkArgument(!items.isEmpty());
double minX1 = Double.MAX_VALUE;
double minY1 = Double.MAX_VALUE;
double maxX2 = -Double.MAX_VALUE;
double maxY2 = -Double.MAX_VALUE;
boolean isDoublePrecision = false;
for (final HasGeometry item : items) {
Rectangle r = item.geometry().mbr();
if (r.isDoublePrecision()) {
isDoublePrecision = true;
}
if (r.x1() < minX1)
minX1 = r.x1();
if (r.y1() < minY1)
minY1 = r.y1();
if (r.x2() > maxX2)
maxX2 = r.x2();
if (r.y2() > maxY2)
maxY2 = r.y2();
}
if (isDoublePrecision) {
return Geometries.rectangle(minX1, minY1, maxX2, maxY2);
} else {
return Geometries.rectangle((float) minX1, (float) minY1, (float) maxX2, (float) maxY2);
}
}
Aggregations