Skip to main content

rstar-python

Python bindings for the rstar R*-tree spatial index library.

A fast, dynamic spatial index for points and stored axis-aligned bounding boxes in 2–8 dimensions. Insert and remove items after construction, run nearest-neighbour and radius searches, and query spatial regions. Each item can carry an integer id so queries can return references to your data.

Installation

pip install rstar-python

Prebuilt abi3 wheels are published for Linux, macOS, and Windows and work on CPython 3.10+.

Why rstar-python?

rstar-python scipy cKDTree / sklearn KDTree Rtree (libspatialindex)
Dynamic insert / remove ❌ (static, rebuild)
Bounding-box / region query ❌ (radius only)
Returns ids of matches ✅ (row indices)
Vectorised batch query
Pure-Rust, no system C/C++ dep (C/Cython, bundled) ❌ (needs libspatialindex)

Reach for the KD-trees in scipy/scikit-learn when you build an index once from a static array and only need point/radius queries. Reach for rstar-python when you need to mutate the index over time or run bounding-box queries — with easy prebuilt wheels and no C/C++ system dependency.

Usage

import numpy as np
from rstar_python import PyBBoxRTree, PyRTree

# Create a 3D R-tree
tree = PyRTree(dims=3)

# Insert points. insert() returns the point's id.
tree.insert([1.0, 2.0, 3.0])            # -> 0  (auto-assigned)
tree.insert([4.0, 5.0, 6.0], data=42)   # -> 42 (explicit id)

# Or bulk-load (replaces existing contents). Accepts lists or numpy arrays,
# and an optional list of ids.
points = np.array([[1.0, 2.0, 3.0],
                   [4.0, 5.0, 6.0],
                   [7.0, 8.0, 9.0]], dtype=np.float64)
tree.bulk_load(points, data=[10, 20, 30])

# --- Coordinate-returning queries ---
tree.nearest_neighbor([1.1, 2.1, 3.1])           # -> [1.0, 2.0, 3.0]
tree.k_nearest_neighbors([1.1, 2.1, 3.1], k=2)   # -> [[...], [...]]
tree.neighbors_within_radius([1.0, 2.0, 3.0], radius=1.0)
tree.locate_in_envelope(min_corner=[0, 0, 0], max_corner=[2, 2, 2])
tree.locate_in_envelope_ids([0, 0, 0], [2, 2, 2])  # stored ids, closed AABB

# Remove an exact point+id when coordinates are shared by multiple items.
tree.remove_item([1.0, 2.0, 3.0], data=10)

# --- Vectorised, id-returning queries (scipy/sklearn style) ---
query_pts = np.array([[1.1, 2.1, 3.1], [7.0, 8.0, 9.0]], dtype=np.float64)
distances, ids = tree.query(query_pts, k=2)
# distances: (2, 2) float64 Euclidean distances
# ids:       (2, 2) int64 ids; padded with -1 / inf if fewer than k exist

within = tree.query_radius(query_pts, radius=1.0)  # list of id lists

# --- Bookkeeping ---
len(tree)                  # number of points
tree.dims                  # 3
[1.0, 2.0, 3.0] in tree    # membership test
tree.remove([1.0, 2.0, 3.0])

# --- Stored bounding boxes ---
boxes = PyBBoxRTree(dims=2)
boxes.insert([0.0, 0.0], [2.0, 2.0], data=100)
boxes.insert([2.0, 1.0], [3.0, 3.0])
# Returns both ids: intersection uses closed bounds, so touching counts.
boxes.intersection([1.0, 1.0], [2.0, 2.0])

# Query many boxes in one native call. Results use CSR-style row offsets:
# the matches for row i are ids[offsets[i]:offsets[i + 1]].
query_mins = np.array([[0.0, 0.0], [5.0, 5.0]], dtype=np.float64)
query_maxs = np.array([[2.0, 2.0], [6.0, 6.0]], dtype=np.float64)
ids, offsets = boxes.intersection_batch(query_mins, query_maxs)

boxes.remove_item([0.0, 0.0], [2.0, 2.0], data=100)

