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 |
dtype | str | Data type for coordinates and ids in the native engine. Default is 'f32'. Options are 'f32', 'f64', 'i32', 'i64'. | 'f32' |
Raises:
| Type | Description |
|---|---|
ValueError | If parameters are invalid or inserts are out of bounds. |
to_dict() ¶
Serialize the quadtree to a dict suitable for JSON or other serialization.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Includes a binary 'core' key for the native engine state, plus other metadata such as bounds and capacity and the obj store if tracking is enabled. |
to_bytes() ¶
from_bytes(data, dtype='f32') classmethod ¶
Deserialize a quadtree from bytes. Specifiy the dtype if the original tree that was serialized used a non-default dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bytes | Bytes representing the serialized quadtree from | required |
dtype | str | The data type used in the native engine ('f32', 'f64', 'i32', 'i64') when saved to bytes. | 'f32' |
Returns:
| Type | Description |
|---|---|
Self | A new quadtree instance with the same state as when serialized. |
insert(geom, *, obj=None) ¶
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 |
obj | Any | None | Optional Python object to associate with id if tracking is enabled. | None |
Returns:
| Type | Description |
|---|---|
int | The id used for this insert. |
Raises:
| Type | Description |
|---|---|
ValueError | If geometry is outside the tree bounds. |
insert_many(geoms, objs=None) ¶
Bulk insert with auto-assigned contiguous ids. Faster than inserting one-by-one.
Can accept either a Python sequence of geometries or a NumPy array of shape (N,2) or (N,4) with a dtype that matches the quadtree's dtype.
If tracking is enabled, the objects will be bulk stored internally. If no objects are provided, the items will have obj=None (if tracking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geoms | NDArray[Any] | Sequence[G] | List of geometries. | required |
objs | list[Any] | None | Optional list of Python objects aligned with geoms. | None |
Returns:
| Type | Description |
|---|---|
int | Number of items inserted. |
Raises:
| Type | Description |
|---|---|
ValueError | If any geometry is outside bounds. |
delete(id_, geom) ¶
Delete an item by id and exact geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_ | int | The id of the item to delete. | required |
geom | G | The geometry of the item to delete. | required |
Returns:
| Type | Description |
|---|---|
bool | True if the item was found and deleted. |
attach(id_, obj) ¶
Attach or replace the Python object for an existing id. Tracking must be enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_ | int | The id of the item to attach the object to. | required |
obj | Any | The Python object to attach. | required |
delete_by_object(obj) ¶
clear() ¶
get_all_objects() ¶
get_all_items() ¶
get_all_node_boundaries() ¶
get(id_) ¶
count_items() ¶
get_inner_max_depth() ¶
query(rect, *, as_items=False) ¶
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 |
|---|---|
list[_IdRect] | list[RectItem] | If as_items is False: list of (id, x0, y0, x1, y1) tuples. |
list[_IdRect] | list[RectItem] | If as_items is True: list of Item objects. |
query_np(rect) ¶
Return all points inside an axis-aligned rectangle as NumPy arrays. The first array is an array of IDs, and the second is a corresponding array of rectangle coordinates.
Requirements
NumPy must be installed to use this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rect | Bounds | Query rectangle as (min_x, min_y, max_x, max_y). | required |
Returns:
| Type | Description |
|---|---|
tuple[Any, Any] | Tuple of (ids, locations) where: ids: NDArray[np.uint64] with shape (N,) locations: NDArray[np.floating] with shape (N, 4) |