Skip to main content

Rust-accelerated quadtree for Python with fast inserts, range queries, and k-NN search.

Project description

fastquadtree

Interactive Screenshot

Rust-optimized quadtree with a clean Python API

👉 Check out the Docs: https://elan456.github.io/fastquadtree/

PyPI Python versions Downloads Build No runtime deps

PyO3 maturin Ruff

Docs Wheels Coverage License: MIT


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 ~10x faster while keeping the same API

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 (~10x faster while keeping the same API)

Quickstart

from fastquadtree import QuadTree

qt = QuadTree((0, 0, 1000, 1000), 16)  # bounds and capacity
qt.insert((100, 200), id_=1)  # insert point with ID 1
print(qt.query((0, 0, 500, 500)))  # gets all points in that area: [(1, 100.0, 200.0)]

See the quickstart guide or the interactive demos for more details.

Benchmarks

fastquadtree outperforms all other quadtree Python packages, including the Rtree spatial index.

Library comparison

Total time Throughput

Summary (PyQtree baseline, sorted by total time)

  • Points: 500,000, Queries: 500
Library Build (s) Query (s) Total (s) Speed vs PyQtree
fastquadtree (np)[^fqtnp] 0.052 0.017 0.068 42.52×
fastquadtree[^fqt] 0.054 0.231 0.285 10.20×
Shapely STRtree[^npreturn] 0.200 0.110 0.309 9.40×
fastquadtree (obj tracking)[^fqto] 0.263 0.093 0.356 8.17×
nontree-QuadTree 0.826 0.844 1.670 1.74×
Rtree 1.805 0.546 2.351 1.24×
e-pyquadtree 1.530 0.941 2.471 1.18×
quads 1.907 0.759 2.667 1.09×
PyQtree 2.495 0.414 2.909 1.00×

[^fqtnp]: Uses query_np for Numpy array return values rather than Python lists.
[^fqt]: Uses standard query method returning Python lists.
[^npreturn]: Uses Shapely STRtree with Numpy array points and returns.
[^fqto]: Uses QuadTreeObjects with object association.

See the benchmark section for details, including configurations, system info, and native vs shim benchmarks.

API

See the full 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 quadtree
  • capacity — max number of points kept in a leaf before splitting
  • max_depth — optional depth cap. If omitted, the tree can keep splitting as needed
  • dtype — 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 capacity so that leaves keep a small batch of points. Typical values are 8 to 64.
  • If your data is very skewed, set a max_depth to prevent long chains.
  • For fastest local runs, use maturin develop --release.
  • Use QuadTree when you only need spatial indexing. Use QuadTreeObjects when you need to store Python objects with your points.
  • Refer to the Native vs Shim Benchmark for overhead details.

Pygame Ball Pit Demo

Ballpit_Demo_Screenshot

A simple demo of moving objects with collision detection using fastquadtree. You can toggle between fastquadtree, pyqtree, and brute-force mode to see the performance difference. I typically see an FPS of ~70 with fastquadtree, ~25 with pyqtree, and <1 FPS with brute-force on my machine with 1500 balls.

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


Download files

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

Source Distribution

fastquadtree-2.1.1.tar.gz (910.2 kB view details)

Uploaded Source

Built Distributions

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

fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl (445.2 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARMv7l

fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (397.2 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl (443.5 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARMv7l

fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (396.7 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

fastquadtree-2.1.1-cp39-abi3-win_amd64.whl (310.7 kB view details)

Uploaded CPython 3.9+Windows x86-64

fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_x86_64.whl (422.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_armv7l.whl (445.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARMv7l

fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_aarch64.whl (397.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

fastquadtree-2.1.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (737.0 kB view details)

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

File details

Details for the file fastquadtree-2.1.1.tar.gz.

File metadata

  • Download URL: fastquadtree-2.1.1.tar.gz
  • Upload date:
  • Size: 910.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1.tar.gz
Algorithm Hash digest
SHA256 93e89ea1faa60a3632179d5c72a89e7f9620568432a7149ee6a9211e76c19382
MD5 3779c4661cf8c305715d47aed0207cdd
BLAKE2b-256 5701561421dcc185bce07c5152350772e46842d95d329b27833bd1f84841af28

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 445.2 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 e7cb2ad105293e0696de469d000f1ec6b5f7447f56b7dde8f4931361d5989e11
MD5 b90415f91de13b2b451b6f4595893c70
BLAKE2b-256 36dcbee06920d615536d0a91b33edc6a2bcc51345ca19846715ddbba6b251c72

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 397.2 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fc83ef261b43768084ad3bd682a0d0c53e376661e4c2252bfdbac8b27c5171f8
MD5 a6de2e4620ac6057a63e8cbb4e2366e9
BLAKE2b-256 43171adbe2b7816bde791955ebef22deb1f827cedd4cb9fe752de248da32f221

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 443.5 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 bb66ce00a023b46bc202a705cb31a4ed6a8f4611e73d57dcb49269ce2dbb30f9
MD5 5beac816c46e49ba400b450ea95f8e2b
BLAKE2b-256 50eb5ba9b0139cfe29baafc80e93657e0557640e896e65c69edcb123622f6bac

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 396.7 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bdbbf2d7a4b15f820d446185fe74e84111fb3c5bf1d425edb39e5abb83a4168d
MD5 6e320aca577b489afdaf121169017b43
BLAKE2b-256 6fb000611dc8362fafba904558b9ae8d8c429603cbe49e9c660eee9db60b6c1f

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 310.7 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 90bda4f1f3f74d0bbefd37d81f27f16a9ea16e1313fad860bd0de14e0ab183d9
MD5 4a0a0fa335eb62ba08f548573e8d36a7
BLAKE2b-256 85ac8d84d2a75ee8c244d27c36c2959d21beb29a1340c83b1b5981d3fa7f58d4

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 422.7 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 65dff748eff1ecf7e81e5f9e9e0a89fcf787ca667e2fbcdcfeeeff129d0981e5
MD5 12d3dafecb21f6cec28f64c5c59b13c5
BLAKE2b-256 bc5cc9263654394af4f0843fe092195c9bd4db1bdfb40797fa8244cf2e95273d

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_armv7l.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 445.1 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 30892a8de067967a3de766e098f3ee1af462304181f106899ef678aa6e646cd3
MD5 876906870139a9d7610d6e2066acc454
BLAKE2b-256 4b3a1b88b5cad06b966462dcab292a8fe184c177ec2471631b829bf7b6364196

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 397.6 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 983dfc7eb08f0ee8fa7123b6dd7ba96a470cfd772e4ef75829bba74651c7098c
MD5 ebfcfbad4d4b0e4dc117662aa0e278a2
BLAKE2b-256 6059e8e2af051d06493e1ae34e21d901795173b8e56ec480a21e708ad409d0f2

See more details on using hashes here.

File details

Details for the file fastquadtree-2.1.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

  • Download URL: fastquadtree-2.1.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 737.0 kB
  • Tags: CPython 3.9+, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastquadtree-2.1.1-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 764ef73d54b9a434bb51afd1dc06c3818a8a160ef2d4451e169827f6d126d1b0
MD5 ec1ee6bd99ba692e2cf614c1217f611b
BLAKE2b-256 483bdfd3f63a84245be2bfabf54d7c646945830767ff949cd71ada4083889fac

See more details on using hashes here.

Supported by

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