terrain¶
Terrain discovery and consolidation for HEC-RAS projects.
Overview¶
The terrain module discovers named terrain layers from a HEC-RAS project's rasmap configuration and consolidates each surface's TIFF members independently. This is useful for:
- Inspecting terrain configuration: Enumerate all terrain layers, their CRS, resolution, and file locations
- Consolidating terrain: Merge the TIFF members of each named terrain into its own authoritative file
- Downsampling without upsampling: Select a whole native-cell multiple with a 5 ft publication floor
- Recovering relocated projects: Consolidate an explicit, priority-ordered TIFF list when stored RASMapper paths cannot be resolved on the processing host
- Publishing source construction: Export source TIFF footprints and terrain-modification vectors
- Creating HEC-RAS terrain HDFs: Generate new terrain HDF files via RasProcess.exe (required for result mapping)
How Terrain Discovery Works¶
- Reads the project's
.rasmapfile to get terrain names in priority order - For each terrain name, locates the corresponding
.hdffile in theTerrain/directory - Discovers associated
.tiffiles by matching the HDF stem against TIF file names - Optionally reads CRS and resolution from TIF files using rasterio
Terrain Name Matching¶
TIF files are associated with a terrain by matching the file stem against the terrain name. The matching is case-insensitive and allows suffixes separated by ., _, or -:
| TIF Stem | Terrain Name | Match? |
|---|---|---|
Terrain50 |
Terrain50 |
Yes (exact) |
Terrain50.muncie_clip |
Terrain50 |
Yes (dot separator) |
Terrain50_tile2 |
Terrain50 |
Yes (underscore separator) |
Terrain50-highres |
Terrain50 |
Yes (dash separator) |
Terrain50WithChannel |
Terrain50 |
No (alphanumeric continuation) |
How Terrain Consolidation Works¶
- Discover terrain TIFs from rasmap (priority ordered)
- Keep terrain names separate: different named RASMapper terrains are never merged implicitly
- Choose a target grid: preserve native resolution at or above 5 ft; otherwise use the smallest whole native-cell multiple at or above 5 ft. A mixed-resolution mosaic requires an explicit target that is a whole multiple of its coarsest native grid; every source factor is retained in provenance.
- Merge by windows: reproject each member to the target grid and let the first RASMapper source win in overlaps without allocating the full mosaic in memory
- Optionally create HEC-RAS terrain HDF via
RasTerrain.create_terrain_from_rasters()(requires RasProcess.exe) - Optionally register the new terrain in the project's rasmap
Steps 5-6 require RasProcess.exe (Windows or Wine). Steps 1-4 are pure Python (rasterio).
API Reference¶
ras2cng.terrain.TerrainInfo
dataclass
¶
Information about a single terrain layer discovered from a RAS project.
Source code in ras2cng/terrain.py
ras2cng.terrain.discover_terrains(project_path)
¶
Discover terrain layers from rasmap in priority order.
Uses RasMap.get_terrain_names() + rasmap_df['terrain_hdf_path']. For each terrain HDF, discovers associated .tif files in same directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_path
|
Path
|
Path to .prj file or project directory |
required |
Returns:
| Type | Description |
|---|---|
list[TerrainInfo]
|
List of TerrainInfo in rasmap priority order |
Source code in ras2cng/terrain.py
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 | |
ras2cng.terrain.consolidate_terrain(project_path, output_dir, *, terrain_name='Consolidated', downsample_factor=None, target_resolution=None, terrain_names=None, horizontal_units='Feet', units='Feet', ras_version='6.6', create_hdf=True, register_rasmap=True)
¶
Consolidate project terrains and create a new HEC-RAS terrain HDF.
Full pipeline: 1. Discover terrain TIFFs from rasmap (priority ordered) 2. Merge via rasterio.merge.merge(method='first') -- first wins in overlaps 3. Optionally downsample (reduce resolution) 4. Create HEC-RAS terrain HDF via RasTerrain.create_terrain_from_rasters() 5. Register new terrain in rasmap via RasMap.add_terrain_layer()
Steps 4-5 require RasProcess.exe. If create_hdf=False, only produces the merged TIFF (useful for exporting to cloud-native COG pipeline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_path
|
Path
|
Path to .prj file or project directory |
required |
output_dir
|
Path
|
Directory for output terrain files |
required |
terrain_name
|
str
|
Name for the consolidated terrain (default: "Consolidated") |
'Consolidated'
|
downsample_factor
|
Optional[float]
|
Factor to reduce resolution (2.0 = half resolution) |
None
|
target_resolution
|
Optional[float]
|
Target cell size in project units (overrides downsample_factor) |
None
|
terrain_names
|
Optional[list[str]]
|
One named terrain to consolidate. Required when the project contains multiple named terrains. |
None
|
horizontal_units
|
str
|
Horizontal raster units (Feet or Meters), used to enforce the five-foot publication floor. |
'Feet'
|
units
|
str
|
Vertical units "Feet" or "Meters" |
'Feet'
|
ras_version
|
str
|
HEC-RAS version for RasProcess.exe |
'6.6'
|
create_hdf
|
bool
|
If True, create HEC-RAS terrain HDF (requires RasProcess.exe) |
True
|
register_rasmap
|
bool
|
If True, register new terrain in project rasmap |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to consolidated terrain HDF (if create_hdf) or TIFF (if not) |
Source code in ras2cng/terrain.py
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 | |
ras2cng.terrain.consolidate_terrain_files(tif_files, output_dir, *, terrain_name='Consolidated', source_terrain_name=None, downsample_factor=None, target_resolution=None, horizontal_units='Feet', source_paths=None)
¶
Consolidate an explicit, priority-ordered terrain TIFF mosaic.
This entry point supports projects whose RAS Mapper paths cannot be resolved on the processing host. The first source wins where valid pixels overlap, and the normal no-upsample publication policy is always enforced.
Source code in ras2cng/terrain.py
ras2cng.terrain.consolidate_project_terrains(project_path, output_dir, *, target_resolutions=None, horizontal_units='Feet')
¶
Consolidate TIFF members separately for every named project terrain.
Source code in ras2cng/terrain.py
ras2cng.terrain.extract_terrain_source_footprints(tif_files, *, out_crs=None)
¶
Build one queryable footprint polygon per native terrain TIFF member.
Source code in ras2cng/terrain.py
ras2cng.terrain.extract_terrain_modification_layers(terrain_hdf_path, *, crs=None)
¶
Read RASMapper terrain modification vectors from a terrain HDF.
Source code in ras2cng/terrain.py
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |