Spatial Operations#
Spatial indexing (KD-tree, STRtree), point-in-face queries, mesh clipping by polygon, and bounding box subsetting.
pyramids.netcdf.ugrid.MeshSpatialIndex
#
Lazy-built spatial index for mesh elements.
Uses scipy.spatial.cKDTree for nearest-neighbor queries and shapely.STRtree for point-in-polygon queries. Both indexes are built on demand and cached.
Attributes:
| Name | Type | Description |
|---|---|---|
_mesh |
Reference to the Mesh2d topology. |
|
_node_tree |
Any
|
KD-tree on node coordinates (lazy). |
_face_tree |
Any
|
KD-tree on face centroids (lazy). |
_face_strtree |
Any
|
STRtree on face polygons (lazy). |
_face_polygons |
list[Any] | None
|
List of Shapely polygons (lazy). |
Source code in src/pyramids/netcdf/ugrid/spatial.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
node_tree
property
#
KD-tree on node coordinates. Lazy-built on first access.
face_tree
property
#
KD-tree on face centroids. Lazy-built on first access.
face_strtree
property
#
Shapely STRtree on face polygons. Lazy-built on first access.
face_polygons
property
#
List of Shapely Polygon objects for all faces.
locate_nearest_node(x, y, k=1)
#
Find k nearest nodes to query point(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float | ndarray
|
Query x-coordinate(s) (scalar or array). |
required |
y
|
float | ndarray
|
Query y-coordinate(s) (scalar or array). |
required |
k
|
int
|
Number of nearest neighbors to find. |
1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of node indices. Shape: (k,) for scalar input, |
ndarray
|
(n_queries, k) for array input. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
locate_nearest_face(x, y, k=1)
#
Find k nearest face centroids to query point(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float | ndarray
|
Query x-coordinate(s) (scalar or array). |
required |
y
|
float | ndarray
|
Query y-coordinate(s) (scalar or array). |
required |
k
|
int
|
Number of nearest neighbors to find. |
1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of face indices. Shape: (k,) for scalar input, |
ndarray
|
(n_queries, k) for array input. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
locate_nodes_in_bounds(xmin, ymin, xmax, ymax)
#
Find all nodes within a bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmin
|
float
|
Minimum x-coordinate. |
required |
ymin
|
float
|
Minimum y-coordinate. |
required |
xmax
|
float
|
Maximum x-coordinate. |
required |
ymax
|
float
|
Maximum y-coordinate. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of node indices within the bounding box. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
locate_faces_in_bounds(xmin, ymin, xmax, ymax)
#
Find all faces whose centroids fall within a bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmin
|
float
|
Minimum x-coordinate. |
required |
ymin
|
float
|
Minimum y-coordinate. |
required |
xmax
|
float
|
Maximum x-coordinate. |
required |
ymax
|
float
|
Maximum y-coordinate. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of face indices within the bounding box. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
locate_faces(x, y)
#
Find which face contains each query point.
Uses Shapely STRtree for exact containment testing. Returns -1 for points outside all faces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Query x-coordinates (array). |
required |
y
|
ndarray
|
Query y-coordinates (array). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of face indices, -1 for points outside mesh. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
pyramids.netcdf.ugrid.spatial.clip_mesh(dataset, mask, touch=True)
#
Clip a UGRID dataset to a polygon mask.
Selects faces that intersect (touch=True) or are fully contained within (touch=False) the mask polygon. Renumbers nodes and edges to produce a compact, self-consistent mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Any
|
Source UgridDataset. |
required |
mask
|
Any
|
Polygon mask (GeoDataFrame, FeatureCollection, or Shapely geometry). |
required |
touch
|
bool
|
If True, include faces that touch the mask boundary. If False, only include faces fully inside. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
New UgridDataset with clipped mesh and subset data. |
Source code in src/pyramids/netcdf/ugrid/spatial.py
pyramids.netcdf.ugrid.spatial.subset_by_bounds(dataset, xmin, ymin, xmax, ymax)
#
Subset mesh to faces whose centroids fall within a bounding box.
Faster than clip_mesh because it only checks face centroids against the box without building Shapely polygons or doing intersection tests. Uses vectorized numpy comparisons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Any
|
Source UgridDataset. |
required |
xmin
|
float
|
Minimum x-coordinate. |
required |
ymin
|
float
|
Minimum y-coordinate. |
required |
xmax
|
float
|
Maximum x-coordinate. |
required |
ymax
|
float
|
Maximum y-coordinate. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
New UgridDataset with faces whose centroids are within the box. |