Terrain#
Terrain visualisation — color relief, hill shade, slope, and aspect via GDAL DEMProcessing. Subclasses pyramids.dataset.Dataset, so all pyramids methods are inherited.
digitalrivers.terrain.Terrain
#
Bases: Dataset
Terrain analysis tools built on GDAL DEMProcessing.
Wraps a single- or multi-band raster and exposes convenience methods for color relief, hill shade, slope, and aspect computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster
|
str | Dataset
|
File path or GDAL dataset to open. |
required |
access
|
str
|
|
'read_only'
|
Source code in src/digitalrivers/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 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 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 400 401 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
color_relief(band=0, path=None, color_table=None, **kwargs)
#
Create a color relief for a band in the Dataset.
A color relief raster is a raster image where each pixel's value is mapped to a specific color based on a predefined color palette or color table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
band
|
int
|
int, default is 0. band index. |
0
|
path
|
str
|
str, default is None. path to save the color relief raster. |
None
|
color_table
|
DataFrame
|
DataFrame, default is None. DataFrame with columns: band, values, color or DataFrame with columns: values, red, green, blue, alpha, (the alpha column is optional) |
None
|
Returns: Dataset: Dataset with the color relief with four bands read, green, blue, and alpha.
Examples:
- First create a one band dataset, consisting of 10 columns and 10 rows, with random values between 0 and 15.
- Now let's create the color table using hex colors.
- Now let's create the color relief for the dataset using the color table
DataFrame.>>> color_relief = dataset.color_relief(band=0, color_table=df) >>> print(color_relief) # doctest: +SKIP <BLANKLINE> Cell size: 0.05 Dimension: 10 * 10 EPSG: 4326 Number of Bands: 4 Band names: ['Band_1', 'Band_2', 'Band_3', 'Band_4'] Mask: None Data type: byte projection: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]] Metadata: {} File: ... <BLANKLINE> >>> print(color_relief.band_color) {0: 'red', 1: 'green', 2: 'blue', 3: 'alpha'} - The result color relief dataset will have 4 bands red, green, blue, and alpha. with values from 0 to 255.
- To plot the color relief dataset, you can use the
plotmethod. but you need to provide the the rgb indices with the alpha index as the fourth index, otherwise the alpha band will be missing.
See Also
Dataset.hill_shade: create a hill-shade for a band in the Dataset.
Source code in src/digitalrivers/terrain.py
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 | |
hill_shade(band=0, azimuth=315, altitude=45, vertical_exaggeration=1, scale=1, path=None, weights=None, **kwargs)
#
Create hill-shade.
Hillshade is a technique used in digital elevation modeling (DEM) to create a grayscale representation of a terrain's surface that simulates the effect of sunlight falling across the landscape. This technique helps to visualize the shape and features of the terrain by highlighting the variations in elevation and the slope of the surface.
Hillshade calculates the illumination of each pixel based on the slope (gradient) and aspect (direction) of the terrain surface relative to a specified light source.
The main parameters influencing the hillshade effect are: - Light source direction (Azimuth): the azimuth angle of the light source, which is the angle between the light source - Light source elevation (altitude): the source of light elevation, it is measured in degrees from the horizon. - Vertical exaggeration (Z-factor): the vertical exaggeration is used to emphasize the vertical features of the terrain.
Notes
if the hill_shade parameters are given as lists then the hill shade will be calculated for each set
of parameter and then the average will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
band
|
int
|
int band index. |
0
|
azimuth
|
int | float | list[int]
|
int | float | list[int] The source of light direction, it is measured clockwise from the north. zero means from north to south. 45 degrees means from the northeast to the southwest. |
315
|
altitude
|
int | float | list[int]
|
int | float | list[int] The source of light elevation, it is measured in degrees from the horizon. zero means from the horizon. 90 degrees means from the zenith. the overall image gets brighter as the light source gets closer to the zenith. The brightest slopes/DEM features will be perpendicular to the light source, and the darkest will be angled 90˚ or more away. |
45
|
vertical_exaggeration
|
int | float | list[int]
|
int | float | list[int] Vertical exaggeration, the vertical exaggeration It is used to emphasize the vertical features of the terrain. |
1
|
scale
|
int | float | list[int]
|
int | float | list[int] the scale is the ratio of vertical units to horizontal. If the horizontal unit of the source DEM is degrees (e.g Lat/Long WGS84 projection), you can use scale=111120 if the vertical units are meters (or scale=370400 if they are in feet). |
1
|
path
|
str
|
str, optional, default is None path to save the hill-shade raster. |
None
|
weights
|
list[int]
|
list[int], default is None. list of weights to combine the hill-shades if the other parameters are given as lists, an average hill shade will be calculated based on the weights. if None, the weights will be equal. |
None
|
**kwargs
|
multi_directional: bool
if True, the hill shade will be calculated for multiple azimuth values [225, 270, 315, 360] each with a
altitude of 30 degrees, and then the average will be returned. with multi_directional = True any given
azimuth will be ignored.
For more details visit: https://pubs.usgs.gov/of/1992/of92-422/of92-422.pdf
combined: bool
combined shading, a combination of slope and oblique shading.
igor: bool
shading which tries to minimize effects on other map features beneath. with |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
'Dataset'
|
8-bit Dataset with the hill-shade created. |
Examples:
- First create a one band dataset, consisting of 10 columns and 10 rows, with random values between 0 and 15.
>>> import numpy as np >>> arr = np.random.randint(0, 15, size=(100, 100)) >>> dataset = Dataset.create_from_array(arr, top_left_corner=(0, 0), cell_size=0.05, epsg=4326) >>> hill_shade = dataset.hill_shade( ... band=0, altitude=45, azimuth=315, vertical_exaggeration=1, scale=1 ... ) >>> print(hill_shade.dtype) # doctest: +SKIP ['byte'] >>> hill_shade.plot() # doctest: +SKIP
- You can also provide the function with a list os values for each parameter, then the functions will
calculate the hill shade for each set of parameters and then the average will be returned.
>>> hill_shade = dataset.hill_shade( ... band=0, azimuth=[315, 45], altitude=[45, 45], vertical_exaggeration=[1, 1], scale=[1, 1] ... ) >>> hill_shade.plot() # doctest: +SKIP
See Also
Dataset.color_relief: create a color relief for a band in the Dataset. Dataset.slope: create a slope for a band in the Dataset.
Source code in src/digitalrivers/terrain.py
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 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 | |
slope(band=0, scale=1, slope_format='degree', path=None, algorithm=None, creation_options=None, **kwargs)
#
Compute the slope of the terrain surface.
Uses GDAL DEMProcessing to calculate the slope (rate of
elevation change) for every cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
band
|
int
|
Zero-based band index. Defaults to 0. |
0
|
scale
|
int | float | list[int]
|
Ratio of vertical to horizontal units. Use
|
1
|
slope_format
|
str
|
Output format — |
'degree'
|
algorithm
|
str
|
Slope algorithm. One of |
None
|
path
|
str
|
If given, write the result to this GeoTIFF path. Otherwise the raster is created in memory. |
None
|
creation_options
|
list[str]
|
GDAL creation options. Defaults to
|
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
'Dataset'
|
Single-band |
Examples:
- First create a one band dataset, consisting of 10 columns and 10 rows, with random values between 0 and 15.
- Now let's create the slope for the dataset.

