Spatial Operations
This module provides spatial operations for vector data, including attribute transfer, treetop labeling, and crown delineation.
assign_tree_ids_to_crowns(crowns, trees)
Executes a spatial intersection to map master tree anchor IDs to corresponding crown polygons. Enforces a strict one-to-one relationship by retaining only the first intersecting anchor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crowns
|
Vector
|
The vector layer containing the unmapped crown polygons. |
required |
trees
|
Vector
|
The master tree points containing the recognized database identities. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Vector |
Vector
|
A filtered vector containing polygons paired with their unique tree IDs. |
Source code in src/phytospatial/vector/spatial_operations.py
calculate_centroid(vector)
Computes the geometric center of each geometry in the provided vector layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
Vector
|
The input vector layer containing polygonal geometries. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Vector |
Vector
|
A new vector object containing Point geometries at the centroids. |
Source code in src/phytospatial/vector/spatial_operations.py
label_tree_crowns(target_vector, source_points, label_col, max_dist=2.0)
Labels tree crowns in the target vector by transferring species information from nearby source points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_vector
|
Vector
|
The Vector containing tree crown geometries to be labeled. |
required |
source_points
|
Vector
|
The Vector containing point geometries with species information to transfer. |
required |
label_col
|
str
|
The name of the column in the source points that contains the species labels to transfer. |
required |
max_dist
|
float
|
The maximum distance for spatial proximity when transferring labels. Defaults to 2.0 units. |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Vector |
Vector
|
A new Vector with tree crowns labeled with species information from nearby source points. |
Source code in src/phytospatial/vector/spatial_operations.py
prepare_itcd_vectors(vector, id_col=None, species_col=None, do_validate=True, fix_invalid=True)
Prepares a Vector for ITCD processing by ensuring required columns, validating geometries, and standardizing formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
Vector
|
The input Vector to prepare. |
required |
id_col
|
Optional[str]
|
The name of the column to use as the unique identifier for tree crowns. If None, a new 'crown_id' column will be created using the index. Defaults to None. |
None
|
species_col
|
Optional[str]
|
The name of the column containing species information. If None, a new 'species' column will be created with null values. Defaults to None. |
None
|
do_validate
|
bool
|
Whether to validate geometries and optionally fix or drop invalid ones. Defaults to True. |
True
|
fix_invalid
|
bool
|
If do_validate is True, whether to attempt fixing invalid geometries before dropping them. Defaults to True. |
True
|
Returns: A prepared Vector with standardized columns and validated geometries, ready for ITCD processing.
Source code in src/phytospatial/vector/spatial_operations.py
prepare_treetop_vectors(vector, column_mapping=None, default_status='Alive')
Standardizes treetop anchor point data schemas prior to database ingestion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
Vector
|
The input point Vector object. |
required |
column_mapping
|
Optional[Dict[str, str]]
|
A dictionary mapping native source attributes to the required 'tree_id' and 'species' schema columns. |
None
|
default_status
|
str
|
The default string value to inject for the biological or management status of the tree. Defaults to "Alive". |
'Alive'
|
Returns:
| Name | Type | Description |
|---|---|---|
Vector |
Vector
|
A sanitized Vector object containing the precise schema required by the persistent relational data layer. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the resulting DataFrame lacks 'tree_id' or 'species' attributes. |