1010import requests
1111from requests import Session , Response
1212
13- from isamples_export_client .duckdb_utilities import GeoFeaturesResult , read_geo_features_from_jsonl
13+ from isamples_export_client .duckdb_utilities import (
14+ GeoFeaturesResult ,
15+ TemporalExtent ,
16+ read_geo_features_from_jsonl ,
17+ get_temporal_extent_from_jsonl
18+ )
1419from isamples_export_client .geoparquet_utilities import write_geoparquet_from_json_lines
1520
1621GEOPARQUET = "geoparquet"
2631
2732STAC_COLLECTION_TYPE = "Collection"
2833STAC_VERSION = "1.0.0"
34+ STAC_DEFAULT_LICENSE = "CC-BY-4.0" # https://spdx.org/licenses/CC-BY-4.0.html
2935COLLECTION_ID = "isamples-stac-collection-"
3036COLLECTION_DESCRIPTION = """The Internet of Samples (iSamples) is a multi-disciplinary and multi-institutional
3137project funded by the National Science Foundation to design, develop, and promote service infrastructure to uniquely,
@@ -41,6 +47,13 @@ def datetime_to_solr_format(dt):
4147 return dt .strftime (SOLR_TIME_FORMAT )
4248
4349
50+ class JsonDateTimeEncoder (json .JSONEncoder ):
51+ def default (self , o ):
52+ if isinstance (o , datetime .datetime ):
53+ return o .isoformat (timespec = "seconds" )
54+ return json .JSONEncoder .default (self , o )
55+
56+
4457class ExportJobStatus (Enum ):
4558 CREATED = "created"
4659 STARTED = "started"
@@ -185,7 +198,15 @@ def write_manifest(self, query: str, uuid: str, tstarted: datetime.datetime, num
185198 f .write (json .dumps (manifests , indent = 4 ))
186199 return manifest_path
187200
188- def write_stac (self , uuid : str , tstarted : datetime .datetime , geo_result : GeoFeaturesResult , solr_query : str , json_file_path : str , parquet_file_path : str ) -> str :
201+ def write_stac (
202+ self ,
203+ uuid : str ,
204+ tstarted : datetime .datetime ,
205+ geo_result : GeoFeaturesResult ,
206+ temporal_result : TemporalExtent ,
207+ solr_query : str ,
208+ json_file_path : str ,
209+ parquet_file_path : str ) -> str :
189210 assets_dict = {
190211 }
191212 description_string = (
@@ -210,8 +231,17 @@ def write_stac(self, uuid: str, tstarted: datetime.datetime, geo_result: GeoFeat
210231 "type" : STAC_COLLECTION_TYPE ,
211232 "id" : f"iSamples Export Service result { uuid } " ,
212233 "collection" : f"{ COLLECTION_TITLE } { uuid } " ,
213- "geometry" : geo_result .geo_json_dict ,
214- "bbox" : geo_result .bbox ,
234+ "license" : STAC_DEFAULT_LICENSE ,
235+ "extent" : {
236+ "spatial" : {
237+ "bbox" : [geo_result .bbox ,]
238+ },
239+ "temporal" : {
240+ "interval" : [
241+ temporal_result
242+ ]
243+ }
244+ },
215245 "properties" : {
216246 "datetime" : datetime_to_solr_format (tstarted )
217247 },
@@ -305,8 +335,8 @@ def write_stac(self, uuid: str, tstarted: datetime.datetime, geo_result: GeoFeat
305335 "assets" : assets_dict
306336 }
307337 stac_path = ExportClient ._stac_file_path (self ._destination_directory )
308- with open (stac_path , "w" ) as f :
309- f . write ( json .dumps (stac_item , indent = 4 ) )
338+ with open (stac_path , "w" , encoding = "UTF-8" ) as f :
339+ json .dump (stac_item , f , indent = 4 , ensure_ascii = False , cls = JsonDateTimeEncoder )
310340 return stac_path
311341
312342 def perform_full_download (self ):
@@ -334,13 +364,14 @@ def perform_full_download(self):
334364 manifest_path = self .write_manifest (self ._query , uuid , tstarted , num_results )
335365 logging .info (f"Successfully wrote manifest file to { manifest_path } " )
336366 geo_result = read_geo_features_from_jsonl (filename )
367+ temporal_result = get_temporal_extent_from_jsonl (filename )
337368 parquet_filename = None
338369 if self .is_geoparquet :
339370 parquet_filename = write_geoparquet_from_json_lines (filename )
340371 query_string = status_json .get ("query" ).replace ("'" , "\" " )
341372 solr_query_dict = json .loads (query_string )
342373 query = solr_query_dict .pop ("q" )
343- stac_path = self .write_stac (uuid , tstarted , geo_result , query , filename , parquet_filename )
374+ stac_path = self .write_stac (uuid , tstarted , geo_result , temporal_result , query , filename , parquet_filename )
344375 logging .info (f"Successfully wrote stac item to { stac_path } " )
345376 break
346377 except Exception as e :
0 commit comments