R-Tree Spatial Index for Python GIS
Rtree is a Python library that provides an R-Tree spatial index, implemented as a ctypes wrapper around the high-performance libspatialindex C library. It enables efficient spatial querying capabilities such as nearest neighbor and intersection searches for multi-dimensional data. The library supports bulk loading, deletion, and disk serialization of indexes, making it a crucial tool for various GIS and geospatial data processing tasks. It is actively maintained with regular updates.
Warnings
- breaking Python version requirements have increased over recent major versions. Rtree 1.0.0 required Python 3.7+, 1.1.0 required Python 3.8+, and 1.4.0 (the latest major release) requires Python 3.9+. Users on older Python versions will encounter installation or runtime errors.
- breaking In version 1.4.0, the project and its build components were officially renamed to 'rtree'. While standard import `from rtree import index` remains stable, if any tooling or older scripts relied on previous naming conventions for internal components, they might require updates.
- gotcha Entries inserted into an R-tree index are not inherently unique, either by ID or by bounding box. If uniqueness is a requirement for your application, it must be managed externally before inserting entries into the Rtree index.
- gotcha The coordinate ordering for all Rtree functions is sensitive to the index's `interleaved` data member. If `interleaved` is `False` (the default), coordinates must be `[xmin, xmax, ymin, ymax, ..., kmin, kmax]`. If `True`, they must be `[xmin, ymin, ..., kmin, xmax, ymax, ..., kmax]`. Incorrect ordering will lead to incorrect query results.
- gotcha Rtree is a wrapper for `libspatialindex`, which was not designed to be a full-fledged spatial database. It does not offer typical database integrity protections (e.g., transactions, recovery). While useful for certain applications, it should not be treated as a robust database solution.
Install
-
pip install rtree
Imports
- index
from rtree import index
Quickstart
from rtree import index
# Create an in-memory R-tree index
idx = index.Index()
# Insert some bounding boxes (id, (minx, miny, maxx, maxy))
# Note: IDs are not enforced to be unique by Rtree itself
idx.insert(0, (0, 0, 1, 1)) # Item with ID 0, bounds (0,0) to (1,1)
idx.insert(1, (0.5, 0.5, 1.5, 1.5))
idx.insert(2, (2, 2, 3, 3))
idx.insert(3, (0.8, 0.8, 1.2, 1.2), obj={'name': 'Intersection Point'})
# Query for intersections with a given bounding box
search_bounds = (0.7, 0.7, 1.1, 1.1)
hits = list(idx.intersection(search_bounds))
print(f"IDs intersecting {search_bounds}: {hits}")
# Query for intersections and retrieve objects (if stored)
hits_with_objects = list(idx.intersection(search_bounds, objects=True))
print("Items intersecting (with objects):")
for item in hits_with_objects:
print(f" ID: {item.id}, Bounds: {item.bbox}, Object: {item.object}")
# Query for nearest neighbors
nearest_point = (0.1, 0.1)
nearest_items = list(idx.nearest(nearest_point, num_results=1))
print(f"Nearest item to {nearest_point}: {nearest_items}")