See Also
Terrain.hill_shade: Create a hill-shade for a band in the Dataset. Terrain.color_relief: Create a color relief for a band in the Dataset.
Source code in src/digitalrivers/terrain.py
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 400 401 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 | |
aspect(band=0, scale=1, vertical_exaggeration=1, zero_flat_surface=False, algorithm=None, path=None, creation_options=None, **kwargs)
#
Compute the aspect (slope direction) of the terrain surface.
Uses GDAL DEMProcessing to calculate the compass direction
of the steepest downhill slope for every cell. Values range
from 0° (north) clockwise to 360°.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
band
|
int
|
Zero-based band index. Defaults to 0. |
0
|
scale
|
int | float | list[int]
|
Ratio of vertical to horizontal units. Use
|
1
|
vertical_exaggeration
|
int | float | list[int]
|
Z-factor used to emphasise vertical features. Defaults to 1. |
1
|
zero_flat_surface
|
bool
|
If |
False
|
algorithm
|
str
|
Aspect algorithm. One of |
None
|
path
|
str
|
If given, write the result to this GeoTIFF path. Otherwise the raster is created in memory. |
None
|
creation_options
|
list[str]
|
GDAL creation options. Defaults to
|
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
'Dataset'
|
Single-band |
Examples:
- Create a small raster and compute its aspect.
- Compute the aspect raster.

See Also
Terrain.hill_shade: Create a hill-shade for a band in the Dataset. Terrain.slope: Compute the slope of the terrain surface.
Source code in src/digitalrivers/terrain.py
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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |