rtree Java Library

repository·master·Indexed 22 days ago

https://github.com/davidmoten/rtree

An 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.

Tokens
2.5K
Snippets
15
Records
18
Agent score
28%

What's inside rtree

  1. Enable FlatBuffers serialization

    master

    To 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>
  2. Configure R-tree for performance

    master

    Performance depends on your dataset characteristics. General recommendations:

    • Small datasets (< 10,000 entries): Use default R-tree (Quadratic splitter, maxChildren=4).
    • Large datasets (>= 10,000 entries): Use R*-tree (RTree.star()).
    • Static/Large datasets (where creation time matters): Use STR bulk loading.
    • Memory optimization: Use single-precision float values in your geometries instead of double where possible.
  3. Install rtree via Maven

    master

    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>
  4. Instantiate an R-Tree

    master

    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();
  5. Visualize the R-tree

    master

    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();
  6. Handle Geospatial geometries (lats and longs)

    master

    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);
  7. Serialize and deserialize an RTree

    master

    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);
  8. Convert Observable search results to Iterable

    master

    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();