### Instantiate R-Tree with Default Settings Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates an R-tree using the Quadratic split strategy with default min/max children. No specific setup or imports are required beyond the RTree class itself. ```java RTree tree = RTree.create(); ``` -------------------------------- ### Get Text Representation of R-tree using asString() Source: https://context7.com/davidmoten/rtree/llms.txt Provides a human-readable, indented string representation of the R-tree, detailing each node's MBR and leaf entries. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; RTree tree = RTree.create() .add(1, Geometries.point(28, 19)) .add(2, Geometries.point(29, 4)) .add(3, Geometries.point(10, 63)) .add(4, Geometries.point(34, 85)) .add(5, Geometries.point(62, 45)); System.out.println(tree.asString()); // mbr=Rectangle [x1=10.0, y1=4.0, x2=62.0, y2=85.0] // mbr=Rectangle [x1=28.0, y1=4.0, x2=34.0, y2=85.0] // entry=Entry [value=2, geometry=Point [x=29.0, y=4.0]] // entry=Entry [value=1, geometry=Point [x=28.0, y=19.0]] // entry=Entry [value=4, geometry=Point [x=34.0, y=85.0]] // mbr=Rectangle [x1=10.0, y1=45.0, x2=62.0, y2=63.0] // entry=Entry [value=5, geometry=Point [x=62.0, y=45.0]] // entry=Entry [value=3, geometry=Point [x=10.0, y=63.0]] ``` -------------------------------- ### Filter, map, and reduce R-tree search results Source: https://github.com/davidmoten/rtree/blob/master/README.md Shows how to process the Observable results from an R-tree search. This example filters entries by name, extracts the first character of the name, and reduces the stream to the alphabetically first character. ```java import rx.Observable; import rx.functions.*; import rx.schedulers.Schedulers; Character result = tree.search(Geometries.rectangle(8, 15, 30, 35)) // filter for names alphabetically less than M .filter(entry -> entry.value() < "M") // get the first character of the name .map(entry -> entry.value().charAt(0)) // reduce to the first character alphabetically .reduce((x,y) -> x <= y ? x : y) // subscribe to the stream and block for the result .toBlocking().single(); System.out.println(list); ``` -------------------------------- ### Get Iterable from R-tree Search Source: https://github.com/davidmoten/rtree/blob/master/README.md Shows how to obtain an Iterable directly from an R-tree search result, bypassing the Observable API for simpler use cases. ```java Iterable it = tree.search(Geometries.point(4,5)) .toBlocking().toIterable(); ``` -------------------------------- ### Add entries and search R-tree with configuration Source: https://github.com/davidmoten/rtree/blob/master/README.md Demonstrates creating an R-tree with a specific maximum number of children per node and adding entries. It then performs a rectangular search on the populated tree. ```java import com.github.davidmoten.rtree.RTree; import static com.github.davidmoten.rtree.geometry.Geometries.*; RTree tree = RTree.maxChildren(5).create(); tree = tree.add("DAVE", point(10, 20)) .add("FRED", point(12, 25)) .add("MARY", point(97, 125)); Observable> entries = tree.search(Geometries.rectangle(8, 15, 30, 35)); ``` -------------------------------- ### Run RTree Benchmarks Source: https://github.com/davidmoten/rtree/blob/master/README.md Builds and runs the RTree benchmarks using Maven with the benchmark profile. ```bash mvn clean install -Pbenchmark ``` -------------------------------- ### Create an R*-Tree Source: https://context7.com/davidmoten/rtree/llms.txt Returns a builder pre-configured with R*-tree heuristics. Recommended for datasets of 10,000 entries or more where search performance is critical. Custom node capacities can be specified. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometry; // R*-tree with default maxChildren=4 RTree tree = RTree.star().create(); // R*-tree with custom capacity RTree tree2 = RTree.star().maxChildren(6).create(); ``` -------------------------------- ### Clone and Build RTree Project Source: https://github.com/davidmoten/rtree/blob/master/README.md Clones the RTree project from GitHub and builds it using Maven. ```bash git clone https://github.com/davidmoten/rtree.git cd rtree mvn clean install ``` -------------------------------- ### RTree.create(List>) Source: https://context7.com/davidmoten/rtree/llms.txt Constructs an R-tree from an existing list of entries using the STR bulk loading algorithm for faster index construction. ```APIDOC ## RTree.create(List>) ### Description Constructs an R-tree from an existing list of entries using the Sort-Tile-Recursive (STR) algorithm. 10× faster than individual inserts for large or static datasets. The input list may be reordered. ### Method `RTree.create(List> entries)` ### Parameters * **entries** (List>) - A list of entries to be included in the R-tree. ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.Entries; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.Arrays; import java.util.List; List> entries = Arrays.asList( Entries.entry("Paris", Geometries.point(2.3522, 48.8566)), Entries.entry("London", Geometries.point(-0.1276, 51.5074)), Entries.entry("Berlin", Geometries.point(13.4050, 52.5200)), Entries.entry("Madrid", Geometries.point(-3.7038, 40.4168)) ); // STR bulk-load with default capacity RTree tree = RTree.create(entries); // STR bulk-load with R*-tree heuristics and custom capacity RTree tree2 = RTree.star().maxChildren(6).create(entries); System.out.println(tree.size()); // 4 ``` ``` -------------------------------- ### RTree.star() Source: https://context7.com/davidmoten/rtree/llms.txt Creates a builder pre-configured with R*-tree heuristics, recommended for larger datasets where search performance is critical. ```APIDOC ## RTree.star() ### Description Returns a builder pre-configured with R*-tree heuristics (RStar splitter + RStar selector). Recommended for datasets ≥ 10,000 entries where search performance is critical. ### Method `RTree.star()` ### Parameters None (configuration options like `maxChildren` can be chained before `create()`) ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometry; // R*-tree with default maxChildren=4 RTree tree = RTree.star().create(); // R*-tree with custom capacity RTree tree2 = RTree.star().maxChildren(6).create(); ``` ``` -------------------------------- ### Instantiate R-Star Tree Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates an R*-tree, which employs a more sophisticated splitting and selection strategy for potentially better performance. This is an alternative to the default R-tree. ```java RTree tree = RTree.star().maxChildren(6).create(); ``` -------------------------------- ### Instantiate R-Tree with Custom Min/Max Children Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates an R-tree with specified minimum and maximum children per node. This allows tuning the tree's performance and memory usage. Use this when default values are not optimal for your use case. ```java RTree tree = RTree.minChildren(3).maxChildren(6).create(); ``` -------------------------------- ### STR Bulk Loading for R-Tree Creation Source: https://context7.com/davidmoten/rtree/llms.txt Constructs an R-tree from an existing list of entries using the Sort-Tile-Recursive (STR) algorithm. This is significantly faster than individual inserts for large or static datasets. The input list may be reordered. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.Entries; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.Arrays; import java.util.List; List> entries = Arrays.asList( Entries.entry("Paris", Geometries.point(2.3522, 48.8566)), Entries.entry("London", Geometries.point(-0.1276, 51.5074)), Entries.entry("Berlin", Geometries.point(13.4050, 52.5200)), Entries.entry("Madrid", Geometries.point(-3.7038, 40.4168)) ); // STR bulk-load with default capacity RTree tree = RTree.create(entries); // STR bulk-load with R*-tree heuristics and custom capacity RTree tree2 = RTree.star().maxChildren(6).create(entries); System.out.println(tree.size()); // 4 ``` -------------------------------- ### RTree.create() Source: https://context7.com/davidmoten/rtree/llms.txt Creates a new empty R-tree using Quadratic splitting with default node capacities. Suitable for smaller datasets. ```APIDOC ## RTree.create() ### Description Returns a new empty R-tree using Quadratic splitting with default `maxChildren=4`. Suitable for datasets up to ~10,000 entries. ### Method `RTree.create()` ### Parameters None ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometry; // Default Quadratic-split tree (maxChildren=4, minChildren=2) RTree tree = RTree.create(); // With explicit node capacity RTree tree2 = RTree.minChildren(2).maxChildren(6).create(); System.out.println(tree.isEmpty()); // true System.out.println(tree.size()); // 0 ``` ``` -------------------------------- ### Create Geometries with Double and Single Precision Source: https://github.com/davidmoten/rtree/blob/master/README.md Demonstrates how to create rectangle geometries using both double and single precision floating-point values. Use single precision to minimize memory usage. ```java Rectangle r = Geometries.rectangle(1.0, 2.0, 3.0, 4.0); ``` ```java Rectangle r = Geometries.rectangle(1.0f, 2.0f, 3.0f, 4.0f); ``` -------------------------------- ### Render R-tree as PNG using visualize() Source: https://context7.com/davidmoten/rtree/llms.txt Generates a PNG image visualizing the bounding boxes at each tree level. Supports auto-fitting the coordinate view or specifying an explicit view and format. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.Random; RTree tree = RTree.star().maxChildren(8).create(); Random rnd = new Random(42); for (int i = 0; i < 1000; i++) { tree = tree.add(i, Geometries.point(rnd.nextDouble() * 100, rnd.nextDouble() * 100)); } // Save a 600×600 PNG — auto-fits the coordinate view to the data extent tree.visualize(600, 600).save("target/mytree.png"); // Save with an explicit coordinate view tree.visualize(600, 600, Geometries.rectangle(0, 0, 100, 100)) .save("target/mytree-custom.png", "PNG"); ``` -------------------------------- ### Create a Default R-Tree Source: https://context7.com/davidmoten/rtree/llms.txt Creates a new empty R-tree using Quadratic splitting with default node capacities. Suitable for datasets up to approximately 10,000 entries. You can also explicitly set min/max children. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometry; // Default Quadratic-split tree (maxChildren=4, minChildren=2) RTree tree = RTree.create(); // With explicit node capacity RTree tree2 = RTree.minChildren(2).maxChildren(6).create(); System.out.println(tree.isEmpty()); // true System.out.println(tree.size()); // 0 ``` -------------------------------- ### Visualize R-tree to PNG Source: https://github.com/davidmoten/rtree/blob/master/README.md Renders the R-tree structure into a PNG image file. Specify the desired width and height for the output image. ```java tree.visualize(600,600) .save("target/mytree.png"); ``` -------------------------------- ### Reactive Composition with RxJava Source: https://context7.com/davidmoten/rtree/llms.txt Demonstrates filtering, mapping, and reducing search results from an R-tree using RxJava operators. Requires RxJava for blocking operations. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; RTree tree = RTree.maxChildren(5).create() .add("DAVE", Geometries.point(10, 20)) .add("FRED", Geometries.point(12, 25)) .add("MARY", Geometries.point(97, 125)); // Filter, map and reduce the search Observable Character result = tree.search(Geometries.rectangle(8, 15, 30, 35)) // keep only names alphabetically before "M" .filter(entry -> entry.value().compareTo("M") < 0) // extract the first character .map(entry -> entry.value().charAt(0)) // take the smallest character alphabetically .reduce((x, y) -> x <= y ? x : y) .toBlocking() .single(); System.out.println(result); // D ``` -------------------------------- ### FlatBuffers Serialization and Deserialization Source: https://context7.com/davidmoten/rtree/llms.txt Shows how to persist an R-tree to an OutputStream and reload it from an InputStream using FlatBuffers serializers. Supports memory-efficient single-array structure or the default structure. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Serializer; import com.github.davidmoten.rtree.Serializers; import com.github.davidmoten.rtree.InternalStructure; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.io.*; // Build a tree RTree tree = RTree.star().maxChildren(10).create() .add("Paris", Geometries.point(2.35, 48.86)) .add("London", Geometries.point(-0.13, 51.51)) .add("Berlin", Geometries.point(13.41, 52.52)); // Create a UTF-8 string serializer backed by FlatBuffers Serializer serializer = Serializers.flatBuffers().utf8(); // --- Write --- File file = new File("target/cities.rtree"); try (OutputStream os = new FileOutputStream(file)) { serializer.write(tree, os); } System.out.println("Written: " + file.length() + " bytes"); // --- Read into memory-efficient single-array structure --- try (InputStream is = new FileInputStream(file)) { RTree loaded = serializer.read(is, file.length(), InternalStructure.SINGLE_ARRAY); System.out.println("Loaded entries: " + loaded.size()); // 3 loaded.search(Geometries.rectangle(-1, 48, 14, 53)) .toBlocking() .forEach(e -> System.out.println(e.value())); // Paris, London, Berlin } // --- Read into standard default structure (faster searches) --- try (InputStream is = new FileInputStream(file)) { RTree loaded = serializer.read(is, file.length(), InternalStructure.DEFAULT); System.out.println("Default structure entries: " + loaded.size()); // 3 } ``` -------------------------------- ### Float vs Double Precision Geometries in RTree Source: https://context7.com/davidmoten/rtree/llms.txt Demonstrates the creation of geometries using both double (default) and float (single precision) overloads. Using float can halve the memory consumed by coordinate storage. ```java import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Rectangle; import com.github.davidmoten.rtree.geometry.Circle; import com.github.davidmoten.rtree.geometry.Point; // Double precision (default — higher accuracy) Rectangle rd = Geometries.rectangle(1.0, 2.0, 3.0, 4.0); Circle cd = Geometries.circle(5.0, 6.0, 2.5); Point pd = Geometries.point(7.0, 8.0); // Single precision (float — ~50% less memory per geometry object) Rectangle rf = Geometries.rectangle(1.0f, 2.0f, 3.0f, 4.0f); Circle cf = Geometries.circle(5.0f, 6.0f, 2.5f); Point pf = Geometries.point(7.0f, 8.0f); System.out.println(rd.x1()); // 1.0 System.out.println(rf.x1()); // 1.0 (stored as float internally) ``` -------------------------------- ### R-tree Benchmark Results Source: https://github.com/davidmoten/rtree/blob/master/README.md Benchmark results for rtree version 0.8-RC7 on an i7-920 @2.67GHz. This data shows performance metrics for various insertion, search, and deletion operations with different R-tree configurations (MaxChildren) and datasets (GreekData). ```text Benchmark Mode Cnt Score Error Units defaultRTreeInsertOneEntryInto1000EntriesMaxChildren004 thrpt 10 262260.993 ± 2767.035 ops/s defaultRTreeInsertOneEntryInto1000EntriesMaxChildren010 thrpt 10 296264.913 ± 2836.358 ops/s defaultRTreeInsertOneEntryInto1000EntriesMaxChildren032 thrpt 10 135118.271 ± 1722.039 ops/s defaultRTreeInsertOneEntryInto1000EntriesMaxChildren128 thrpt 10 315851.452 ± 3097.496 ops/s defaultRTreeInsertOneEntryIntoGreekDataEntriesMaxChildren004 thrpt 10 278761.674 ± 4182.761 ops/s defaultRTreeInsertOneEntryIntoGreekDataEntriesMaxChildren010 thrpt 10 315254.478 ± 4104.206 ops/s defaultRTreeInsertOneEntryIntoGreekDataEntriesMaxChildren032 thrpt 10 214509.476 ± 1555.816 ops/s defaultRTreeInsertOneEntryIntoGreekDataEntriesMaxChildren128 thrpt 10 118094.486 ± 1118.983 ops/s defaultRTreeSearchOf1000PointsMaxChildren004 thrpt 10 1122140.598 ± 8509.106 ops/s defaultRTreeSearchOf1000PointsMaxChildren010 thrpt 10 569779.807 ± 4206.544 ops/s defaultRTreeSearchOf1000PointsMaxChildren032 thrpt 10 238251.898 ± 3916.281 ops/s defaultRTreeSearchOf1000PointsMaxChildren128 thrpt 10 702437.901 ± 5108.786 ops/s defaultRTreeSearchOfGreekDataPointsMaxChildren004 thrpt 10 462243.509 ± 7076.045 ops/s defaultRTreeSearchOfGreekDataPointsMaxChildren010 thrpt 10 326395.724 ± 1699.043 ops/s defaultRTreeSearchOfGreekDataPointsMaxChildren032 thrpt 10 156978.822 ± 1993.372 ops/s defaultRTreeSearchOfGreekDataPointsMaxChildren128 thrpt 10 68267.160 ± 929.236 ops/s rStarTreeDeleteOneEveryOccurrenceFromGreekDataChildren010 thrpt 10 211881.061 ± 3246.693 ops/s rStarTreeInsertOneEntryInto1000EntriesMaxChildren004 thrpt 10 187062.089 ± 3005.413 ops/s rStarTreeInsertOneEntryInto1000EntriesMaxChildren010 thrpt 10 186767.045 ± 2291.196 ops/s rStarTreeInsertOneEntryInto1000EntriesMaxChildren032 thrpt 10 37940.625 ± 743.789 ops/s rStarTreeInsertOneEntryInto1000EntriesMaxChildren128 thrpt 10 151897.089 ± 674.941 ops/s rStarTreeInsertOneEntryIntoGreekDataEntriesMaxChildren004 thrpt 10 237708.825 ± 1644.611 ops/s rStarTreeInsertOneEntryIntoGreekDataEntriesMaxChildren010 thrpt 10 229577.905 ± 4234.760 ops/s rStarTreeInsertOneEntryIntoGreekDataEntriesMaxChildren032 thrpt 10 78290.971 ± 393.030 ops/s rStarTreeInsertOneEntryIntoGreekDataEntriesMaxChildren128 thrpt 10 6521.010 ± 50.798 ops/s rStarTreeSearchOf1000PointsMaxChildren004 thrpt 10 1330510.951 ± 18289.410 ops/s rStarTreeSearchOf1000PointsMaxChildren010 thrpt 10 1204347.202 ± 17403.105 ops/s rStarTreeSearchOf1000PointsMaxChildren032 thrpt 10 576765.468 ± 8909.880 ops/s rStarTreeSearchOf1000PointsMaxChildren128 thrpt 10 1028316.856 ± 13747.282 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren004 thrpt 10 904494.751 ± 15640.005 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren010 thrpt 10 649636.969 ± 16383.786 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren010FlatBuffers thrpt 10 84230.053 ± 1869.345 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren010FlatBuffersBackpressure thrpt 10 36420.500 ± 1572.298 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren010WithBackpressure thrpt 10 116970.445 ± 1955.659 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren032 thrpt 10 224874.016 ± 14462.325 ops/s rStarTreeSearchOfGreekDataPointsMaxChildren128 thrpt 10 358636.637 ± 4886.459 ops/s searchNearestGreek thrpt 10 3715.020 ± 46.570 ops/s ``` -------------------------------- ### RTree Tree Metadata: MBR and Size Source: https://context7.com/davidmoten/rtree/llms.txt Shows how to inspect the overall bounding rectangle (MBR) and the number of entries in an RTree. This is useful for understanding the spatial extent and content of the index. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.Optional; import com.github.davidmoten.rtree.geometry.Rectangle; RTree tree = RTree.create() .add("A", Geometries.point(1, 1)) .add("B", Geometries.point(9, 7)); System.out.println(tree.size()); // 2 System.out.println(tree.isEmpty()); // false Optional mbr = tree.mbr(); mbr.ifPresent(r -> System.out.printf( "MBR: [%.1f,%.1f,%.1f,%.1f]%n", r.x1(), r.y1(), r.x2(), r.y2())); // MBR: [1.0,1.0,9.0,7.0] System.out.println(tree.calculateDepth()); // 1 ``` -------------------------------- ### Trigger Coverity Scan Source: https://github.com/davidmoten/rtree/blob/master/README.md Instructions for project committers to trigger a Coverity scan by updating the 'coverity_scan' branch. ```bash git checkout coverity_scan git pull origin master git push origin coverity_scan ``` -------------------------------- ### Add Maven Dependency for R-tree Source: https://github.com/davidmoten/rtree/blob/master/README.md Include this XML snippet in your pom.xml to add the rtree library as a project dependency. Replace VERSION_HERE with the desired version. ```xml com.github.davidmoten rtree VERSION_HERE ``` -------------------------------- ### Search R-tree with custom geometry, distance, and distance function Source: https://github.com/davidmoten/rtree/blob/master/README.md Performs a proximity search using a custom geometry and a function to calculate the distance between geometries. Specify the maximum distance for the search. ```java RTree tree = RTree.create(); Func2 distancePointToPolygon = ... Polygon polygon = ... ... entries = tree.search(polygon, 10, distancePointToPolygon); ``` -------------------------------- ### Add Flatbuffers Dependency for Serialization Source: https://github.com/davidmoten/rtree/blob/master/README.md Specifies the Maven dependency required to use flatbuffers for R-tree serialization. This is an optional dependency. ```xml com.google.flatbuffers flatbuffers-java 2.0.3 true ``` -------------------------------- ### tree.search(Point) Source: https://context7.com/davidmoten/rtree/llms.txt Retrieves all entries whose minimum bounding rectangle contains the specified point. ```APIDOC ## `tree.search(Point)` — Search by Point Returns all entries whose MBR contains the given point. ### Parameters #### Path Parameters - **point** (Point) - The point to search for. ### Request Example ```java RTree tree = RTree.create() .add("Zone-A", Geometries.rectangle(0, 0, 10, 10)) .add("Zone-B", Geometries.rectangle(5, 5, 15, 15)) .add("Zone-C", Geometries.rectangle(20, 20, 30, 30)); // Find all zones containing the point (6, 6) tree.search(Geometries.point(6, 6)) .toBlocking() .forEach(e -> System.out.println(e.value())); // Zone-A // Zone-B ``` ``` -------------------------------- ### R-tree Structure as Text Source: https://github.com/davidmoten/rtree/blob/master/README.md Outputs the R-tree's internal structure, including bounding boxes and entries, as a human-readable string. Useful for debugging and understanding the tree's organization. ```text mbr=Rectangle [x1=10.0, y1=4.0, x2=62.0, y2=85.0] mbr=Rectangle [x1=28.0, y1=4.0, x2=34.0, y2=85.0] entry=Entry [value=2, geometry=Point [x=29.0, y=4.0]] entry=Entry [value=1, geometry=Point [x=28.0, y=19.0]] entry=Entry [value=4, geometry=Point [x=34.0, y=85.0]] mbr=Rectangle [x1=10.0, y1=45.0, x2=62.0, y2=63.0] entry=Entry [value=5, geometry=Point [x=62.0, y=45.0]] entry=Entry [value=3, geometry=Point [x=10.0, y=63.0]] ``` -------------------------------- ### Geospatial Search — Latitude/Longitude Points Source: https://context7.com/davidmoten/rtree/llms.txt Demonstrates how to perform searches using geographic coordinates (latitude and longitude), handling longitude wraparound and using great-circle distance for accuracy. ```APIDOC ## Geospatial Search — Latitude/Longitude Points Use `Geometries.pointGeographic` and `rectangleGeographic` to correctly handle longitude wraparound at the ±180° boundary. Combine with a great-circle distance filter for precise radius searches. ### Description Enables searching within an RTree that stores geographic points, accounting for the Earth's curvature and the ±180° longitude line. ### Usage This section illustrates a practical example of searching for points within a certain radius of a given geographic location. ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import com.github.davidmoten.rtree.geometry.Rectangle; import com.github.davidmoten.grumpy.core.Position; import rx.Observable; RTree tree = RTree.star().create() .add("Sydney", Geometries.point(151.2094, -33.86)) .add("Brisbane", Geometries.point(153.0278, -27.4679)); Point canberra = Geometries.point(149.1244, -35.3075); double distanceKm = 300; Position from = Position.create(canberra.y(), canberra.x()); // Build a bounding box enclosing the search radius, then refine with exact distance Position north = from.predict(distanceKm, 0); Position south = from.predict(distanceKm, 180); Position east = from.predict(distanceKm, 90); Position west = from.predict(distanceKm, 270); Rectangle bounds = Geometries.rectangle( west.getLon(), south.getLat(), east.getLon(), north.getLat()); Observable> results = tree .search(bounds) .filter(entry -> { Position pos = Position.create(entry.geometry().y(), entry.geometry().x()); return from.getDistanceToKm(pos) < distanceKm; }); results.toBlocking().forEach(e -> System.out.println(e.value())); // Sydney (Brisbane is > 300km from Canberra) ``` ### Response Returns an Observable of `Entry` objects that are within the specified geographic radius. ``` -------------------------------- ### Read RTree from InputStream (Low-Memory) Source: https://github.com/davidmoten/rtree/blob/master/README.md Reads an RTree from an InputStream into a low-memory flatbuffers-based structure. Requires the serializer, InputStream, length, and InternalStructure.SINGLE_ARRAY. ```java RTree tree = serializer.read(is, lengthBytes, InternalStructure.SINGLE_ARRAY); ``` -------------------------------- ### Create Geographic Point Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates a Point geometry using longitude and latitude values, handling Earth's wraparound at the 180/-180 meridian. Use this for geospatial applications. ```java Point point = Geometries.pointGeographic(lon, lat); ``` -------------------------------- ### tree.search(Rectangle) Source: https://context7.com/davidmoten/rtree/llms.txt Searches the R-tree for entries whose minimum bounding rectangle intersects with the given rectangle. The search is evaluated lazily and can be cancelled by unsubscription. ```APIDOC ## `tree.search(Rectangle)` — Search by Bounding Box Returns an `Observable` sequence of all entries whose minimum bounding rectangle intersects the given rectangle. Evaluated lazily; cancelled by unsubscription. ### Parameters #### Path Parameters - **rectangle** (Rectangle) - The bounding box to search within. ### Request Example ```java RTree tree = RTree.maxChildren(5).create() .add("DAVE", Geometries.point(10, 20)) .add("FRED", Geometries.point(12, 25)) .add("MARY", Geometries.point(97, 125)); // Search for entries intersecting the rectangle [8,15,30,35] List> results = tree.search(Geometries.rectangle(8, 15, 30, 35)) .toList() .toBlocking() .single(); // Returns DAVE and FRED results.forEach(e -> System.out.println(e.value() + " @ " + e.geometry())); // DAVE @ Point [x=10.0, y=20.0] // FRED @ Point [x=12.0, y=25.0] // Use as Iterable without the Observable API Iterable> it = tree.search(Geometries.rectangle(8, 15, 30, 35)) .toBlocking() .toIterable(); ``` ``` -------------------------------- ### Read RTree from InputStream (Default Structure) Source: https://github.com/davidmoten/rtree/blob/master/README.md Reads an RTree from an InputStream into the default structure. Requires the serializer, InputStream, length, and InternalStructure.DEFAULT. ```java RTree tree = serializer.read(is, lengthBytes, InternalStructure.DEFAULT); ``` -------------------------------- ### Retrieve all entries from R-tree Source: https://github.com/davidmoten/rtree/blob/master/README.md Returns an Observable sequence of all entries currently stored in the R-tree. This can be used to iterate over the entire dataset. ```java Observable> results = tree.entries(); ``` -------------------------------- ### Search R-tree with custom geometry and intersection function Source: https://github.com/davidmoten/rtree/blob/master/README.md Allows searching using a custom geometry (e.g., Polygon) and a provided intersection function. You must implement the intersection logic for your specific geometry types. ```java RTree tree = RTree.create(); Func2 pointInPolygon = ... Polygon polygon = ... ... entries = tree.search(polygon, pointInPolygon); ``` -------------------------------- ### tree.entries() Source: https://context7.com/davidmoten/rtree/llms.txt Retrieves all entries currently stored within the RTree. ```APIDOC ## `tree.entries()` — Return All Entries Returns an `Observable` of every entry in the tree. ### Description Provides a stream of all entries contained within the RTree. ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; RTree tree = RTree.create() .add("A", Geometries.point(1, 1)) .add("B", Geometries.point(2, 2)) .add("C", Geometries.point(3, 3)); int count = tree.entries().count().toBlocking().single(); System.out.println(count); // 3 tree.entries() .toBlocking() .forEach(e -> System.out.println(e.value() + " -> " + e.geometry())); // A -> Point [x=1.0, y=1.0] // B -> Point [x=2.0, y=2.0] // C -> Point [x=3.0, y=3.0] ``` ### Response Returns an Observable of `Entry` objects, representing all items in the tree. ``` -------------------------------- ### Search RTree by Bounding Box Source: https://context7.com/davidmoten/rtree/llms.txt Finds entries intersecting a given rectangle. The search is lazy and can be cancelled by unsubscription. Results can be collected into a List or iterated over. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import rx.Observable; import java.util.List; RTree tree = RTree.maxChildren(5).create() .add("DAVE", Geometries.point(10, 20)) .add("FRED", Geometries.point(12, 25)) .add("MARY", Geometries.point(97, 125)); // Search for entries intersecting the rectangle [8,15,30,35] List> results = tree.search(Geometries.rectangle(8, 15, 30, 35)) .toList() .toBlocking() .single(); // Returns DAVE and FRED results.forEach(e -> System.out.println(e.value() + " @ " + e.geometry())); // DAVE @ Point [x=10.0, y=20.0] // FRED @ Point [x=12.0, y=25.0] // Use as Iterable without the Observable API Iterable> it = tree.search(Geometries.rectangle(8, 15, 30, 35)) .toBlocking() .toIterable(); ``` -------------------------------- ### Insert an Entry into an R-Tree Source: https://context7.com/davidmoten/rtree/llms.txt Returns a new immutable R-tree containing the added entry. The original tree remains unchanged. Insertions can be chained. Entries can be added directly or using an Entry object. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; RTree tree = RTree.maxChildren(5).create(); // Chain insertions — each call returns a new tree tree = tree .add("DAVE", Geometries.point(10, 20)) .add("FRED", Geometries.point(12, 25)) .add("MARY", Geometries.point(97, 125)); System.out.println(tree.size()); // 3 // Alternatively use an Entry object import com.github.davidmoten.rtree.Entries; tree = tree.add(Entries.entry("ALICE", Geometries.point(15, 30))); System.out.println(tree.size()); // 4 ``` -------------------------------- ### Instantiate R-Tree with Generic Type Safety Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates an R-tree where the geometry type is known at compile time (e.g., Point). This enhances type safety and can simplify code by avoiding generic casts. ```java RTree tree = RTree.create(); ``` -------------------------------- ### tree.nearest(Rectangle, double maxDistance, int maxCount) Source: https://context7.com/davidmoten/rtree/llms.txt Finds the nearest entries to a given rectangle within a specified maximum distance and count. Results are ordered by distance. ```APIDOC ## `tree.nearest(Rectangle, double maxDistance, int maxCount)` — Nearest-Neighbor Search Returns the nearest `maxCount` entries to a rectangle, strictly within `maxDistance`, in ascending order of distance. ### Description Performs a nearest-neighbor search to find a specified number of entries closest to a given rectangle, constrained by a maximum distance. ### Parameters * **rectangle**: The reference rectangle for the search. * **maxDistance**: The maximum distance from the reference rectangle to consider entries. * **maxCount**: The maximum number of nearest entries to return. ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.List; RTree tree = RTree.create() .add("A", Geometries.point(1, 1)) .add("B", Geometries.point(3, 3)) .add("C", Geometries.point(6, 6)) .add("D", Geometries.point(10, 10)); // Find the 2 nearest entries to the origin within distance 10 List> nearest = tree.nearest(Geometries.rectangle(0, 0, 0, 0), 10.0, 2) .toList().toBlocking().single(); nearest.forEach(e -> System.out.println(e.value())); // A (closest) // B (second closest) ``` ### Response Returns an Observable of `Entry` objects representing the nearest neighbors, ordered by distance. ``` -------------------------------- ### Nearest-Neighbor Search with RTree Source: https://context7.com/davidmoten/rtree/llms.txt Finds the nearest entries to a specified rectangle within a maximum distance and count. Results are ordered by distance. Ensure maxDistance and maxCount are set appropriately for your needs. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Point; import java.util.List; RTree tree = RTree.create() .add("A", Geometries.point(1, 1)) .add("B", Geometries.point(3, 3)) .add("C", Geometries.point(6, 6)) .add("D", Geometries.point(10, 10)); // Find the 2 nearest entries to the origin within distance 10 List> nearest = tree.nearest(Geometries.rectangle(0, 0, 0, 0), 10.0, 2) .toList().toBlocking().single(); nearest.forEach(e -> System.out.println(e.value())); // A (closest) // B (second closest) ``` -------------------------------- ### Search RTree by Point Source: https://context7.com/davidmoten/rtree/llms.txt Finds all entries whose minimum bounding rectangle contains the specified point. This is useful for point-in-polygon or zone-lookup operations. ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Rectangle; RTree tree = RTree.create() .add("Zone-A", Geometries.rectangle(0, 0, 10, 10)) .add("Zone-B", Geometries.rectangle(5, 5, 15, 15)) .add("Zone-C", Geometries.rectangle(20, 20, 30, 30)); // Find all zones containing the point (6, 6) tree.search(Geometries.point(6, 6)) .toBlocking() .forEach(e -> System.out.println(e.value())); // Zone-A // Zone-B ``` -------------------------------- ### Search R-tree for entries within a rectangle Source: https://github.com/davidmoten/rtree/blob/master/README.md Use this method to find all entries whose geometries intersect with the specified rectangular region. Returns an Observable sequence of matching entries. ```java Observable> results = tree.search(Geometries.rectangle(0,0,2,2)); ``` -------------------------------- ### Maven Dependency for RTree Source: https://context7.com/davidmoten/rtree/llms.txt Add this dependency to your Maven project to include the RTree library. An optional dependency for FlatBuffers serialization is also provided. ```xml com.github.davidmoten rtree 0.12 com.google.flatbuffers flatbuffers-java 2.0.3 ``` -------------------------------- ### tree.search(Circle) Source: https://context7.com/davidmoten/rtree/llms.txt Returns all entries whose geometry intersects with the specified circle. ```APIDOC ## `tree.search(Circle)` — Search by Circle Returns all entries whose geometry intersects the given circle. ### Parameters #### Path Parameters - **circle** (Circle) - The circle to search within. ### Request Example ```java import com.github.davidmoten.rtree.RTree; import com.github.davidmoten.rtree.Entry; import com.github.davidmoten.rtree.geometry.Geometries; import com.github.davidmoten.rtree.geometry.Geometry; import java.util.List; RTree tree = RTree.create() .add("near", Geometries.point(1, 1)) .add("edge", Geometries.point(4, 0)) .add("far", Geometries.point(10, 10)); List hits = tree .search(Geometries.circle(0, 0, 5)) // circle centred at origin, radius 5 .map(e -> e.value()) .toList().toBlocking().single(); System.out.println(hits); // [near, edge] ``` ``` -------------------------------- ### Create Geographic Rectangle Source: https://github.com/davidmoten/rtree/blob/master/README.md Creates a Rectangle geometry for geospatial data, normalizing longitude values and adjusting for wraparound. This ensures correct representation of rectangles crossing the antimeridian. ```java Rectangle rectangle = Geometries.rectangleGeographic(lon1, lat1, lon2, lat2); ```