solshade.terrain¶
compute_hillshade
¶
compute_hillshade(slope, aspect, azimuth_deg=315.0, altitude_deg=45.0)
Compute a hillshade array using slope and aspect with Lambertian illumination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slope
|
DataArray
|
Slope in degrees. |
required |
aspect
|
DataArray
|
Aspect in degrees clockwise from north. |
required |
azimuth_deg
|
float
|
Azimuth angle of the sun (0° = north, 90° = east). Default is 315°. |
315.0
|
altitude_deg
|
float
|
Altitude angle of the sun above the horizon. Default is 45°. |
45.0
|
Returns:
| Name | Type | Description |
|---|---|---|
hillshade |
DataArray
|
Normalized hillshade values from 0 (dark) to 1 (bright), preserving coordinates and CRS metadata. |
Notes
- Uses a Lambertian reflection model.
- All input and output arrays are 2D.
Logging
- DEBUG: sun azimuth/altitude inputs.
- INFO: completion of hillshade.
Source code in src/solshade/terrain.py
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 | |
compute_horizon_map
¶
compute_horizon_map(dem, n_directions=360, max_distance=5000, step=20, chunk_size=32, n_jobs=-1, progress=True)
Compute a per-pixel horizon angle map from a digital elevation model (DEM) using chunked ray tracing.
For each pixel, rays are cast in n_directions azimuthal directions
and sampled up to max_distance away. The maximum elevation angle
encountered along each ray is recorded as the local horizon.
Parallel processing is done with Joblib, and a rich progress bar can optionally be shown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dem
|
DataArray
|
Input digital elevation model with a defined CRS and affine transform. Must be a 2D array with shape (y, x) and spatial coordinates. |
required |
n_directions
|
int
|
Number of azimuthal directions to trace (default is 64). |
360
|
max_distance
|
float
|
Maximum distance (in meters) to trace each ray from each pixel (default is 5000 m). |
5000
|
step
|
float
|
Distance step (in meters) between ray samples (default is 20 m). |
20
|
chunk_size
|
int
|
Size (in pixels) of square chunks to process independently (default is 32). |
32
|
n_jobs
|
int
|
Number of parallel jobs to run. Use -1 to use all available cores (default is -1). |
-1
|
progress
|
bool
|
If True, display a rich progress bar (default is True). |
True
|
Returns:
| Type | Description |
|---|---|
DataArray
|
A 3D xarray DataArray with dimensions (azimuth, y, x), representing
the local horizon angle (in degrees) in each direction for each pixel.
The azimuthal directions are stored in the |
Logging
- INFO: parameters used and final shape.
- DEBUG: chunking details, dispatching jobs, pixel sizes.
- WARN: defensive warnings if a None chunk were returned.
Source code in src/solshade/terrain.py
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 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
compute_slope_aspect_normals
¶
compute_slope_aspect_normals(dem)
Compute slope, aspect, and ENU unit normal vectors from a DEM.
Slope and aspect follow GIS conventions: - Slope: angle from horizontal (0° flat, 90° vertical). - Aspect: compass direction of steepest descent, clockwise from North (0°=N, 90°=E).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dem
|
DataArray
|
2D DEM with projected CRS and linear units (e.g., meters). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
slope |
DataArray(y, x)
|
Slope in degrees. |
aspect |
DataArray(y, x)
|
Aspect in degrees clockwise from North. |
normal_enu |
DataArray(3, y, x)
|
ENU unit normal vectors. Bands: [east, north, up]. |
Notes
Normal vector components: E = sin(slope) * sin(aspect) N = sin(slope) * cos(aspect) U = cos(slope)
Logging
- DEBUG: DEM shape and pixel resolution.
- INFO: completion of slope/aspect/normal computation.
Source code in src/solshade/terrain.py
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 | |
horizon_interp
¶
horizon_interp(azimuths, horizon_vals, fallback_frac=0.5)
Interpolate horizon angles as a function of azimuth using cubic interpolation, ensuring periodic continuity. Falls back to linear if data is sparse or if cubic interpolation yields unphysical results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
azimuths
|
ndarray
|
1D array of azimuth angles in degrees [0, 360). |
required |
horizon_vals
|
ndarray
|
1D array of horizon angles (in degrees), same shape as azimuths. May contain NaNs. |
required |
fallback_frac
|
float
|
Threshold fraction of NaNs above which to fall back to linear interpolation. |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
interp_func |
callable or None
|
A function that takes azimuth(s) in degrees and returns interpolated horizon angle(s). Returns None if no valid data. |
Logging
- DEBUG: data completeness, ranges, and interpolation path taken.
- INFO: chosen interpolation (cubic vs linear) and output range when applicable.
- WARN: cubic spline unphysical output fallback.
Source code in src/solshade/terrain.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
load_dem
¶
load_dem(path)
Load a single-band Digital Elevation Model (DEM) from a GeoTIFF file.
Uses rioxarray to preserve CRS and coordinate metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to a single-band GeoTIFF file containing elevation data. The file must contain exactly one raster band. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dem |
DataArray
|
A 2D array of elevation values with dimensions (y, x), including CRS, transform, and coordinate metadata. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input file contains more than one band or does not reduce to a DataArray. |
Notes
- Elevation units are inherited from the input file (typically meters).
- The spatial reference (CRS) must be projected (not geographic/latlon).
- Squeezes band dimension if present.
- Logging: this function logs basic file/metadata information at INFO level.
Source code in src/solshade/terrain.py
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 | |