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 6.791x 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.pyqtree import Index  # Drop-in pyqtree shim (6.791x faster while keeping the same API)

Benchmarks

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

Library comparison

Total time Throughput

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.050 0.017 0.067 44.95×
Shapely STRtree 0.209 0.103 0.312 9.58×
fastquadtree (obj tracking) 0.289 0.178 0.467 6.41×
nontree-QuadTree 0.878 1.030 1.908 1.57×
Rtree 1.742 0.523 2.265 1.32×
e-pyquadtree 1.425 0.975 2.400 1.25×
quads 2.094 0.782 2.876 1.04×
PyQtree 2.607 0.385 2.992 1.00×

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

Quickstart

See the quickstart guide

API

See the full API

QuadTree(bounds, capacity, max_depth=None, track_objects=False, 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
  • track_objects — if True, the wrapper maintains an id → object map for convenience.
  • dtype — data type for coordinates, e.g., "f32", "f64", "i32", "i64"

Key Methods

  • insert(xy, *, obj=None) -> int

  • query(rect, *, as_items=False) -> list

  • nearest_neighbor(xy, *, as_item=False) -> (id, x, y) | Item | None

  • delete(id, xy) -> bool

There are more methods and object tracking versions in the docs.

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.
  • The wrapper maintains an object map only if the quadtree was constructed with track_objects=True. If you don't need it, leave it off for best performance.
  • 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 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, xy) 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 track_objects=True, 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 the NumPy array variant of insert_many for better performance. 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.

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. See the interactive v2 demo for an example of saving and loading a quadtree.

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-1.5.2.tar.gz (871.6 kB view details)

Uploaded Source

Built Distributions

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

fastquadtree-1.5.2-cp38-abi3-win_amd64.whl (277.0 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (386.1 kB view details)

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

fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (418.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (376.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

fastquadtree-1.5.2-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (678.8 kB view details)

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

File details

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

File metadata

  • Download URL: fastquadtree-1.5.2.tar.gz
  • Upload date:
  • Size: 871.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for fastquadtree-1.5.2.tar.gz
Algorithm Hash digest
SHA256 f60f2f1c8a9caaad89120bbeed3ab8c39fb072c5b9ab97c7ab1a371233450207
MD5 77470229907f6ffd0e393c60b6b24d3d
BLAKE2b-256 52360ef80b88335eee1a3bc3b363cd907bc54e453b32eed4c1aba5d46bb201bc

See more details on using hashes here.

File details

Details for the file fastquadtree-1.5.2-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for fastquadtree-1.5.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 243b4433ac0e4917484aeb39f2f9a732c435110fa7d9be5344532c93630bba6e
MD5 5ce66f78ea8ae1cef5b4883452c82312
BLAKE2b-256 004f21fba09670f9bb27ed9cfafb06570d17f1702f1541d64aad6f86f46f1b31

See more details on using hashes here.

File details

Details for the file fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0e14b96db15ba7a9f977888bf9200f2e3626cdef576f4b54855f6a553e86d2e8
MD5 e5513738d55e23d246962510cdb0ebe6
BLAKE2b-256 e49d82702cb65ad708bf69b4a7a430fc9d93de4a18bf7a835675c071e39a2ac0

See more details on using hashes here.

File details

Details for the file fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 59073f738225ffad75fb682a319ae0b9962ba5171879de987e52ec7462125807
MD5 6034804bd6f58699cba87297dcea5856
BLAKE2b-256 40e781abfa99c8e59f9b7c4559fc09471c9ea9894dd4e457f217446b2f5da1d1

See more details on using hashes here.

File details

Details for the file fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for fastquadtree-1.5.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b817ba7aae9270eafd5d1d86717d6f6d2935ded52b9d6d2ea7ff75433ea3b22a
MD5 a54d45aff21c75b6b679c508669b9355
BLAKE2b-256 5cdb781642ed217e2d43af19b237f9de3a01f1b5dd47c623ee69351fd240871c

See more details on using hashes here.

File details

Details for the file fastquadtree-1.5.2-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for fastquadtree-1.5.2-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3b3c761f00e1a9e51aa76111fd2bfaf4ccdd1e487a2796c4a79fbf28c153e099
MD5 116115579148d25db720efb280867bc6
BLAKE2b-256 f46c60edbd29bb38857d3f76447d364bfae552f2e64c9b46aede5c19b5c96695

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