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

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
  • 100% test coverage and CI on GitHub Actions
  • Offers a drop-in pyqtree shim that is 6.567x faster while keeping the same API

👉 Docs: https://elan456.github.io/fastquadtree/

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.120 s
Library Build (s) Query (s) Total (s) Speed vs PyQtree
fastquadtree 0.031 0.089 0.120 14.64×
Shapely STRtree 0.179 0.100 0.279 6.29×
nontree-QuadTree 0.595 0.605 1.200 1.46×
Rtree 0.961 0.300 1.261 1.39×
e-pyquadtree 1.005 0.660 1.665 1.05×
PyQtree 1.492 0.263 1.755 1.00×
quads 1.407 0.484 1.890 0.93×

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.1.0.tar.gz (799.9 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.1.0-cp38-abi3-win_amd64.whl (158.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (259.9 kB view details)

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

fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (269.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (258.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

fastquadtree-1.1.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (452.7 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.1.0.tar.gz.

File metadata

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

File hashes

Hashes for fastquadtree-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b3fa145e08bae8fda44648e361ded921e2a6d01d784713d06025e60327a05d9e
MD5 e8a800051102d4193ed8446865aab3f0
BLAKE2b-256 ece19e4ce6d38a26977c4ce379484843b1482ac190ee26ea48646c8357fb9913

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.1.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5b63bdb6f7e34e7c1bf03f77321ffd0b6f46f967d785bc057ac1ca63314e6463
MD5 db1fc4e149ed8d280fb8d430ad67f692
BLAKE2b-256 354191a9f3f5cd5a634942ab84db2c627654fb45d464e6d122cfcde5613500f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab56ad2e2afb357218b814593944da9d255e6bb79461441ac42868fc2a9be5dd
MD5 72631ade54c355703ad2f253213380e5
BLAKE2b-256 c32f2a2450e834269b67901df086af5e7809a8130d476325d650ec520a9dec1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 233945fd5b61fe1e959e3ccd306b60c26c0cd9bacb5ca932e9a87f23dbc61887
MD5 2c8ba6147e205c6a2e2972c16d8c8578
BLAKE2b-256 5a51716e1706031a70890d62cdd4aa2b1c5bec0576d10fed574d095857d4f794

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6a125631af26abb1a18cd6d07c3a6049b8943ebe930d9b6e17dd3c77aa1aaf5d
MD5 7bab6c52e68ac3c67d97b9f25cacc035
BLAKE2b-256 c562b16b1dda9ad56980ff9b22893ab5085ac13625c775c5bc98eaf064ac56f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-1.1.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 a05774f4db78c0f299ebcc5d53b760bf63eeb8a0feccc7d0a4a6f1648d9188e3
MD5 9769589c91cb7ee89cab925c91cb82a8
BLAKE2b-256 78949bd94d9e284f0073481a8b8ee0455dfcfbc975c7e45f313ac12145637fae

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