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

  • Clean Python API with no external dependencies and modern typing hints
  • The fastest quadtree Python package (>10x faster than pyqtree)
  • Prebuilt wheels for Windows, macOS, and Linux
  • 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.567x 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.567x 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: 250,000, Queries: 500
  • Fastest total: fastquadtree at 0.030 s
Library Build (s) Query (s) Total (s) Speed vs PyQtree
fastquadtree 0.023 0.007 0.030 41.31×
Shapely STRtree 0.094 0.049 0.143 8.67×
nontree-QuadTree 0.448 0.475 0.924 1.34×
Rtree 0.801 0.225 1.025 1.21×
e-pyquadtree 0.666 0.451 1.117 1.11×
PyQtree 1.066 0.175 1.241 1.00×
quads 0.969 0.330 1.299 0.96×

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, start_id=1)

  • 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.
  • start_id — starting value for auto-assigned ids

Key Methods

  • insert(xy, *, id=None, 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

What happens if I insert the same id more than once? Allowed. For k-nearest, duplicates are de-duplicated by id. For range queries you will see every inserted point.

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.

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.4.2.tar.gz (800.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.4.2-cp38-abi3-win_amd64.whl (259.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (365.2 kB view details)

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

fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (395.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (351.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

fastquadtree-1.4.2-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (639.4 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.4.2.tar.gz.

File metadata

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

File hashes

Hashes for fastquadtree-1.4.2.tar.gz
Algorithm Hash digest
SHA256 328adea4935ad00686030e75d567ecb5cd78c77f2f561550e764a5bb8abe0236
MD5 58d0305fc38ac3736e58fbe116a7904f
BLAKE2b-256 d87f2ad690ed97731319656dfe1fba9dbf560caf3be9dbfb252e3c16315de2f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.4.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 076a5112b1644187fa9ee3ce5835c8277da0d59eaaf22090c9e2d9822ac28db2
MD5 0f12261b3c80f375717bc51ae5af895e
BLAKE2b-256 96ee7daf673b1860ee97efc13270dc4d749f51235841df11f046bb8c0b3bfc0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c055c2e22de5b011c4c1c4fd14b0466ae59668786841616dd2e9fd3fccc682d5
MD5 1ad1a4a2fca50eea48c955bf4a990951
BLAKE2b-256 271db95bb0fa1475efb5b4ccbe8749500c4fdb32525cbbd08acb93f96a084991

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 e54ede7d9f14025a2122ef4f0f01d7cfba17ad5c017201c49fc15bf2836889ff
MD5 a9825edc39bf6a223545fa6c472be32b
BLAKE2b-256 dffceed17cbd43c44b44a027f291405c9d5cd40a48fea653417ef9c242bdfad5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.4.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 38060e525364e767fb796a7bd8d6ff29e70b02bdf850a44a140a81ae92e06d7a
MD5 11f347d4a8591fca6823d5d48995a621
BLAKE2b-256 64b98e6b09ac04e3d4fe68ff558bdf2983c12f2327d109d75e5455b10ddd5c0a

See more details on using hashes here.

File details

Details for the file fastquadtree-1.4.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.4.2-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 8fc94022a93ac3915de40f0bf1d4e49cdd04246eab7e3f0af1010cd1241d6f0b
MD5 7cfbd44c6d2458c0b3bd1fb313add7d7
BLAKE2b-256 13ab6d494ac69913b781e6c6e615f43665e95b077777d41f4f0a4e40ee2534b0

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