Use supported Geometries
masterThe following geometries are supported for insertion into an RTree:
RectanglePointCircleLine(requiresjts-coredependency)
repository·master·Indexed 22 days ago
https://github.com/davidmoten/rtreeAn in-memory, immutable 2D R-tree implementation for Java. It features RxJava Observables for reactive search results and supports spatial indexing heuristics including Quadratic split and R*-tree. The library supports Rectangle, Point, Circle, and Line geometries, and allows for custom Geometry implementations. It provides options for STR bulk loading, FlatBuffers serialization for reduced memory usage, and specialized geographic factory methods for longitude wraparound.
The following geometries are supported for insertion into an RTree:
RectanglePointCircleLine (requires jts-core dependency)Benchmarks can be executed using the Maven benchmark profile.
mvn clean install -PbenchmarkTo use FlatBuffers for serialization (which can reduce memory usage by approximately one third), add the flatbuffers-java dependency to your pom.xml.
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>2.0.3</version>
<optional>true</optional>
</dependency>To build the project from source using Maven, clone the repository and run the mvn clean install command.
git clone https://github.com/davidmoten/rtree.git
cd rtree
mvn clean installPerformance depends on your dataset characteristics. General recommendations:
maxChildren=4).RTree.star()).float values in your geometries instead of double where possible.Add the following dependency to your pom.xml to use the rtree library. Replace VERSION_HERE with the desired version (e.g., 0.12).
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>rtree</artifactId>
<version>VERSION_HERE</version>
</dependency>Use the static builder methods on the RTree class to create a new tree. You can configure parameters like minChildren, maxChildren, splitter, and selector.
// Create a default R-tree using Quadratic split
RTree<String, Geometry> tree = RTree.create();
// Create an R-tree with custom min/max children
RTree<String, Geometry> tree = RTree.minChildren(3).maxChildren(6).create();You can visualize the tree structure as a PNG image or as a text representation.
// Save as PNG
tree.visualize(600,600).save("target/mytree.png");
// Get text representation
String text = RTree.asString();If you know the entry geometry type (e.g., Point), specify it in the RTree generic type to gain more type safety.
RTree<String, Point> tree = RTree.create();To handle longitude wraparound (the -180/180 boundary), use the specialized geographic factory methods in the Geometries class. These normalize longitude to the [-180, 180) interval.
Point point = Geometries.pointGeographic(lon, lat);
Rectangle rectangle = Geometries.rectangleGeographic(lon1, lat1, lon2, lat2);You can persist an RTree to an OutputStream and reconstruct it from an InputStream using a Serializer. The library supports FlatBuffers for low-memory footprint structures.
When reading from an InputStream, you can choose between two InternalStructure modes:
InternalStructure.SINGLE_ARRAY: Loads the tree into a low-memory FlatBuffers-based structure.InternalStructure.DEFAULT: Loads the tree into the default R-Tree structure.// Write an RTree to an OutputStream
RTree<String, Point> tree = ...;
OutputStream os = ...;
Serializer<String, Point> serializer =
Serializers.flatBuffers().utf8();
serializer.write(tree, os);
// Read into a low-memory flatbuffers based structure
RTree<String, Point> treeLowMem =
serializer.read(is, lengthBytes, InternalStructure.SINGLE_ARRAY);
// Read into a default structure
RTree<String, Point> treeDefault =
serializer.read(is, lengthBytes, InternalStructure.DEFAULT);If you prefer not to use the RxJava Observable API, you can convert search results to a standard Java Iterable using .toBlocking().toIterable().
Iterable<T> it = tree.search(Geometries.point(4,5))
.toBlocking().toIterable();