RTree

class RTree<T : GeoJsonObject>(initialData: List<T> = emptyList(), maxEntries: Int = 16) : Collection<T> (source)

A high-performance R-tree-based spatial index for 2D points and rectangles.

RTree is an implementation of an R-tree, a tree data structure used for spatial access methods, i.e., for indexing multi-dimensional information such as geographical coordinates, rectangles, or polygons. It's a popular choice for spatial indexing due to its efficiency in handling dynamic datasets (i.e., data that changes over time) and its speed in performing spatial queries.

This implementation is a port of the JavaScript RBush library and is optimized for indexing and querying large datasets of geographical features. It supports operations like insertion, removal, and spatial searches (e.g., finding all items within a given bounding box).

The performance of the R-tree is heavily influenced by the maxEntries parameter. A higher value for maxEntries leads to a tree with fewer, fuller nodes. This can speed up initial loading and insertion operations but may slow down search queries because each node covers a larger area. Conversely, a lower maxEntries value creates a more fine-grained tree with smaller nodes, which can be faster for searches but slower for insertions. The default value of 16 is a commonly used and well-balanced choice.

Parameters

T

The type of Feature to be stored in the tree. The feature's geometry is used to calculate its bounding box for indexing.

maxEntries

The maximum number of entries in a single tree node. A higher value leads to faster loading/insertion but slower searches, and vice versa. Must be 4 or greater. Defaults to 16.

Constructors

Link copied to clipboard
constructor(initialData: List<T> = emptyList(), maxEntries: Int = 16)

Properties

Link copied to clipboard
open override val size: Int

Functions

Link copied to clipboard
fun clear()

Removes all items from the tree.

Link copied to clipboard

Checks if there are any items in the tree that intersect with the given bounding box. This is a faster alternative to search if you only need to know if an intersection exists, as it returns as soon as the first overlapping item is found.

Link copied to clipboard
open operator override fun contains(element: T): Boolean
Link copied to clipboard
open override fun containsAll(elements: Collection<T>): Boolean
Link copied to clipboard
fun insert(item: T)

Inserts a single item into the tree.

fun insert(data: List<T>)

Bulk-loads data into the R-tree.

Link copied to clipboard
open override fun isEmpty(): Boolean
Link copied to clipboard
open operator override fun iterator(): Iterator<T>
Link copied to clipboard

Finds the nearest point in the collection to the given position.

Link copied to clipboard
fun remove(item: T): Boolean

Removes a specific item from the tree.

Link copied to clipboard
fun search(bbox: BoundingBox): List<T>

Searches the R-tree for all items that intersect with the given bounding box.