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

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 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)

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 (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.

Quickstart

See the quickstart guide

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 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, 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.1.tar.gz (836.7 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.1-cp38-abi3-win_amd64.whl (303.4 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (414.6 kB view details)

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

fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (443.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (403.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

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

File metadata

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

File hashes

Hashes for fastquadtree-2.0.1.tar.gz
Algorithm Hash digest
SHA256 fb014b9a2261ad448fd1443944faee21a59e4e8a438b5f575bafd4d5efd2d430
MD5 fc572c8764c7b7c317822f3380aa24ae
BLAKE2b-256 464defc7b5fb735fbe93d50c27f88ee24a51b049f02ae598b868337b054e2a6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-2.0.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 37c87f18989679afdaaf0621419a552a8dd82331cc2c2377d5cfe2172e5ef4e9
MD5 a2d2d2495c39ee6ca2fb91a3305f2b23
BLAKE2b-256 efe5bbad751cf00f6dff6043375ace3463294566bd0c39d5b76f371abc2f2b82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 608864b708d46fa9849652c2c72fc063f07629c4fdbfe9af123ad8ceab42ac75
MD5 d4fe7485b4b6e002f98b91cfe1d06cca
BLAKE2b-256 c00b5aa8d27ef7f6ec9faa6ceb54ed0a1caa247168ccf414ed63de310056052f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 0d2f3873e115b23efd3bcac1071d7c259254b7d69cd888383e654bcd01207aad
MD5 6fbbc0be86363f8ebb6fc7e1ae93eb42
BLAKE2b-256 abcd6401c026c90fd57440c018f6a55ecda350a38efcec3d045345a22b893eab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-2.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 398e692e3efef667607014363b93dbc72e6ded18195e72942dde53a729524fb8
MD5 c0a677cb5d2dc57dbaf81cb58ccf2095
BLAKE2b-256 db6097d479ad6991436c53979fe7ee9dc4a8422760b8f857c7186a37341b88bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastquadtree-2.0.1-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 2b83715570ee7f3a65c7251dcfbecc717536645078a57e672e77bb6d2cf6b7a9
MD5 a193c7824ef44b40e3c8289b1e17e41a
BLAKE2b-256 0fe57916d89e9fbdf11e30058daeab857399dfd645aee4b4ab647241cca8a7f8

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