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.5x 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 (~6.5x 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.057 0.021 0.078 54.45×
fastquadtree[^fqt] 0.060 0.189 0.249 17.04×
Shapely STRtree[^npreturn] 0.321 0.196 0.517 8.21×
fastquadtree (obj tracking)[^fqto] 0.437 0.239 0.675 6.28×
Rtree 1.796 0.561 2.357 1.80×
nontree-QuadTree 1.275 1.272 2.547 1.67×
e-pyquadtree 2.144 1.507 3.650 1.16×
quads 3.001 1.171 4.172 1.02×
PyQtree 3.677 0.565 4.242 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.0.4.tar.gz (936.5 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.0.4-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl (438.5 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARMv7l

fastquadtree-2.0.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (391.1 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

fastquadtree-2.0.4-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl (437.3 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARMv7l

fastquadtree-2.0.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (391.0 kB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

fastquadtree-2.0.4-cp38-abi3-win_amd64.whl (304.7 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_x86_64.whl (414.8 kB view details)

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

fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_armv7l.whl (437.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ ARMv7l

fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_aarch64.whl (390.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ ARM64

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

File metadata

  • Download URL: fastquadtree-2.0.4.tar.gz
  • Upload date:
  • Size: 936.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4.tar.gz
Algorithm Hash digest
SHA256 ea13690e693bc82cc2b84f7da6acb6a15e4c0b8db9051a94d344cbce9490dd71
MD5 3f18b5a7c12ba95a742b381a28b12b90
BLAKE2b-256 ab7123bb79589bfabf98d5782575ac4a25d87951d3ebdf7c554ede9cddd6e840

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 438.5 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-pp310-pypy310_pp73-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 5d24387999f8ddacea9538c1a716097148877f5f509939e1e2edd179fa5afbbc
MD5 b69c645f7198d7712e4c0046ab78a182
BLAKE2b-256 5fbe3b0f55c2ece9a79db44abb9c2d2ec39d47c20836ab14eed1c4fb4dfbfceb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 391.1 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 953ddbf1f512acdc4fddba978a5ed677b3d15c19400d6d14ccbcef5702e13481
MD5 53abdb071f791f7a443573ccc96ede96
BLAKE2b-256 35f4409e07c860a81431c0f3c9f8735c465551aba2d56b55db2618d8787eb3cf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 437.3 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-pp39-pypy39_pp73-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 ae87d958fb44762d346c84cac2f359bbf02d4e1b0939f30c6e9e635e9d828eb4
MD5 bc5b0e59be3f67ee3dcdfd0ac0668e48
BLAKE2b-256 735f34991d49e7e60a6180730467908cb27329d1c2c142b2b4abe07a5d2e551d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 391.0 kB
  • Tags: PyPy, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 03ed5fe7da4f4f13a80324fee69fec38c34d26edeb638dc7b975a9985005dd89
MD5 e5fe00fe4e6889570daef8216e02330f
BLAKE2b-256 cfc0c4e2f8452d8d5ef89b25599b9f6b48f7b9cb309f561fdfd0ad58de2a2518

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 304.7 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f09b8232d830cde55053baa9040650ac28c7286e53de93f46daea16d4d883b96
MD5 5ad5cdfce4092f0a4c5e6c7f01bccbcc
BLAKE2b-256 7997ae8aadaf5240221be6641dd9a910d8add1b8e8fb7f14ca58816247503995

See more details on using hashes here.

File details

Details for the file fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 414.8 kB
  • Tags: CPython 3.8+, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-cp38-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5e72a5f1daaef7458add812bf612012262a6c7d53ebbf34777fd6ba80cdba3a2
MD5 98a6b8eb86bbac3dba103b8ca7e869db
BLAKE2b-256 19f5fc37b53f7e1e91ed1090efe2c1a0880be5bb7440d205633de41930d0d6b1

See more details on using hashes here.

File details

Details for the file fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_armv7l.whl.

File metadata

  • Download URL: fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_armv7l.whl
  • Upload date:
  • Size: 437.6 kB
  • Tags: CPython 3.8+, manylinux: glibc 2.28+ ARMv7l
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-cp38-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 ed637ad239ac7cd7c0f7126dd979e1d99a329959e40ce5f7be0b661fa7e77bb5
MD5 48b31f800d9be060fa7a6dce71a4090e
BLAKE2b-256 a959906e1644866f0292d6a85840a5b2e4145c03ccd350bd2e982e7e3b0db602

See more details on using hashes here.

File details

Details for the file fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: fastquadtree-2.0.4-cp38-abi3-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 390.7 kB
  • Tags: CPython 3.8+, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-cp38-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ff26e9895386e149a0cff664321d8272ebd3a87685e5af5da432341ba5639899
MD5 cbb10d2743fa54a100028c8f4c0924a1
BLAKE2b-256 2cac75056540a6a445bbde3ffaf1fe6119baa06129b2c93953595e87a9553b47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastquadtree-2.0.4-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 720.3 kB
  • Tags: CPython 3.8+, 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.25 {"installer":{"name":"uv","version":"0.9.25","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.0.4-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 f011f0610cd015781b2a914584c32612378037091d793dd1e89308660fc9f58a
MD5 80d749822e1aa0c251fb6ad8efa96e18
BLAKE2b-256 0d205b0fabde357d1645c988f117caa4247822f4a6b6a4f2d733bc8d13e92ac4

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