Handlers Module¶
create_geotiff(layer, output_file, crs, spatial_extent, nodata=-9999)
¶
Create a GeoTIFF file from the given data layer.
The function transposes the input layer before saving and writes it as a single-band GeoTIFF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer
|
ndarray
|
The data layer to write, assumed to have shape (X, Y). |
required |
output_file
|
str
|
Path where the GeoTIFF file will be saved. |
required |
crs
|
str
|
Coordinate Reference System for the GeoTIFF. |
required |
spatial_extent
|
tuple
|
The spatial extent as (x_min, x_max, y_min, y_max). |
required |
nodata
|
float or int
|
Value to use for NoData areas. Defaults to -9999. |
-9999
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
RasterioError
|
If there is an error creating the GeoTIFF. |
ValueError
|
If the layer has invalid dimensions or the spatial extent is invalid. |
Source code in pyforestscan/handlers.py
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 |
|
get_raster_epsg(dtm_path)
¶
Retrieve the EPSG code from a raster file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtm_path
|
str
|
File path to the raster file. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The EPSG code or CRS string of the raster file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified file does not exist. |
Source code in pyforestscan/handlers.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
load_polygon_from_file(vector_file_path, index=0)
¶
Load a polygon geometry and its CRS from a given vector file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector_file_path
|
str
|
Path to the vector file containing the polygon geometry. |
required |
index
|
int
|
Index of the polygon to load from the file. Defaults to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
Tuple[str, str]
|
A tuple containing: - str: Well-Known Text (WKT) representation of the selected polygon. - str: Coordinate reference system (CRS) of the vector file as a string. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified vector file does not exist. |
ValueError
|
If the file cannot be read or is not a valid vector file format. |
Source code in pyforestscan/handlers.py
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 |
|
read_lidar(input_file, srs, bounds=None, thin_radius=None, hag=False, hag_dtm=False, dtm=None, crop_poly=False, poly=None)
¶
Read and process a LiDAR point cloud file using PDAL with various options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_file
|
str
|
Path to the input LiDAR file. Supported formats: .las, .laz, .copc, .copc.laz, or ept.json. |
required |
srs
|
str
|
Spatial Reference System (SRS) of the point cloud. |
required |
bounds
|
tuple or list
|
Bounds for cropping data (only applies to EPT format). Format: ([xmin, xmax], [ymin, ymax], [zmin, zmax]). |
None
|
thin_radius
|
float
|
Radius for thinning the point cloud. Must be positive. |
None
|
hag
|
bool
|
If True, calculate Height Above Ground (HAG) using Delaunay triangulation. Defaults to False. |
False
|
hag_dtm
|
bool
|
If True, calculate Height Above Ground (HAG) using a DTM file. Defaults to False. |
False
|
dtm
|
str
|
Path to the DTM (.tif) file, required if hag_dtm is True. |
None
|
crop_poly
|
bool
|
If True, crop the point cloud using a polygon. Defaults to False. |
False
|
poly
|
str
|
Path to a polygon file or the WKT string of the polygon geometry. |
None
|
Returns:
Type | Description |
---|---|
ndarray or None
|
np.ndarray or None: Processed point cloud data as a NumPy array, or None if no data is retrieved. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the input file, DTM file, or polygon file does not exist. |
ValueError
|
If the input file extension is unsupported; thinning radius is non-positive; both 'hag' and 'hag_dtm' are True at the same time; or a required parameter (e.g., DTM for hag_dtm) is missing or invalid. |
Source code in pyforestscan/handlers.py
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 244 245 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 |
|
simplify_crs(crs_list)
¶
Convert a list of coordinate reference system (CRS) representations to their corresponding EPSG codes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crs_list
|
list
|
List of CRS definitions to be simplified. Each element may be any format accepted by pyproj.CRS (e.g., WKT string, PROJ string, EPSG code, etc.). |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List
|
List of EPSG codes corresponding to the input CRS definitions. |
Raises:
Type | Description |
---|---|
CRSError
|
If any of the CRS definitions cannot be converted to an EPSG code. |
Source code in pyforestscan/handlers.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
validate_crs(crs_list)
¶
Validate that all CRS (Coordinate Reference System) representations in the list are identical.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crs_list
|
list
|
List of coordinate reference system definitions to validate. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if all CRSs match. |
Raises:
Type | Description |
---|---|
ValueError
|
If the CRSs do not match. |
Source code in pyforestscan/handlers.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
validate_extensions(las_file_path, dtm_file_path)
¶
Validate that input file paths have the correct extensions for point cloud and DTM files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
las_file_path
|
str
|
File path of the point cloud file. Supported extensions are .las and .laz. |
required |
dtm_file_path
|
str
|
File path of the DTM (Digital Terrain Model) file. Supported extension is .tif. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the point cloud file does not have a .las or .laz extension. |
ValueError
|
If the DTM file does not have a .tif extension. |
Source code in pyforestscan/handlers.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
write_las(arrays, output_file, srs=None, compress=True)
¶
Write point cloud data to a LAS or LAZ file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays
|
list or ndarray
|
Point cloud data arrays to write. |
required |
output_file
|
str
|
Path for the output file. Must end with .las (if uncompressed) or .laz (if compressed). |
required |
srs
|
str
|
Spatial Reference System to reproject the data. If provided, reprojection is applied. |
None
|
compress
|
bool
|
If True, write a compressed LAZ file (.laz). If False, write an uncompressed LAS file (.las). Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
If 'compress' is True and the output file extension is not .laz. |
ValueError
|
If 'compress' is False and the output file extension is not .las. |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in pyforestscan/handlers.py
292 293 294 295 296 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|