1- import datetime
21import io
32import json
3+ import logging
44import sys
5+ from datetime import datetime
56from pathlib import Path
67
78import pandas as pd
1415from ras_stac .ras1d .utils .classes import (
1516 GenericAsset ,
1617 GeometryAsset ,
18+ NullGeometryAsset ,
1719 PlanAsset ,
1820 SteadyFlowAsset ,
1921 ThumbAsset ,
@@ -48,6 +50,8 @@ def export_stac(self, output_path: str) -> None:
4850 def export_thumbnail (self , thumb_path : str ) -> None :
4951 """Generate STAC thumbnail, save to S3, and log path."""
5052 gdfs = self .primary_geometry .gdfs
53+ if len (gdfs ) == 0 or "null" in gdfs :
54+ return
5155 thumb = make_thumbnail (gdfs )
5256 if file_location (thumb_path ) == "local" :
5357 thumb .savefig (thumb_path , dpi = 80 )
@@ -72,15 +76,16 @@ def stac_item(self) -> dict:
7276 )
7377 stor_ext = StorageExtension .ext (stac , add_if_missing = True )
7478 stor_ext .apply (platform = "AWS" , region = "us-east-1" )
75- prj_ext = AssetProjectionExtension .ext (stac , add_if_missing = True )
76- og_crs = CRS (self .crs )
77- prj_ext .apply (
78- epsg = og_crs .to_epsg (),
79- wkt2 = og_crs .to_wkt (),
80- geometry = self .get_footprint (),
81- bbox = self .get_bbox (),
82- centroid = to_geojson (self .get_centroid ()),
83- )
79+ if self .crs :
80+ prj_ext = AssetProjectionExtension .ext (stac , add_if_missing = True )
81+ og_crs = CRS (self .crs )
82+ prj_ext .apply (
83+ epsg = og_crs .to_epsg (),
84+ wkt2 = og_crs .to_wkt (),
85+ geometry = self .get_footprint (),
86+ bbox = self .get_bbox (),
87+ centroid = to_geojson (self .get_centroid ()),
88+ )
8489 return stac
8590
8691 @property
@@ -139,6 +144,7 @@ def stac_properties(self):
139144 "datetime_source" : "processing_time" if self .primary_geometry .last_update is None else "model_geometry" ,
140145 "assigned_HUC8" : self .huc8 ,
141146 "has_2d" : any ([a .has_2d for a in self .assets if isinstance (a , GeometryAsset )]),
147+ "has_1d" : any ([a .has_1d for a in self .assets if isinstance (a , GeometryAsset )]),
142148 }
143149 for p in self .custom_properties :
144150 properties [p ] = self .custom_properties [p ]
@@ -177,7 +183,16 @@ def primary_plan(self) -> PlanAsset:
177183 @property
178184 def primary_geometry (self ) -> GeometryAsset :
179185 """The geometry file listed in the primary plan"""
180- return self .extension_dict [self .primary_plan .geometry ]
186+ if not self .crs :
187+ return NullGeometryAsset ()
188+ try :
189+ geom = self .extension_dict [self .primary_plan .geometry ]
190+ except Exception :
191+ return NullGeometryAsset ()
192+ if not geom .has_1d :
193+ return NullGeometryAsset ()
194+ else :
195+ return geom
181196
182197 def check_for_mip (self ) -> None :
183198 mip_data = [a for a in self .assets if a .name == "mip_package_geolocation_metadata.json" ]
@@ -214,18 +229,24 @@ def ras_to_stac(ras_dir: str, crs: str):
214229
215230def process_in_place_s3 (in_prefix : str , crs : str , out_prefix : str ):
216231 """Convert a HEC-RAS model to a STAC item and save to same directory."""
232+ logging .info (f"Processing model with crs { crs } at prefix { in_prefix } " )
233+ logging .info ("Discovering model contents" )
217234 converter = from_directory (in_prefix , crs )
218235 converter .check_for_mip ()
219236 thumb_path = out_prefix + "Thumbnail.png"
237+ logging .info (f"Generating thumbnail at { thumb_path } " )
220238 converter .export_thumbnail (thumb_path )
221239 stac_path = out_prefix + f"{ converter .idx } .json"
240+ logging .info (f"Generating STAC item at { thumb_path } " )
222241 converter .export_stac (stac_path )
223242 return {"in_path" : in_prefix , "crs" : crs , "thumb_path" : thumb_path , "stac_path" : stac_path }
224243
225244
226245if __name__ == "__main__" :
227246 ras_dir = sys .argv [1 ]
228247 crs = sys .argv [2 ]
248+ if crs == "None" :
249+ crs = None
229250 out_dir = sys .argv [3 ]
230- process_in_place_s3 (ras_dir , crs , out_dir )
231- # ras_to_stac(ras_dir, crs)
251+ # process_in_place_s3(ras_dir, crs, out_dir)
252+ ras_to_stac (ras_dir , crs )
0 commit comments