Raster Smoothing and Normalized DTM — Algorithm Notes
Related user pages: Normalized DTM from DTM Raster, Detect Hummock and Hollow from LiDAR Data
Purpose
Smooth a one-band raster, such as a DTM, by the mean or median of a circular moving window.
The normalized DTM is the DTM minus its smoothed DTM: hummocks are positive, hollows negative, and a
plane, flat or sloping, is 0.
The smoothing is written twice, with the same results:
- Python:
smooth_rasterinberatools/modules/raster2products/algo_raster_smoothing.py, used by Normalized DTM from DTM Raster (beratools/modules/raster2products/ndtm_fr_dtmraster.py). It needs no R. - R:
smooth_rasterinberatools/modules/pc2products/r_scripts_collection.r, used by Detect Hummock and Hollow from LiDAR Data on the DTM it builds from the point cloud.
A test (test_r_smooth_raster_matches_python in the frame's tests/test_cross_module.py, as it
uses a tool of each module) checks that the
two give the same values.
Inputs and outputs
- Inputs: a raster (NaN or the raster's nodata for no value), the window's radius in map units,
meanormedian - Output: the smoothed raster on the same grid; the normalized DTM tool writes
ndtm_<DTM name>_<Smooth_by>_<Min_ws>m.tif
Method summary
- Window (
circle_kernel): the cells whose centres lie within the radius of the centre cell, measured with the cell width across and the cell height up and down. It is a true circle also on cells that aren't square.terra::focalMat(x, d, "circle")isn't: it measures rows in steps of the cell width too, so on 0.3 × 0.2 m cells a 1.5 m radius reached only 1.0 m up and down. - Radius slack: the window is
2 * floor(r / cell) + 1cells each way. A cell size stored rounded can putr / celljust under a whole number (a 3 m window on 0.25 m cells of a DTM in feet came to 5.9999998 cells), which would lose the outer ring, so the radius is multiplied by1 + 1e-6(RADIUS_SLACK). - Smoothing (
smooth_raster): for each cell with a value, the mean or median of the cells of the window that have a value. Cells with no value, and cells beyond the raster's edges, are left out, so near an edge the window holds only the real neighbours. Cells with no value stay without one. The median of an even count is the mean of the two middle values, as R'smedian. - Normalized DTM (
normalized_raster): the DTM minus the smoothed DTM.
The Python loops are compiled with numba and run in parallel on processes threads, one row of
cells per task. The R version passes the window to terra::focal(..., na.rm = TRUE, expand = FALSE).
The median slides the window along each row (_median_sliding). The window's values are kept
sorted; moving one cell east, only the cells leaving its west edge and entering its east edge change
(about 60 of the 709 cells of a 3 m window on 0.1 m cells). These few are sorted and merged into the
window in one pass, and the median is read from the middle. That gives exactly the values of finding
the median of the whole window at every cell, about 3 times faster for a 709-cell window and 2 times
for a 113-cell one. A window whose rows aren't single spans of columns (not a circle) uses the full
median at every cell. The mean adds up the window at every cell.
A folder of DTM files (Normalized DTM from DTM Raster)
A folder's GeoTIFFs are read as one mosaic (Mosaic in beratools/modules/raster2products/ndtm_fr_dtmraster.py),
leaving out files named ndtm_*, Merged_* (the LiDAR tools' mosaic next to their tiles) and
*.partial.tif:
- The files must share the CRS and the cell size (to a relative
1e-6), be north up, and have their corners on one grid, whole cells apart (to1e-3of a cell), so each file is a rectangle of rows and columns counted from the first file's corner. The files are never resampled. - A window of the mosaic is read from every file it overlaps, in name order. Where files overlap, the first file wins: a later file fills only the cells the earlier ones have no value in. The tool warns with the number of overlapping pairs.
- Outside every file, the mosaic has no value, as beyond a single DTM's edges.
Each output is a rectangle of the mosaic: a file's own rectangle (one output per file) and, with
merge_output, also the mosaic's whole extent. With merge_output the tiles are laid over the
mosaic's extent, and each smoothed tile is written into the merged output and into each file's
output it overlaps, so every cell is smoothed once for all the outputs. As the windows read the mosaic, not the file, the outputs of
neighbouring files join without seams and have the values of one run on the merged DTM.
Tiling (Normalized DTM from DTM Raster)
Each output is computed in square tiles of tile_size cells, one after another, each read with a
buffer as wide as the window's reach. Every cell's window then lies whole inside the buffered tile,
or reaches the DTM's own edge as it would in a run on the whole DTM (cells outside the DTM are read
as no value), so the tiles join with exactly the same values as smoothing the whole DTM at once.
Each tile is cropped back to its own cells and written into the output, which is written under a
.partial.tif name and renamed once complete.
Reading, smoothing and writing overlap, on three threads of the tool:
- A reader thread reads the next buffered tiles from the DTM files, up to one batch's cells ahead, so the smoothing doesn't wait on the drive.
- The main thread smooths the tiles in batches, in one parallel loop over all the batch's rows
(
smooth_rasters). A batch takes tiles until it has 32 rows per thread (BATCH_ROWS_PER_THREAD): a 2000-cell tile already has that for up to ~60 threads, so a laptop or a small machine smooths one tile at a time and pays nothing for batching, while a machine with ~100 cores smooths 2 or 3 tiles at once, which keeps its threads busy. A batch uses at most a quarter of the free memory (BATCH_MEMORY_FRACTION, about 40 bytes a cell). - A writer thread writes (and compresses) the smoothed tiles while the next batch is smoothed,
holding at most 16 tiles (
WRITE_QUEUE).
The values are the same whatever the batches; an error on any thread stops the run and leaves no
.partial.tif file. Batching and the overlap matter most where reading or writing is slow (a
network or iSCSI drive) or where a tile has fewer rows than the machine has threads to fill; on a
laptop with the DTM on a local disk they gain little.
Assumptions
- The raster is in a projected CRS. The normalized DTM tool converts
Min_ws(metres) to the map units from the CRS, and refuses a CRS in degrees. - The window is at least 3 cells across:
Min_wsat least twice the cell size.
Edge cases
- A tile with no values is written as no data; a DTM with no values at all is refused. In a folder, a file with no values gets no output (with a warning).
- Files of a folder on another grid, CRS or cell size are refused: resample them onto one grid first.
- A larger window takes longer, about in proportion to the cells in the circle. Per 2000 × 2000 tile on 20 threads: a 3 m window on 0.25 m cells (113 cells) about 0.2 s for the median and 0.1 s for the mean; on 0.1 m cells (709 cells) about 0.7 s for the median and 0.3 s for the mean.
- A folder's files are opened at the start to read their grids, with progress. The first 8 are
opened one at a time and timed: if an open takes 10 ms or more (a network or iSCSI drive, where
each open waits on the network), the rest are opened 16 at a time; otherwise they stay one at a
time, as on a local disk threads only wait on each other (
HEADER_PROBE,HEADER_SLOW_OPEN,HEADER_THREADS). For thousands of files on a network drive this can still take a while. - GDAL lists a file's folder on every open, looking for sidecar files (
.aux.xml,.ovr,.tfw, ...). In a folder of 2,380 DTM tiles on an iSCSI drive, that made each open take about 0.1 s, so the tool opens the DTM files without it (GDAL_DISABLE_READDIR_ON_OPEN=EMPTY_DIR), unless the first file reads differently without it (its CRS, grid, no-data value or mask is in a sidecar file). - The mosaic keeps up to 256 DTM files open (
OPEN_FILES), so a file is opened about once in a run, not once for each tile whose buffered window reaches it.