Rust-accelerated quadtree for Python with fast inserts, range queries, and k-NN search.
Project description
fastquadtree
Rust-optimized quadtree with a clean Python API
👉 Check out the Docs: https://elan456.github.io/fastquadtree/
Why use fastquadtree
- Just pip install: prebuilt wheels for Windows, macOS, and Linux (no Rust or compiler needed)
- The fastest quadtree Python package (>10x faster than pyqtree)
- Clean Python API with no external dependencies and modern typing hints
- Support for inserting bounding boxes or points
- Fast KNN and range queries
- Optional object tracking for id ↔ object mapping
- Fast serialization to/from bytes
- Support for multiple data types (f32, f64, i32, i64) for coordinates
- 100% test coverage and CI on GitHub Actions
- Offers a drop-in pyqtree shim that is ~6.5x faster while keeping the same API
Examples
See examples of how fastquadtree can be used in the runnables section.
Install
pip install fastquadtree
from fastquadtree import QuadTree # Point handling
from fastquadtree import RectQuadTree # Bounding box handling
from fastquadtree import QuadTreeObjects # Point handling with object tracking
from fastquadtree import RectQuadTreeObjects # Bounding box handling with object tracking
from fastquadtree.pyqtree import Index # Drop-in pyqtree shim (~6.5x faster while keeping the same API)
Benchmarks
fastquadtree outperforms all other quadtree Python packages, including the Rtree spatial index.
Library comparison
Summary (largest dataset, PyQtree baseline)
- Points: 500,000, Queries: 500
- Fastest total: fastquadtree at 0.067 s
| Library | Build (s) | Query (s) | Total (s) | Speed vs PyQtree |
|---|---|---|---|---|
| fastquadtree | 0.063 | 0.026 | 0.089 | 48.99× |
| Shapely STRtree | 0.314 | 0.178 | 0.492 | 8.90× |
| fastquadtree (obj tracking) | 0.388 | 0.244 | 0.632 | 6.93× |
| nontree-QuadTree | 1.180 | 1.287 | 2.467 | 1.77× |
| Rtree | 1.905 | 0.622 | 2.527 | 1.73× |
| e-pyquadtree | 2.083 | 1.479 | 3.562 | 1.23× |
| quads | 3.058 | 1.140 | 4.198 | 1.04× |
| PyQtree | 3.775 | 0.603 | 4.378 | 1.00× |
See the benchmark section for details, including configurations, system info, and native vs shim benchmarks.
Quickstart
API
QuadTree(bounds, capacity, max_depth=None, dtype="f32")
bounds— tuple(min_x, min_y, max_x, max_y)defines the 2D area covered by the quadtreecapacity— max number of points kept in a leaf before splittingmax_depth— optional depth cap. If omitted, the tree can keep splitting as neededdtype— data type for coordinates, e.g.,"f32","f64","i32","i64"
Key Methods
-
insert(xy, id_=None) -> int -
query(rect) -> list[tuple[int, float, float]] -
nearest_neighbor(xy) -> tuple[int, float, float] | None -
delete(id, x, y) -> bool
For object tracking, use QuadTreeObjects instead. See the docs for more methods.
Geometric conventions
- Rectangles are
(min_x, min_y, max_x, max_y). - Containment rule is closed on the min edge and open on the max edge
(x >= min_x and x < max_x and y >= min_y and y < max_y). This only matters for points exactly on edges.
Performance tips
- Choose
capacityso that leaves keep a small batch of points. Typical values are 8 to 64. - If your data is very skewed, set a
max_depthto prevent long chains. - For fastest local runs, use
maturin develop --release. - Use
QuadTreewhen you only need spatial indexing. UseQuadTreeObjectswhen you need to store Python objects with your points. - Refer to the Native vs Shim Benchmark for overhead details.
Pygame Ball Pit Demo
A simple demo of moving objects with collision detection using fastquadtree. You can toggle between quadtree mode and brute-force mode to see the performance difference.
See the runnables guide for setup instructions.
FAQ
Can I delete items from the quadtree?
Yes! Use delete(id, x, y) to remove specific items. You must provide both the ID and exact location for precise deletion. This handles cases where multiple items exist at the same location. If you're using QuadTreeObjects, you can also use delete_by_object(obj) for convenient object-based deletion with O(1) lookup. The tree automatically merges nodes when item counts drop below capacity.
Can I store rectangles or circles?
Yes, you can store rectangles using the RectQuadTree class. Circles can be approximated with bounding boxes. See the RectQuadTree docs for details.
Do I need NumPy installed?
No, NumPy is a fully optional dependency. If you do have NumPy installed, you can use methods such as query_np and insert_many_np for better performance. Note that insert_many raises TypeError on NumPy input—you must use insert_many_np explicitly for NumPy arrays. The Rust core is able to handle NumPy arrays faster than Python lists, so there's a lot of time savings in utilizing the NumPy functions. See the Native vs Shim benchmark for details on how returing NumPy arrays can speed up queries.
# Using Python lists
qt.insert_many([(10, 20), (30, 40), (50, 60)])
# Using NumPy arrays (requires NumPy)
import numpy as np
points = np.array([[10, 20], [30, 40], [50, 60]])
qt.insert_many_np(points) # Use insert_many_np for NumPy arrays
Does fastquadtree support multiprocessing?
Yes, fastquadtree objects can be serialized to bytes using the to_bytes() method and deserialized back using from_bytes(). This allows you to share quadtree data across processes and even cache prebuilt trees to disk. When using QuadTreeObjects or RectQuadTreeObjects, you must pass include_objects=True to to_bytes() to serialize Python objects, and allow_objects=True to from_bytes() when loading. By default, objects are skipped for safety, as deserializing untrusted Python objects can be unsafe. See the interactive v2 demo for an example of saving and loading a quadtree, and the QuadTreeObjects API docs for full details on the serialization methods.
License
MIT. See LICENSE.
Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastquadtree-2.0.0.tar.gz.
File metadata
- Download URL: fastquadtree-2.0.0.tar.gz
- Upload date:
- Size: 866.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a9593b475152ac0495b3efed0b464d439dbfa505c4fad87260df207e8136389
|
|
| MD5 |
0688bbc47ed3769d0c69a598675032d7
|
|
| BLAKE2b-256 |
8e1120a92c9c6dbb31e4bb51912afdc3200cdbcd8e213d940f262fe49e24e75a
|
File details
Details for the file fastquadtree-2.0.0-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: fastquadtree-2.0.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 303.2 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2305a4b81198350b0a903b2fd1188a960af7d9701021361617fd71c5033de41d
|
|
| MD5 |
abe0e431f0b08c901d42811e12225eed
|
|
| BLAKE2b-256 |
eb7fa19276d0cb719b5ea83447a86784bc7c8f6e6d53eef97ae329579cb030a0
|
File details
Details for the file fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 414.1 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9f62a268da0454381904d442fa918a254fe9c9a9ec095aa1b73b374b13b2319
|
|
| MD5 |
06af01e747bdfb64147e4bf18fe10da8
|
|
| BLAKE2b-256 |
690964932ab61b772e03db171a6225029795f920b6fd05dc71a844667e47a536
|
File details
Details for the file fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.
File metadata
- Download URL: fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 442.3 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e67c340db747ca338fa202312f1b516af4f0bbf0145200d8c96a123eaac98b76
|
|
| MD5 |
c92b94eda390fa1f1bf587b300a96b53
|
|
| BLAKE2b-256 |
fef12e454884f94d4921aef26a0e54b3223a3fe3c79b11f38de1a97ae8973d58
|
File details
Details for the file fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: fastquadtree-2.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 403.0 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
969a736f5c32ad200d0f8a8b244a0989e66a80f0a96b72af3a7e019f338d2427
|
|
| MD5 |
1b736f97c4dd21168246a711b31dc767
|
|
| BLAKE2b-256 |
5c2d01be1a8efb2d533e457a1f98d4f8cf2c5d6cc087f95069d4076337404ef8
|
File details
Details for the file fastquadtree-2.0.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.
File metadata
- Download URL: fastquadtree-2.0.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
- Upload date:
- Size: 718.6 kB
- Tags: CPython 3.8+, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4604da26bd54959c318b6d4997925fb676c2b1d9c14bc64d682aa52846eb449c
|
|
| MD5 |
54b4177c0eb512e26344301da26b82ce
|
|
| BLAKE2b-256 |
57ae7f2177f891a416b7ea83eb5e5f5b0ee6c6ef5e997f2bef4c4df73fd2f1a4
|