Skip to main content

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

Project description

fastquadtree

Docs PyPI version Python versions Wheels License: MIT

PyPI Downloads

Build Codecov

Rust core via PyO3 Built with maturin Ruff

Interactive_V2_Screenshot

Rust-optimized quadtree with a simple Python API.

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

  • Python package: fastquadtree
  • Python ≥ 3.8
  • Import path: from fastquadtree import QuadTree

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×

Benchmark Configuration

Parameter Value
Bounds (0, 0, 1000, 1000)
Max points per node 128
Max depth 16
Queries per experiment 500

See the benchmark section for details.

Install

pip install fastquadtree

If you are developing locally:

# optimized dev install
maturin develop --release

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

Uploaded Source

Built Distributions

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

fastquadtree-0.8.0-cp38-abi3-win_amd64.whl (142.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (238.7 kB view details)

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

fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (248.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (238.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

fastquadtree-0.8.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (414.0 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-0.8.0.tar.gz.

File metadata

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

File hashes

Hashes for fastquadtree-0.8.0.tar.gz
Algorithm Hash digest
SHA256 b88cd55c9807fa4210b21f314cb5412a6f0643a8c6b0812d342da75bad0215cd
MD5 206f88a7b60e933959978b17ba5e8b08
BLAKE2b-256 e5072ef3f50bd32f5b0e7971f5d7ca0cb84b922f7f044bbe88de47d4ae7327c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-0.8.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f9c0eddfbe1fe045fc1c8e43abe459345283fc75499bce11d005be21b8d3142e
MD5 9713ca907bc3fae7949dc637658218b3
BLAKE2b-256 857744ba2d49103f39e8a225f9a22c7b78e1749d0468e60529b18833a0f1625d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a70e9de9c31a68e8b72e9ac9520bf24997a38650c3ce689d0c0ce9aadad8a053
MD5 6ff4511a1f6765dd89098187a677cf8e
BLAKE2b-256 93e6596470caa88241a2bd48c37bff1af3d6e0c078f17dea2bc42f8b56b16ca1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 912b26f95f8da96fc5b8d5b593a1050f17df18894a468b13ce5b7d9c142dc6a6
MD5 151537d9aa1bb9f3d116bbdb721681a4
BLAKE2b-256 509d70df69a84e96bbfd99ed52630cf2541a2d0177be9bcab39531a7d35b9a01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-0.8.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9d4b5683ae8aaf8c0ab658f8fa2ec7e78a9e9ff8f38e6b3ab7bae766290c2cb4
MD5 0beeabb12d3cd44fb9f5c3e99002a853
BLAKE2b-256 1ec95b710d60b4e14a539b7d9735bf42cc9ff9d8f4ed6cb80e81b36fd07a294c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-0.8.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 de2bb090325663006d5041a2e04a5c279de317fbf353df93cee83e9d9a7e3ca2
MD5 21570c010e8797060aa207e0839cd6c3
BLAKE2b-256 58b2ae38034dedfb65b421dec116ce19051345e32cabd32b5d1b79137aff7dce

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