Search in sources :

Example 1 with Rectangle

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);
}
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 Rectangle

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());
    }
}
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 3 with Rectangle

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);
    }
}
Also used : Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle)

Example 4 with Rectangle

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);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle)

Example 5 with Rectangle

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 &gt;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);
    }
}
Also used : Rectangle(com.github.davidmoten.rtree2.geometry.Rectangle) HasGeometry(com.github.davidmoten.rtree2.geometry.HasGeometry)

Aggregations

Rectangle (com.github.davidmoten.rtree2.geometry.Rectangle)15 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)7 Point (com.github.davidmoten.rtree2.geometry.Point)6 PointXY (me.wobblyyyy.pathfinder2.geometry.PointXY)6 Rectangle (me.wobblyyyy.pathfinder2.geometry.Rectangle)6 Zone (me.wobblyyyy.pathfinder2.zones.Zone)6 HasGeometry (com.github.davidmoten.rtree2.geometry.HasGeometry)5 Test (org.junit.jupiter.api.Test)5 Geometries.point (com.github.davidmoten.rtree2.geometry.Geometries.point)3 Geometry (com.github.davidmoten.rtree2.geometry.Geometry)3 HashSet (java.util.HashSet)3 Position (com.github.davidmoten.grumpy.core.Position)2 LeafDefault (com.github.davidmoten.rtree2.internal.LeafDefault)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1