LiDAR#
LiDAR point-cloud I/O, classification, gridding, and analysis. The LAS / LAZ I/O surface
soft-imports laspy — add laspy[lazrs] to the environment 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 clouds: reading them, classifying them, and gridding them.
The path from a raw cloud to a surface digital-rivers can route over:
- :mod:
io— read the LAS / LAZ file into aLasPoints. - :mod:
points— clip, merge or filter it down to the returns you want. - :mod:
classify— separate ground from vegetation and structures, or find tree tops. - :mod:
gridding— bin the ground returns into a raster, which is whereDEMtakes over.
laspy is an optional dependency and is imported only inside :mod:io, so the array
half of this package works on a cloud that came from anywhere.
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/points.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/points.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/classify.py
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/classify.py
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 | |
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/gridding.py
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 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 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 | |
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/io.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/io.py
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/points.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/points.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/points.py
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 |