darts_preprocessing.v2
¶
PLANET scene based preprocessing.
calculate_aspect
¶
Calculate aspect (compass direction) of the terrain surface from an ArcticDEM Dataset.
Aspect indicates the downslope direction of the maximum rate of change in elevation.
Parameters:
Returns:
-
xarray.Dataset–xr.Dataset: Input Dataset with new data variable added:
-
xarray.Dataset–-
aspect (float32): Aspect in degrees clockwise from north [0-360], or -1 for flat areas.
-
long_name: "Aspect"
- units: "degrees"
- description: Compass direction of slope
- source: "ArcticDEM"
-
Note
Aspect values:
- 0° or 360°: North-facing
- 90°: East-facing
- 180°: South-facing
- 270°: West-facing
- -1: Flat (no dominant direction)
Example
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
calculate_curvature
¶
Calculate curvature of the terrain surface from an ArcticDEM Dataset.
Curvature measures the rate of change of slope, indicating terrain convexity or concavity.
Parameters:
Returns:
-
xarray.Dataset–xr.Dataset: Input Dataset with new data variable added:
-
xarray.Dataset–-
curvature (float32): Curvature values.
-
long_name: "Curvature"
- description: Rate of change of slope
- source: "ArcticDEM"
-
Note
Curvature interpretation:
- Positive values: Convex terrain (hills, ridges)
- Negative values: Concave terrain (valleys, depressions)
- Near zero: Planar terrain
Example
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
calculate_hillshade
¶
calculate_hillshade(
arcticdem_ds: xarray.Dataset,
azimuth: int = 225,
angle_altitude: int = 25,
) -> xarray.Dataset
Calculate hillshade of the terrain surface from an ArcticDEM Dataset.
Hillshade simulates illumination of terrain from a specified sun position, useful for visualization and terrain analysis.
Parameters:
-
arcticdem_ds(xarray.Dataset) –Dataset containing: - dem (float32): Digital Elevation Model
-
azimuth(int, default:225) –Light source azimuth in degrees clockwise from north [0-360]. Defaults to 225 (southwest).
-
angle_altitude(int, default:25) –Light source altitude angle in degrees above horizon [0-90]. Defaults to 25.
Returns:
-
xarray.Dataset–xr.Dataset: Input Dataset with new data variable added:
-
xarray.Dataset–-
hillshade (float32): Illumination values [0-255], where 0 is shadow and 255 is fully lit.
-
long_name: "Hillshade"
- description: Documents azimuth and angle_altitude used
- source: "ArcticDEM"
-
Note
Common azimuth/altitude combinations:
- 315°/45°: Classic northwest illumination (default for many GIS applications)
- 225°/25°: Southwest with low sun (better for visualizing subtle features)
The hillshade calculation accounts for both slope and aspect of the terrain.
Example
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
calculate_ndvi
¶
Calculate NDVI (Normalized Difference Vegetation Index) from spectral bands.
NDVI is a widely-used vegetation index that indicates photosynthetic activity and vegetation health. Values range from -1 to 1, with higher values indicating denser, healthier vegetation.
Parameters:
-
optical(xarray.Dataset) –Dataset containing spectral bands: - nir (float32): Near-infrared reflectance [0-1] - red (float32): Red reflectance [0-1]
Returns:
Note
Formula: NDVI = (NIR - Red) / (NIR + Red)
Input bands are clipped to [0, 1] before calculation to avoid numerical instabilities from negative reflectance values or sensor artifacts. The final result is also clipped to ensure values remain in the valid [-1, 1] range.
Example
Calculate NDVI from optical data:
Source code in darts-preprocessing/src/darts_preprocessing/engineering/indices.py
calculate_slope
¶
Calculate slope of the terrain surface from an ArcticDEM Dataset.
Slope represents the rate of change of elevation, indicating terrain steepness.
Parameters:
Returns:
-
xarray.Dataset–xr.Dataset: Input Dataset with new data variable added:
-
xarray.Dataset–-
slope (float32): Slope in degrees [0-90].
-
long_name: "Slope"
- units: "degrees"
- source: "ArcticDEM"
-
Note
Slope is calculated using finite difference methods on the DEM. Values approaching 90° indicate near-vertical terrain.
Example
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
calculate_topographic_position_index
¶
calculate_topographic_position_index(
arcticdem_ds: xarray.Dataset,
outer_radius: int,
inner_radius: int,
) -> xarray.Dataset
Calculate the Topographic Position Index (TPI) from an ArcticDEM Dataset.
TPI measures the relative topographic position of a point by comparing its elevation to the mean elevation of the surrounding neighborhood. Positive values indicate higher positions (ridges), negative values indicate lower positions (valleys).
Parameters:
-
arcticdem_ds(xarray.Dataset) –The ArcticDEM Dataset containing the 'dem' variable (float32).
-
outer_radius(int) –The outer radius of the neighborhood in meters. Can also be specified as string with units (e.g., "100m" or "10px").
-
inner_radius(int) –The inner radius of the annulus kernel in meters. If > 0, creates an annulus (ring) instead of a circle. Set to 0 for a circular kernel. Can also be specified as string with units (e.g., "50m" or "5px").
Returns:
-
xarray.Dataset–xr.Dataset: The input Dataset with a new data variable added:
-
xarray.Dataset–-
tpi (float32): Topographic Position Index values.
-
long_name: "Topographic Position Index (TPI)"
- description: Details about the kernel used
-
Note
Kernel shape combinations:
- inner_radius=0: Circular kernel comparing each cell to all neighbors within outer_radius
- inner_radius>0: Annulus kernel comparing each cell to neighbors in a ring between inner_radius and outer_radius. Useful for multi-scale terrain analysis.
The actual radii used are rounded to the nearest pixel based on the DEM resolution.
Example
Calculate TPI with circular and annulus kernels:
from darts_preprocessing import calculate_topographic_position_index
# Circular kernel (100m radius)
arcticdem_with_tpi = calculate_topographic_position_index(
arcticdem_ds=arcticdem,
outer_radius=100,
inner_radius=0
)
# Annulus kernel (50-100m ring)
arcticdem_multi_scale = calculate_topographic_position_index(
arcticdem_ds=arcticdem,
outer_radius=100,
inner_radius=50
)
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
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 | |
get_azimuth_and_elevation
¶
Get the azimuth and elevation from the optical dataset attributes.
Parameters:
Returns:
Source code in darts-preprocessing/src/darts_preprocessing/v2.py
preprocess_arcticdem
¶
preprocess_arcticdem(
ds_arcticdem: xarray.Dataset,
tpi_outer_radius: int,
tpi_inner_radius: int,
azimuth: int,
angle_altitude: int,
) -> xarray.Dataset
Preprocess the ArcticDEM data with mdoern (DARTS v2) preprocessing steps.
Parameters:
-
ds_arcticdem(xarray.Dataset) –The ArcticDEM dataset.
-
tpi_outer_radius(int) –The outer radius of the annulus kernel for the tpi calculation in number of cells.
-
tpi_inner_radius(int) –The inner radius of the annulus kernel for the tpi calculation in number of cells.
-
azimuth(int) –The azimuth angle of the light source in degrees for hillshade calculation.
-
angle_altitude(int) –The altitude angle of the light source in degrees for hillshade
Returns:
Source code in darts-preprocessing/src/darts_preprocessing/v2.py
preprocess_v2
¶
preprocess_v2(
ds_optical: xarray.Dataset,
ds_arcticdem: xarray.Dataset | None,
ds_tcvis: xarray.Dataset | None,
tpi_outer_radius: int = 100,
tpi_inner_radius: int = 0,
device: typing.Literal["cuda", "cpu"]
| int = darts_utils.cuda.DEFAULT_DEVICE,
) -> xarray.Dataset
Preprocess optical data with modern (DARTS v2) preprocessing steps.
This function combines optical imagery with terrain (ArcticDEM) and temporal vegetation indices (TCVIS) to create a multi-source feature dataset for segmentation. All auxiliary data sources are reprojected and cropped to match the optical data's extent and resolution.
Processing steps
- Calculate NDVI from optical bands
- If TCVIS provided: Reproject and merge Tasseled Cap trends
- If ArcticDEM provided: Calculate terrain features (TPI, slope, hillshade, aspect, curvature) using solar geometry from optical data attributes
Parameters:
-
ds_optical(xarray.Dataset) –Optical imagery dataset (PlanetScope or Sentinel-2) containing: - Required variables: blue, green, red, nir (float32, reflectance values) - Required variables: quality_data_mask, valid_data_mask (uint8) - Required attributes: azimuth (float), elevation (float) for hillshade calculation
-
ds_arcticdem(xarray.Dataset | None) –ArcticDEM dataset containing 'dem' (float32) and 'arcticdem_data_mask' (uint8). If None, terrain features are skipped.
-
ds_tcvis(xarray.Dataset | None) –TCVIS dataset containing tc_brightness, tc_greenness, tc_wetness (float). If None, TCVIS features are skipped.
-
tpi_outer_radius(int, default:100) –Outer radius for TPI calculation in meters. Defaults to 100m.
-
tpi_inner_radius(int, default:0) –Inner radius for TPI annulus kernel in meters. Set to 0 for circular kernel. Defaults to 0.
-
device(typing.Literal['cuda', 'cpu'] | int, default:darts_utils.cuda.DEFAULT_DEVICE) –Device for GPU-accelerated computations (NDVI, TPI, slope). Use "cuda" for first GPU, int for specific GPU, or "cpu". Defaults to "cuda" if available, else "cpu".
Returns:
-
xarray.Dataset–xr.Dataset: Preprocessed dataset with all input optical variables plus:
-
xarray.Dataset–Added from optical processing: - ndvi (float32): Normalized Difference Vegetation Index Attributes: long_name="NDVI"
-
xarray.Dataset–Added from TCVIS (if ds_tcvis provided): - tc_brightness (float): Tasseled Cap brightness trend - tc_greenness (float): Tasseled Cap greenness trend - tc_wetness (float): Tasseled Cap wetness trend
-
xarray.Dataset–Added from ArcticDEM (if ds_arcticdem provided): - dem (float32): Elevation in meters - relative_elevation (float32): Topographic Position Index (TPI) Attributes: long_name="Topographic Position Index (TPI)" - slope (float32): Slope in degrees [0-90] Attributes: long_name="Slope" - hillshade (uint8): Hillshade values [0-255] Attributes: long_name="Hillshade" - aspect (float32): Aspect in degrees [0-360] Attributes: long_name="Aspect" - curvature (float32): Surface curvature Attributes: long_name="Curvature" - arcticdem_data_mask (uint8): DEM validity mask
Note
Attribute usage:
- azimuth attribute from ds_optical: Used for hillshade calculation (solar azimuth angle).
Falls back to 225° if missing or invalid.
- elevation attribute from ds_optical: Used for hillshade calculation (solar elevation angle).
Falls back to 25° if missing or invalid.
Processing behavior: - If both ds_tcvis and ds_arcticdem are None, only NDVI is calculated. - ArcticDEM is buffered by tpi_outer_radius before reprojection to avoid edge effects, then cropped back to optical extent after terrain feature calculation. - Reprojection uses cubic resampling for smooth terrain features. - GPU acceleration (if device="cuda") significantly speeds up TPI and slope calculations.
Example
Complete preprocessing with all data sources:
from darts_preprocessing import preprocess_v2
from darts_acquisition import load_cdse_s2_sr_scene, load_arcticdem, load_tcvis
# Load optical data
optical = load_cdse_s2_sr_scene(s2_scene_id, ...)
# Load auxiliary data
arcticdem = load_arcticdem(optical.odc.geobox, ...)
tcvis = load_tcvis(optical.odc.geobox, ...)
# Preprocess
preprocessed = preprocess_v2(
ds_optical=optical,
ds_arcticdem=arcticdem,
ds_tcvis=tcvis,
tpi_outer_radius=100,
tpi_inner_radius=0,
device="cuda"
)
# Result contains: blue, green, red, nir, ndvi, tc_brightness, tc_greenness,
# tc_wetness, dem, relative_elevation, slope, hillshade, aspect, curvature
Source code in darts-preprocessing/src/darts_preprocessing/v2.py
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 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 | |