terrain¶
Terrain discovery and consolidation for HEC-RAS projects.
Overview¶
The terrain module discovers terrain layers from a HEC-RAS project's rasmap configuration and can consolidate multiple terrain TIFFs into a single merged raster. This is useful for:
- Inspecting terrain configuration: Enumerate all terrain layers, their CRS, resolution, and file locations
- Consolidating terrain: Merge multiple terrain tiles into a single file for simplified workflows
- Downsampling: Reduce terrain resolution for faster mapping or smaller file sizes
- 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)
- Harmonize CRS: If TIFs have different but equivalent CRS representations, reprojects to match the first TIF's CRS
- Merge via
rasterio.merge.merge(method='first')— first terrain wins in overlapping areas - Optionally downsample — reduce resolution by a factor or to a target cell size
- 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
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 | |
ras2cng.terrain.consolidate_terrain(project_path, output_dir, *, terrain_name='Consolidated', downsample_factor=None, target_resolution=None, terrain_names=None, 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]]
|
Specific terrain names to include (None = all from rasmap) |
None
|
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
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 | |