fastquadtree.RectQuadTree¶
Bases: _BaseQuadTree[Bounds, _IdRect, RectItem]
Rectangle version of the quadtree. All geometries are axis-aligned rectangles. (min_x, min_y, max_x, max_y) High-level Python wrapper over the Rust quadtree engine.
Performance characteristics
Inserts: average O(log n)
Rect queries: average O(log n + k) where k is matches returned
Nearest neighbor: average O(log n)
Thread-safety
Instances are not thread-safe. Use external synchronization if you mutate the same tree from multiple threads.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bounds | Bounds | World bounds as (min_x, min_y, max_x, max_y). | required |
capacity | int | Max number of points per node before splitting. | required |
max_depth | int | None | Optional max tree depth. If omitted, engine decides. | None |
track_objects | bool | Enable id <-> object mapping inside Python. | False |
start_id | int | Starting auto-assigned id when you omit id on insert. | 1 |
Raises:
Type | Description |
---|---|
ValueError | If parameters are invalid or inserts are out of bounds. |
Source code in pysrc/fastquadtree/rect_quadtree.py
__slots__ class-attribute
instance-attribute
¶
__slots__ = (
"_bounds",
"_capacity",
"_count",
"_items",
"_max_depth",
"_native",
"_next_id",
"_track_objects",
)
__len__ ¶
attach ¶
Attach or replace the Python object for an existing id. Tracking must be enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_ | int | Target id. | required |
obj | Any | Object to associate with id. | required |
Source code in pysrc/fastquadtree/_base_quadtree.py
clear ¶
Empty the tree in place, preserving bounds/capacity/max_depth.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_ids | bool | If True, restart auto-assigned ids from 1. | False |
Source code in pysrc/fastquadtree/_base_quadtree.py
count_items ¶
Return the number of items currently in the tree.
Note
Performs a full scan of tree to count up every item. Use the len()
function or len(tree)
for O(1) access.
Returns:
Type | Description |
---|---|
int | Number of items in the tree. |
Source code in pysrc/fastquadtree/_base_quadtree.py
delete ¶
Delete an item by id and exact geometry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_ | int | Integer id to remove. | required |
geom | G | Exact geometry to remove. Either Point (x, y) or Rect (x0, y0, x1, y1) depending on quadtree type. | required |
Returns:
Type | Description |
---|---|
bool | True if the item was found and deleted, else False. |
Source code in pysrc/fastquadtree/_base_quadtree.py
delete_by_object ¶
Delete an item by Python object.
Requires object tracking to be enabled. Performs an O(1) reverse lookup to get the id, then deletes that entry at the given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj | Any | The tracked Python object to remove. | required |
Returns:
Type | Description |
---|---|
bool | True if the item was found and deleted, else False. |
Raises:
Type | Description |
---|---|
ValueError | If object tracking is disabled. |
Source code in pysrc/fastquadtree/_base_quadtree.py
get ¶
Return the object associated with id, if tracking is enabled.
Source code in pysrc/fastquadtree/_base_quadtree.py
get_all_items ¶
Return all Item wrappers in the tree.
Returns:
Type | Description |
---|---|
list[ItemType] | List of Item objects. |
Raises: ValueError: If object tracking is disabled.
Source code in pysrc/fastquadtree/_base_quadtree.py
get_all_node_boundaries ¶
Return all node boundaries in the tree. Great for visualizing the tree structure.
Returns:
Type | Description |
---|---|
list[Bounds] | List of (min_x, min_y, max_x, max_y) for each node in the tree. |
Source code in pysrc/fastquadtree/_base_quadtree.py
get_all_objects ¶
Return all tracked Python objects in the tree.
Returns:
Type | Description |
---|---|
list[Any] | List of objects. |
Raises: ValueError: If object tracking is disabled.
Source code in pysrc/fastquadtree/_base_quadtree.py
insert ¶
Insert a single item
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geom | G | Point (x, y) or Rect (x0, y0, x1, y1) depending on quadtree type. | required |
id_ | int | None | Optional integer id. If None, an auto id is assigned. | None |
obj | Any | Optional Python object to associate with id. Stored only if object tracking is enabled. | None |
Returns:
Type | Description |
---|---|
int | The id used for this insert. |
Raises:
Type | Description |
---|---|
ValueError | If the point is outside tree bounds. |
Source code in pysrc/fastquadtree/_base_quadtree.py
insert_many ¶
Bulk insert items with auto-assigned ids. Faster than inserting one at a time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geoms | list[G] | List of geometries. Either Points (x, y) or Rects (x0, y0, x1, y1) depending on quadtree type. | required |
Returns:
Type | Description |
---|---|
int | The number of items inserted |
Source code in pysrc/fastquadtree/_base_quadtree.py
query ¶
Query the tree for all items that intersect the given rectangle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rect | Bounds | Query rectangle as (min_x, min_y, max_x, max_y). | required |
as_items | bool | If True, return Item wrappers. If False, return raw tuples. | False |
Returns:
Type | Description |
---|---|
If as_items is False: list of (id, x0, y0, x1, y1) tuples. | |
If as_items is True: list of Item objects. |