1
- import datetime
2
1
import io
3
2
import json
3
+ import logging
4
4
import sys
5
+ from datetime import datetime
5
6
from pathlib import Path
6
7
7
8
import pandas as pd
14
15
from ras_stac .ras1d .utils .classes import (
15
16
GenericAsset ,
16
17
GeometryAsset ,
18
+ NullGeometryAsset ,
17
19
PlanAsset ,
18
20
SteadyFlowAsset ,
19
21
ThumbAsset ,
@@ -48,6 +50,8 @@ def export_stac(self, output_path: str) -> None:
48
50
def export_thumbnail (self , thumb_path : str ) -> None :
49
51
"""Generate STAC thumbnail, save to S3, and log path."""
50
52
gdfs = self .primary_geometry .gdfs
53
+ if len (gdfs ) == 0 or "null" in gdfs :
54
+ return
51
55
thumb = make_thumbnail (gdfs )
52
56
if file_location (thumb_path ) == "local" :
53
57
thumb .savefig (thumb_path , dpi = 80 )
@@ -72,15 +76,16 @@ def stac_item(self) -> dict:
72
76
)
73
77
stor_ext = StorageExtension .ext (stac , add_if_missing = True )
74
78
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
+ )
84
89
return stac
85
90
86
91
@property
@@ -139,6 +144,7 @@ def stac_properties(self):
139
144
"datetime_source" : "processing_time" if self .primary_geometry .last_update is None else "model_geometry" ,
140
145
"assigned_HUC8" : self .huc8 ,
141
146
"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 )]),
142
148
}
143
149
for p in self .custom_properties :
144
150
properties [p ] = self .custom_properties [p ]
@@ -177,7 +183,16 @@ def primary_plan(self) -> PlanAsset:
177
183
@property
178
184
def primary_geometry (self ) -> GeometryAsset :
179
185
"""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
181
196
182
197
def check_for_mip (self ) -> None :
183
198
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):
214
229
215
230
def process_in_place_s3 (in_prefix : str , crs : str , out_prefix : str ):
216
231
"""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" )
217
234
converter = from_directory (in_prefix , crs )
218
235
converter .check_for_mip ()
219
236
thumb_path = out_prefix + "Thumbnail.png"
237
+ logging .info (f"Generating thumbnail at { thumb_path } " )
220
238
converter .export_thumbnail (thumb_path )
221
239
stac_path = out_prefix + f"{ converter .idx } .json"
240
+ logging .info (f"Generating STAC item at { thumb_path } " )
222
241
converter .export_stac (stac_path )
223
242
return {"in_path" : in_prefix , "crs" : crs , "thumb_path" : thumb_path , "stac_path" : stac_path }
224
243
225
244
226
245
if __name__ == "__main__" :
227
246
ras_dir = sys .argv [1 ]
228
247
crs = sys .argv [2 ]
248
+ if crs == "None" :
249
+ crs = None
229
250
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