# Bulk loading replaces the stored boxes.
boxes.bulk_load(
    min_corners=[[0.0, 0.0], [5.0, 5.0]],
    max_corners=[[1.0, 1.0], [6.0, 6.0]],
    data=[10, 20],
)

Features

  • Points and stored axis-aligned bounding boxes in 2–8 dimensions
  • Dynamic insertion and exact item removal
  • Per-item integer ids (auto-assigned or supplied)
  • Nearest-neighbour and k-nearest-neighbour queries
  • Radius search and axis-aligned bounding-box (envelope) queries
  • Vectorised point query / query_radius and bounding-box intersection_batch queries over NumPy arrays
  • NumPy-native batched intersection results without Python integer boxing
  • Bulk loading (lists or NumPy arrays) for fast construction
  • Type stubs (py.typed) for IDE and mypy support
  • Built on the fast Rust rstar library

Development

Requirements:

  • Rust (stable)
  • Python 3.10+
  • maturin
git clone https://github.com/kephale/rstar-python
cd rstar-python

python -m venv .venv
source .venv/bin/activate  # or `.venv\Scripts\activate` on Windows

pip install maturin pytest numpy

# Build and install in development mode
maturin develop --release

# Run tests
pytest python/tests -v

# Run the bounding-box performance benchmark
python python/benchmarks/benchmark_bbox_rtree.py

License

This project is licensed under the MIT License - see the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rstar_python-0.2.0.tar.gz (22.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rstar_python-0.2.0-cp310-abi3-win_amd64.whl (598.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

rstar_python-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl (982.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

rstar_python-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl (934.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

rstar_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (763.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

rstar_python-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (759.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

rstar_python-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (1.4 MB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file rstar_python-0.2.0.tar.gz.

File metadata

  • Download URL: rstar_python-0.2.0.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for rstar_python-0.2.0.tar.gz
Algorithm Hash digest
SHA256 42d3dba2e5b3054238a85d644a499d51d5a532e1c97095095d31f193ab96618b
MD5 f19ace0ba328a078e1c83ca36d431666
BLAKE2b-256 51cdf9f9e9225ca0438dd46ebc5c350a8a6c8f2dbc5ed37896b0f941a6464c14

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0.tar.gz:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: rstar_python-0.2.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 598.2 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1355d112e36d818b9d62b09627bf00de39a160caaa04238ca01afb6ea10855fc
MD5 11ee8c2f9edec989d96af7764901a22a
BLAKE2b-256 15132f22d60ef98aa97e6a1eafad0aea794f49b3aff515fb4eb3b7c7cbd0a0f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2cf78a54f0f3971d9d3b6c1f7879647e0d0f199aeee116b7fe27cbfb8c9a3883
MD5 58be869399d132f391168d2e9c5731dd
BLAKE2b-256 63f8507b9c137ddd7d2e70c17990af7f4b6e8d6682a7f76ec7c79e1fc6dc31a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 29e1308377ade8919514f8e9a91804d08f94838a115b9bf02332ea2b7e3d7b86
MD5 fa2fd220ec9147c516ba67e660fe798f
BLAKE2b-256 bd964b36eaa3ab31f7e8371ff2172d47b8fe20a2a6b4596e610db9b36e04b2fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 88d96ce7a2d6d0c220f994c630b1ad53b8f77094dd3a4735c63a5de22076d3f2
MD5 d3318e3c0c666fa1b871d616350c574b
BLAKE2b-256 0554729222a89e4658318cdc2f362e614c550b4ff03b098dfca935a99543ec90

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e30f02892a99345fc8e9d71724f8260c95196ac42af159dac43cd3704d46ce90
MD5 8a251f2b81fb8f790a0d121968995341
BLAKE2b-256 86c9949f8c75365099be03dab34a824984ec370cd4d448769e9f7fd521316d40

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rstar_python-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for rstar_python-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 e82a0270f2dd9b49e0e39e94b127f2b1bedf7587ec84cecec1ba93d0bcb47965
MD5 6cb328be51cca9de8d059b1801d487e5
BLAKE2b-256 182187c55bfadbd675fb4b81413b79ae8b89a4392512f81157a4de5287ea79bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for rstar_python-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on kephale/rstar-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0 This release

7 files

0.1.0

7 files

0.0.2

5 files

0.0.1

5 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page