Add terrain metrics module: TRI, TPI, and Roughness#920
Merged
brendancol merged 1 commit intomasterfrom Feb 28, 2026
Merged
Conversation
Implement three widely-used terrain morphometry metrics as 3×3 focal window operations on elevation values (no cellsize needed): - TRI (Terrain Ruggedness Index) — Riley et al. 1999 - TPI (Topographic Position Index) — Weiss 2001 - Roughness — GDAL definition (max - min in 3×3 window) All three support four backends (numpy, cupy, dask+numpy, dask+cupy), boundary modes (nan, nearest, reflect, wrap), and Dataset input via @supports_dataset. NaN propagates through all kernels for consistency between numpy and dask paths (standard GDAL behavior).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sqrt(sum((z_neighbor - z_center)²))over 8 neighbors (Riley et al. 1999)z_center - mean(z_neighbors)(Weiss 2001)max(window) - min(window)in 3×3 window (GDAL definition)nan,nearest,reflect,wrap@supports_datasetdecorator for Dataset input.xrsaccessor methods on both DataArray and DatasetBenchmarkingpatternFiles changed
xrspatial/terrain_metrics.pyxrspatial/__init__.pytri,tpi,roughnessimportsxrspatial/accessor.pyxrspatial/tests/test_terrain_metrics.pybenchmarks/benchmarks/terrain_metrics.pyREADME.mdTest plan
pytest xrspatial/tests/test_terrain_metrics.py -v— 179 passedtest_curvature.pystill passes (75 passed)Benchmark results
Dask overhead dominates at small sizes; at 1000×500 the NumPy Numba JIT kernels are sub-2ms and CuPy stays under 1ms.