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)
- Native modification-aware export: Ask RAS Mapper to consolidate a registered terrain into one validated GeoTIFF while preserving source priority, stitches, masks, and vector modifications
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).
Native Registered-Terrain Export¶
export_modified_terrain() is the production path for a single,
modification-aware terrain TIFF. It delegates to
RasTerrain.export_rasmapper_terrain() rather than selecting the first source
TIFF or reconstructing terrain-modification mathematics in Python. RAS Mapper
performs the consolidation and nearest-neighbor resampling on the authoritative
source grid. Exact source-derived scale factors are 1, 2, 4, and 8.
The output is committed only after ras-commander semantically validates it; a
JSON receipt is written beside the TIFF. Existing output and receipt files are
protected unless overwrite=True (or --overwrite in the CLI).
The optional geometry argument is deprecated and ignored because native
registered-terrain export does not consume a geometry HDF. terrain_name may
be omitted only when the project registers exactly one terrain; ambiguous
projects must select the exact RAS Mapper terrain name. The compatibility
argument is scheduled for removal in ras2cng 1.1.
This path uses ras-commander 0.100.0 or newer, which exposes
RasTerrain.export_rasmapper_terrain(). An older installation receives an
explicit upgrade error instead of falling back to the numerically different
row sampler.
Qualified runtimes are HEC-RAS 6.4.1, 6.5, 6.6, and 7.0.1 on native Windows and Wine. HEC-RAS 6.3, 6.4.0, 6.7 beta builds, and 7.0.0 are rejected. Stable 7.1 is forward-open: ras-commander will accept it only when the installed binary satisfies the exact managed API contract.
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
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 | |
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
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 | |
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.export_modified_terrain(project_path, output_tif, *, geometry=None, terrain_name=None, downsample_factor=1, overwrite=False)
¶
Export a registered terrain with modifications applied as one GeoTIFF.
Delegates consolidation, grid alignment, nearest-neighbor downsampling, and vector-modification rasterization to RAS Mapper's native terrain export. Registered source priority, stitches, and masks are therefore preserved.
HEC-RAS 6.4.1, 6.5, 6.6, and 7.0.1 are qualified on Windows and Wine. Stable 7.1 is forward-open subject to ras-commander's managed-contract check. Unsupported project/runtime versions fail before native execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_path
|
Path
|
Path to .prj file or project directory |
required |
output_tif
|
Path
|
Output GeoTIFF path |
required |
geometry
|
Optional[str]
|
Deprecated compatibility argument. Native registered-terrain export does not use a geometry HDF, so any supplied value is ignored. |
None
|
terrain_name
|
Optional[str]
|
Exact registered terrain name. May be omitted only when the project registers exactly one terrain. |
None
|
downsample_factor
|
int
|
Exact source-derived factor: 1, 2, 4, or 8. |
1
|
overwrite
|
bool
|
Replace an existing output and receipt when true. |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the output GeoTIFF |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If ras-commander lacks native terrain export support or RAS Mapper reports an operational failure. |
Source code in ras2cng/terrain.py
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
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
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 640 641 642 | |