LiDAR#
LiDAR point-cloud I/O, classification, gridding, and analysis. The LAS / LAZ I/O surface
soft-imports laspy — install with pip install laspy[lazrs] to enable file read/write.
Everything else (gridding, ground filtering, clipping, tree detection) operates on the
in-memory LasPoints record without any external dependency beyond NumPy / SciPy.
Module-level functions#
LiDAR point-cloud I/O, gridding, ground filtering, and analysis.
- :class:
LasPoints— in-memory point-cloud record (xyz + intensity + classification + return-number + CRS). - :func:
read_las/ :func:write_las— LAS / LAZ I/O vialaspy. - :func:
grid_lidar_points— bucket raw(x, y, z)arrays into a gridded DEM with min / max / mean / median aggregation.
Reading / writing LAS files requires laspy. Install with
pip install laspy[lazrs] to also handle LAZ compression. The
gridding helper does not require laspy and operates on raw arrays.
LasPoints
dataclass
#
In-memory LiDAR point cloud.
Numeric arrays are all parallel — index i selects the i-th point
across every field. classification follows the ASPRS LAS standard
(2 = ground, 5 = high vegetation, 6 = building, etc.).
Attributes:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
|
y |
ndarray
|
|
z |
ndarray
|
|
intensity |
ndarray
|
|
classification |
ndarray
|
|
return_number |
ndarray
|
|
crs |
object | None
|
Optional CRS object (whatever |
Source code in src/digitalrivers/lidar.py
subset(mask)
#
Return a new LasPoints containing only the points where mask is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
'LasPoints'
|
A new |
Source code in src/digitalrivers/lidar.py
read_las(path)
#
Read a LAS or LAZ file into a LasPoints record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Filesystem path to a |
required |
Returns:
| Type | Description |
|---|---|
LasPoints
|
|
LasPoints
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/digitalrivers/lidar.py
classify_ground(points, *, method='zhang', cell_size=1.0, window_cells=5, slope_threshold=1.0)
#
Classify each LiDAR point as ground or non-ground.
Two algorithm families are available:
"zhang"(Zhang 2003) — morphological tophat filter on a min-grid DEM. Points whose elevation rises more thanslope_thresholdabove the morphological opening at their cell are non-ground. Fast; works well on relatively flat terrain. Implementation uses a single structuring-element scale rather than the original paper's multi-scale stack."axelsson"— Axelsson 2000 progressive TIN densification. Not yet implemented; raises NotImplementedError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
LasPoints
|
|
required |
method
|
str
|
|
'zhang'
|
cell_size
|
float
|
Cell size in map units for the intermediate min-grid. Smaller values capture fine ground detail at the cost of memory. |
1.0
|
window_cells
|
int
|
Side length of the structuring element used for the morphological opening (Zhang only). Default 5 (a 5×5 window). |
5
|
slope_threshold
|
float
|
Elevation threshold above the opening above which a point is classified as non-ground (Zhang only). Default 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
ndarray
|
(unclassified / non-ground), parallel to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
NotImplementedError
|
If |
Source code in src/digitalrivers/lidar.py
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 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 | |
detect_trees(chm, *, min_height_m=2.0, radius_fn=None)
#
Detect individual tree tops on a canopy height model.
Variable-window local-maxima search: for each candidate cell whose CHM
value is >= min_height_m, scan a square window whose half-width
scales with the cell's height (radius_fn(h) map units, default
_default_tree_radius — ~5% of canopy height). The cell is reported
as a tree top iff its CHM value is the maximum in the window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chm
|
pyramids |
required | |
min_height_m
|
float
|
Minimum canopy height (m) for a cell to be a tree-top candidate. Defaults to 2.0. |
2.0
|
radius_fn
|
Callable mapping |
None
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
|
|
|
indices), and |
References
Popescu, S. C. & Wynne, R. H. (2004). "Seeing the trees in the forest: Using LIDAR and multispectral data fusion with local filtering and variable window size for estimating tree height." Photogrammetric Engineering & Remote Sensing 70(5): 589–604.
Source code in src/digitalrivers/lidar.py
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 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 | |
clip(points, polygon, *, inverse=False)
#
Clip a LasPoints cloud to a polygon (or its complement).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
LasPoints
|
Input |
required |
polygon
|
Shapely Polygon or MultiPolygon in the same CRS as |
required | |
inverse
|
bool
|
If False (default), keep only points inside |
False
|
Returns:
| Type | Description |
|---|---|
LasPoints
|
A new |
Source code in src/digitalrivers/lidar.py
merge(*pointclouds)
#
Concatenate two or more LasPoints into a single cloud.
Numeric arrays are stacked field-by-field. Optional fields (intensity / classification / return_number) are preserved only when every input carries them — otherwise the field on the output is empty (size 0). The CRS is taken from the first input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*pointclouds
|
LasPoints
|
Two or more |
()
|
Returns:
| Type | Description |
|---|---|
LasPoints
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than one cloud is supplied. |
Source code in src/digitalrivers/lidar.py
filter_classes(points, classes)
#
Keep only points whose ASPRS classification is in classes.
Standard ASPRS codes include 2 (ground), 3 (low vegetation), 4
(medium vegetation), 5 (high vegetation), 6 (building), 9 (water).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
LasPoints
|
Input |
required |
classes
|
set[int] | list[int] | tuple[int, ...]
|
Iterable of integer class codes to keep. |
required |
Returns:
| Type | Description |
|---|---|
LasPoints
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/digitalrivers/lidar.py
write_las(points, path, *, point_format=6, version='1.4')
#
Write a LasPoints cloud to a LAS or LAZ file.
The file extension determines compression: .laz uses LAZ
(requires lazrs), .las is uncompressed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
LasPoints
|
|
required |
path
|
str
|
Output filesystem path. |
required |
point_format
|
int
|
ASPRS LAS point format (default 6 — supports GPS time and high-precision returns; pick 0 for legacy compatibility). |
6
|
version
|
str
|
LAS version string (default |
'1.4'
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/digitalrivers/lidar.py
grid_lidar_points(xs, ys, zs, cell_size, bounds=None, aggregate='min', epsg=4326, *, idw_k=8, idw_power=2.0, rbf_kernel='thin_plate_spline', rbf_smoothing=0.0)
#
Grid a LiDAR point cloud to a DEM.
Pragmatic LiDAR-to-DEM step that operates on raw (x, y, z) arrays.
Useful when the caller has read LAS / LAZ externally (via read_las)
and wants a gridded surface.
The aggregate parameter selects either a block-aggregation method or
a spatial-interpolation method:
Block aggregation (one value per cell, based on points that land inside the cell):
"min"(default) — canonical bare-earth choice for first-return LiDAR."max"— canopy / DSM choice."mean"/"median"— smoothed surfaces."count"— point-density raster.
Spatial interpolation (one value per cell centre, computed from nearby points regardless of cell membership):
"idw"— inverse-distance-weighted mean of the K nearest points."nn"— nearest-neighbour assignment."tin"— barycentric interpolation on the Delaunay triangulation."rbf"— radial-basis-function interpolation (scipy.interpolate. RBFInterpolator).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xs / ys / zs
|
1-D arrays of point coordinates. |
required | |
cell_size
|
float
|
output cell side length in map units (must match the CRS). |
required |
bounds
|
|
None
|
|
aggregate
|
str
|
One of |
'min'
|
epsg
|
int
|
EPSG code of the input coordinates. |
4326
|
idw_k
|
int
|
Neighbour count for |
8
|
idw_power
|
float
|
Distance exponent for IDW weights |
2.0
|
rbf_kernel
|
str
|
Kernel name passed to |
'thin_plate_spline'
|
rbf_smoothing
|
float
|
Smoothing parameter for the RBF kernel. Defaults to 0.0 (exact interpolation). |
0.0
|
Returns:
| Type | Description |
|---|---|
|
A pyramids |
Raises:
| Type | Description |
|---|---|
ValueError
|
For mismatched input lengths or unknown |
Examples:
-
Bucket four points into a 2x1 grid with min aggregation:
import numpy as np from digitalrivers.lidar import grid_lidar_points xs = np.array([0.1, 0.2, 1.1]) ys = np.array([0.1, 0.2, 0.1]) zs = np.array([5.0, 2.0, 4.0]) ds = grid_lidar_points( ... xs, ys, zs, cell_size=1.0, bounds=(0.0, 0.0, 2.0, 1.0), ... aggregate="min", epsg=3857, ... ) ds.read_array().tolist() [[2.0, 4.0]]
-
Mean aggregation averages every point that lands in a cell:
import numpy as np from digitalrivers.lidar import grid_lidar_points xs = np.array([0.1, 0.2]) ys = np.array([0.1, 0.2]) zs = np.array([4.0, 6.0]) ds = grid_lidar_points( ... xs, ys, zs, cell_size=1.0, bounds=(0.0, 0.0, 1.0, 1.0), ... aggregate="mean", epsg=3857, ... ) float(ds.read_array()[0, 0]) 5.0
-
Empty cells receive the dataset's no-data sentinel (-9999.0):
import numpy as np from digitalrivers.lidar import grid_lidar_points ds = grid_lidar_points( ... np.array([0.5]), np.array([0.5]), np.array([3.0]), ... cell_size=1.0, bounds=(0.0, 0.0, 2.0, 2.0), ... aggregate="min", ... ) float(ds.no_data_value[0]) -9999.0 int((ds.read_array() == -9999.0).sum()) 3
Source code in src/digitalrivers/lidar.py
475 476 477 478 479 480 481 482 483 484 485 486 487 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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
Surface map#
| Section | Functions / classes |
|---|---|
| Record class | LasPoints (xyz + intensity + classification + return_number + crs) |
| I/O — W-15 | read_las(path), write_las(points, path, point_format=6, version="1.4") |
| Ground filter — W-16 | classify_ground(points, method="zhang"/"axelsson", ...) (Zhang 2003 tophat ships; Axelsson 2000 TIN-progressive deferred) |
| Gridding — W-17 / P34 | grid_lidar_points(xs, ys, zs, cell_size, aggregate=...) — min / max / mean / median / count block aggregation plus idw / nn / tin / rbf interpolation |
| Vector ops — W-18 | clip(points, polygon, inverse=False), merge(*pointclouds), filter_classes(points, classes) |
| Forestry — W-19 | detect_trees(chm, min_height_m=2.0, radius_fn=...) — variable-window local-maxima on a canopy height model |