Skip to content

Commit

Permalink
Merge pull request #300 from Dewberry/bugfix/terrain-gen-failure
Browse files Browse the repository at this point in the history
Bugfix/terrain gen failure
  • Loading branch information
sclaw authored Jan 15, 2025
2 parents 76c4633 + f58d39c commit 35a60ad
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ripple1d/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class UnknownVerticalUnits(Exception):
"""Raised when unknown vertical units are specified."""


class RasTerrainFailure(Exception):
"""Raised when the HEC-RAS CreateTerrain executable fails to generate terrain."""


class NullTerrainError(Exception):
"""Raised when the downloaded terrain for an error is all nodata values."""

Expand All @@ -113,3 +117,4 @@ class BadConflation(Exception):
class SingleXSModel(Exception):
"""Raised when geopackage creation would yield a single cross-section model."""


8 changes: 6 additions & 2 deletions ripple1d/ops/ras_terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
TERRAIN_AGREEMENT_PRECISION,
)
from ripple1d.data_model import XS, NwmReachModel
from ripple1d.errors import RasTerrainFailure
from ripple1d.ras import RasGeomText, create_terrain
from ripple1d.utils.dg_utils import clip_raster, reproject_raster
from ripple1d.utils.ripple_utils import fix_reversed_xs, resample_vertices, xs_concave_hull
Expand Down Expand Up @@ -185,12 +186,15 @@ def create_ras_terrain(
projection_file,
f"{nwm_rm.terrain_directory}\\{nwm_rm.model_name}",
)
os.remove(src_dem_reprojected_localfile)
terrain_path = result["RAS Terrain"] + "." + map_dem_clipped_basename.replace(".vrt", ".tif")
if os.path.exists(terrain_path):
os.remove(src_dem_reprojected_localfile)
else:
raise RasTerrainFailure(f"Tried to create terrain at {terrain_path}, but CreateTerrain exe failed.")
nwm_rm.update_write_ripple1d_parameters({"source_terrain": terrain_source_url})
logging.info(f"create_ras_terrain complete")

# Calculate terrain agreement metrics
terrain_path = result["RAS Terrain"] + "." + map_dem_clipped_basename.replace(".vrt", ".tif")
agreement_path = compute_terrain_agreement_metrics(
submodel_directory,
terrain_path,
Expand Down
27 changes: 27 additions & 0 deletions tests/bad_projection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import shutil

from ripple1d.consts import MAP_DEM_UNCLIPPED_SRC_URL
from ripple1d.errors import RasTerrainFailure
from ripple1d.ops.ras_terrain import create_ras_terrain


def test_bad_prj():
"""Test if ripple will raise error when presented with an unsupported projection."""
passed = False
test_dir = os.path.join(os.path.dirname(__file__), "test-data", "3297968")
try:
result = create_ras_terrain(test_dir)
except RasTerrainFailure:
passed = True
else:
tpath = result["RAS Terrain"] + "." + os.path.basename(MAP_DEM_UNCLIPPED_SRC_URL).replace(".vrt", ".tif")
if os.path.exists(tpath):
raise RuntimeError("Terrain was generated even though test was set up for it to fail.")
finally:
shutil.rmtree(os.path.join(test_dir, "Terrain"))
assert passed, "Ras did not raise an error even though terrain was not generated."


if __name__ == "__main__":
test_bad_prj()
Binary file added tests/test-data/3297968/3297968.gpkg
Binary file not shown.
59 changes: 59 additions & 0 deletions tests/test-data/3297968/3297968.ripple1d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"us_xs": {
"river": "Halawakee Creek",
"reach": "Reach-1",
"min_elevation": 709.0,
"max_elevation": 736.31,
"xs_id": 4670.0
},
"ds_xs": {
"river": "Halawakee Creek",
"reach": "Reach-1",
"min_elevation": 683.55,
"max_elevation": 710.99,
"xs_id": 746.0
},
"eclipsed": false,
"low_flow": 18,
"high_flow": 171,
"network_to_id": "3297966",
"metrics": {
"xs": {
"centerline_offset": {
"count": 21,
"mean": 15,
"std": 9,
"min": 1,
"25%": 11,
"50%": 14,
"75%": 21,
"max": 32
},
"thalweg_offset": {
"count": 21,
"mean": 136,
"std": 58,
"min": 82,
"25%": 105,
"50%": 127,
"75%": 136,
"max": 344
}
},
"lengths": {
"ras": 3922,
"network": 3844,
"network_to_ras_ratio": 0.98
},
"coverage": {
"start": 0.48,
"end": 0.91
}
},
"overlapped_reaches": [],
"eclipsed_reaches": [],
"source_model": "C:\\collections\\mip_03130002\\source_models\\Halawakee_CreekT872\\Halawakee_CreekT872.prj",
"crs": "PROJCRS[\"NAD83(2011) / Alabama East (ftUS)\",BASEGEOGCRS[\"NAD83(2011)\",DATUM[\"NAD83 (National Spatial Reference System 2011)\",ELLIPSOID[\"GRS 1980\",6378137,298.257222101,LENGTHUNIT[\"metre\",1]]],PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],ID[\"EPSG\",6318]],CONVERSION[\"SPCS83 Alabama East zone (US survey foot)\",METHOD[\"Transverse Mercator\",ID[\"EPSG\",9807]],PARAMETER[\"Latitude of natural origin\",30.5,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8801]],PARAMETER[\"Longitude of natural origin\",-85.8333333333333,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8802]],PARAMETER[\"Scale factor at natural origin\",0.99996,SCALEUNIT[\"unity\",1],ID[\"EPSG\",8805]],PARAMETER[\"False easting\",656166.667,LENGTHUNIT[\"US survey foot\",0.304800609601219],ID[\"EPSG\",8806]],PARAMETER[\"False northing\",0,LENGTHUNIT[\"US survey foot\",0.304800609601219],ID[\"EPSG\",8807]]],CS[Cartesian,2],AXIS[\"easting (X)\",east,ORDER[1],LENGTHUNIT[\"US survey foot\",0.304800609601219]],AXIS[\"northing (Y)\",north,ORDER[2],LENGTHUNIT[\"US survey foot\",0.304800609601219]],USAGE[SCOPE[\"Engineering survey, topographic mapping.\"],AREA[\"United States (USA) - Alabama east of approximately 86\u00b037'W - counties Barbour; Bullock; Calhoun; Chambers; Cherokee; Clay; Cleburne; Coffee; Coosa; Covington; Crenshaw; Dale; De Kalb; Elmore; Etowah; Geneva; Henry; Houston; Jackson; Lee; Macon; Madison; Marshall; Montgomery; Pike; Randolph; Russell; StClair; Talladega; Tallapoosa.\"],BBOX[30.99,-86.79,35,-84.89]],ID[\"EPSG\",9748]]",
"version": "0.7.0",
"source_terrain": "C:\\reference_data\\seamless_3dep_dem_3m_5070.vrt"
}

0 comments on commit 35a60ad

Please sign in to comment.