Calculate Module¶
assign_voxels(arr, voxel_resolution)
¶
Assigns voxel grids to spatial data points based on the specified resolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
Input array-like object containing point cloud data with 'X', 'Y', and 'HeightAboveGround' fields. |
required |
voxel_resolution
|
tuple of floats
|
The resolution for x, y, and z dimensions of the voxel grid. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, List]
|
tuple of (numpy.ndarray, List): A tuple containing the histogram of the voxel grid (with corrected orientation) and the extent of the point cloud. |
Source code in pyforestscan/calculate.py
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 | |
calculate_canopy_cover(pad, voxel_height, min_height=2.0, max_height=None, k=0.5)
¶
Calculate GEDI-style canopy cover at a height threshold using PAD.
Uses the Beer–Lambert relation: Cover(z) = 1 - exp(-k * PAI_above(z)), where PAI_above(z) is the integrated Plant Area Index above height z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
ndarray
|
3D array of PAD values with shape (X, Y, Z). |
required |
voxel_height
|
float
|
Height of each voxel in meters (> 0). |
required |
min_height
|
float
|
Height-above-ground threshold z (in meters) at which to compute canopy cover. Defaults to 2.0 m (GEDI convention). |
2.0
|
max_height
|
float or None
|
Maximum height to integrate up to. If None, integrates to the top of the PAD volume. Defaults to None. |
None
|
k
|
float
|
Extinction coefficient (Beer–Lambert constant). Defaults to 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 2D array (X, Y) of canopy cover values in [0, 1], with NaN where PAD is entirely missing for the integration range. If the requested integration range is empty (e.g., min_height >= available max height), returns a zeros array (no canopy above the threshold). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid (e.g., non-positive voxel_height, k < 0, or min_height >= max_height). |
Source code in pyforestscan/calculate.py
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
calculate_chm(arr, voxel_resolution, interpolation='linear', interp_valid_region=False, interp_clean_edges=False)
¶
Calculate the Canopy Height Model (CHM) for a given voxel grid.
The CHM is computed as the maximum 'HeightAboveGround' value within each (X, Y) voxel. Optionally, gaps in the CHM can be filled using interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
Input structured numpy array containing point cloud data with fields 'X', 'Y', and 'HeightAboveGround'. |
required |
voxel_resolution
|
tuple of float
|
The resolution for the X and Y dimensions of the voxel grid, specified as (x_resolution, y_resolution). |
required |
interpolation
|
str or None
|
Method for interpolating gaps in the CHM. Supported methods are "nearest", "linear", "cubic", or None. If None, no interpolation is performed. Defaults to "linear". |
'linear'
|
interp_valid_region
|
bool
|
Whether to calculate a valid region mask using morphological operations for
interpolation. If True, interpolation is only applied within the valid data region. If False (default),
interpolation is applied to all NaN values. Ignored if |
False
|
interp_clean_edges
|
bool
|
Whether to clean edge fringes of the interpolated CHM. Default is False.
Ignored if |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[ndarray, List]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If input array does not contain the required fields. |
ValueError
|
If |
Source code in pyforestscan/calculate.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
calculate_fhd(voxel_returns, voxel_height=1.0, min_height=0.0, max_height=None)
¶
Calculate the Foliage Height Diversity (FHD) for a given set of voxel returns.
This function computes FHD by calculating the entropy of the voxel return proportions along the Z (height) axis, which represents the vertical diversity of canopy structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voxel_returns
|
ndarray
|
3D numpy array of shape (X, Y, Z) representing voxel returns, where X and Y are spatial dimensions and Z represents height bins (vertical layers). |
required |
voxel_height
|
float
|
Height of each voxel in meters (> 0). Defaults to 1.0. |
1.0
|
min_height
|
float
|
Minimum height (in meters) to include in the entropy calculation. Defaults to 0.0 (use all heights by default). |
0.0
|
max_height
|
float or None
|
Maximum height (in meters) to include. If None, uses the full height of the voxel grid. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 2D numpy array of shape (X, Y) with FHD values for each (X, Y) location. Areas with no voxel returns in the requested height range will have NaN values. |
Source code in pyforestscan/calculate.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
calculate_pad(voxel_returns, voxel_height=1.0, beer_lambert_constant=1.0, drop_ground=True)
¶
Calculate the Plant Area Density (PAD) using the Beer-Lambert Law.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voxel_returns
|
ndarray
|
3D numpy array of shape (X, Y, Z) representing the LiDAR returns in each voxel column. |
required |
voxel_height
|
float
|
Height of each voxel. Defaults to 1.0. |
1.0
|
beer_lambert_constant
|
float
|
The Beer-Lambert constant used in the calculation. Defaults to 1.0. |
1.0
|
drop_ground
|
bool
|
If True, sets PAD values in the ground (lowest) voxel layer to NaN in the output. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 3D numpy array containing PAD values for each voxel, same shape as |
Source code in pyforestscan/calculate.py
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 | |
calculate_pai(pad, voxel_height, min_height=1.0, max_height=None)
¶
Calculate Plant Area Index (PAI) from Plant Area Density (PAD) data by summing PAD values along the height (Z) axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
ndarray
|
3D numpy array representing Plant Area Density (PAD) values, shape (X, Y, Z). |
required |
voxel_height
|
float
|
Height of each voxel in meters. |
required |
min_height
|
float
|
Minimum height in meters for summing PAD values. Defaults to 1.0. |
1.0
|
max_height
|
float
|
Maximum height in meters for summing PAD values. If None, uses the full height of the input array. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 2D numpy array of shape (X, Y) with PAI values for each (x, y) voxel column. |
Notes
- If the requested integration range is empty (e.g., min_height >= available maximum height), returns a zeros array (no canopy above the threshold), mirroring the behavior used by canopy cover.
Source code in pyforestscan/calculate.py
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 | |
calculate_point_density(voxel_returns, per_area=False, cell_area=None)
¶
Calculate point density (or count) per (X, Y) voxel column by summing returns across Z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voxel_returns
|
ndarray
|
3D numpy array of shape (X, Y, Z) representing voxel returns (counts). |
required |
per_area
|
bool
|
If True, divide counts by |
False
|
cell_area
|
float or None
|
Area of a single (X, Y) cell in the same units as the coordinates (e.g., m^2).
Required when |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 2D array (X, Y) of point counts (or density if per_area=True). |
Notes
- For columns with no returns, the count is 0. This differs from metrics like FHD where no-data is NaN.
- If you want density per m^2, set
per_area=Trueand passcell_area = dx * dy.
Source code in pyforestscan/calculate.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
calculate_voxel_stat(arr, voxel_resolution, dimension, stat, z_index_range=None)
¶
Compute a column-wise statistic for a given dimension over a 3-D voxel grid.
The function bins points into voxels with the same XY/Z sizing used by assign_voxels.
For each (X, Y) column it filters points to the requested Z-index range, then evaluates a
simple statistic (mean, min, max, etc.) on the provided dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
Structured array containing at least 'X', 'Y', and 'HeightAboveGround'
fields, plus the provided |
required |
voxel_resolution
|
tuple[float, float, float]
|
(dx, dy, dz) sizes in the same units as the coordinates and height-above-ground values. All components must be > 0. |
required |
dimension
|
str
|
Dimension/field name to evaluate (e.g. 'Z', 'Intensity',
'HeightAboveGround'). The field must exist on |
required |
stat
|
str
|
Statistic to compute. Supported values (case-insensitive) are: {'mean', 'sum', 'count', 'min', 'max', 'median', 'std'}. |
required |
z_index_range
|
Tuple[int, Optional[int]] | None
|
Inclusive-exclusive Z index bounds
expressed in voxel indices |
None
|
Returns:
| Type | Description |
|---|---|
|
tuple[np.ndarray, list]: (stat_array, extent) - stat_array: 2-D array shaped (nx, ny) with the requested statistic per column. Cells without points are NaN (except for 'count', which yields 0). - extent: [x_min, x_max, y_min, y_max] covering the raster footprint. |
Source code in pyforestscan/calculate.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
generate_dtm(ground_points, resolution=2.0)
¶
Generates a Digital Terrain Model (DTM) raster from classified ground points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ground_points
|
list
|
Point cloud arrays of classified ground points. |
required |
resolution
|
float
|
Spatial resolution of the DTM in meters. |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[ndarray, List]
|
A tuple containing the DTM as a 2D NumPy array and the spatial extent [x_min, x_max, y_min, y_max]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no ground points are found for DTM generation. |
KeyError
|
If point cloud data is missing 'X', 'Y', or 'Z' fields. |
Source code in pyforestscan/calculate.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |