41
41
from weaver .wps_restapi import swagger_definitions as sd # isort:skip # noqa: E402
42
42
43
43
if TYPE_CHECKING :
44
- from typing import List
44
+ from typing import Dict , List
45
45
46
46
from pystac import Asset
47
47
@@ -127,8 +127,13 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
127
127
api_url , col_id = col_parts if len (col_parts ) == 2 else (None , col_parts [0 ])
128
128
col_id_alt = get_any_id (col_input , pop = True )
129
129
col_id = col_id or col_id_alt
130
+ timeout = col_args .pop ("timeout" , 10 )
130
131
131
- col_args .setdefault ("timeout" , 10 )
132
+ # convert all query parameters to their corresponding function parameter name
133
+ for arg in list (col_args ):
134
+ if "-" in arg :
135
+ col_args [arg .replace ("-" , "_" )] = col_args .pop (arg )
136
+ col_args = parse_collection_parameters (col_args )
132
137
133
138
logger .log ( # pylint: disable=E1205 # false positive
134
139
logging .INFO ,
@@ -144,7 +149,7 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
144
149
col_href ,
145
150
queries = col_args ,
146
151
headers = {"Accept" : f"{ ContentType .APP_GEOJSON } ,{ ContentType .APP_JSON } " },
147
- timeout = col_args [ " timeout" ] ,
152
+ timeout = timeout ,
148
153
retries = 3 ,
149
154
only_server_errors = False ,
150
155
)
@@ -161,16 +166,12 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
161
166
resolved_files .append (file_obj )
162
167
163
168
elif col_fmt in [ExecuteCollectionFormat .STAC , ExecuteCollectionFormat .STAC_ITEMS ]:
164
- # convert all parameters to their corresponding name of the query utility, and ignore unknown ones
165
- for arg in list (col_args ):
166
- if "-" in arg :
167
- col_args [arg .replace ("-" , "_" )] = col_args .pop (arg )
169
+ # ignore unknown or enforced parameters
168
170
known_params = set (inspect .signature (ItemSearch ).parameters )
169
171
known_params -= {"url" , "method" , "stac_io" , "client" , "collection" , "ids" , "modifier" }
170
172
for param in set (col_args ) - known_params :
171
173
col_args .pop (param )
172
174
173
- timeout = col_args .pop ("timeout" , 10 )
174
175
search_url = f"{ api_url } /search"
175
176
search = ItemSearch (
176
177
url = search_url ,
@@ -193,12 +194,18 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
193
194
resolved_files .append (file_obj )
194
195
195
196
elif col_fmt == ExecuteCollectionFormat .OGC_FEATURES :
196
- if str (col_args .get ("filter-lang " )) == "cql2-json" :
197
+ if str (col_args .get ("filter_lang " )) == "cql2-json" :
197
198
col_args ["cql" ] = col_args .pop ("filter" )
199
+ col_args .pop ("filter_lang" )
200
+ else :
201
+ for arg in list (col_args ):
202
+ if arg .startswith ("filter_" ):
203
+ col_args [arg .replace ("_" , "-" )] = col_args .pop (arg )
198
204
search = Features (
199
205
url = api_url ,
200
206
# FIXME: add 'auth' or 'headers' authorization/cookies?
201
207
headers = {"Accept" : f"{ ContentType .APP_GEOJSON } , { ContentType .APP_VDN_GEOJSON } , { ContentType .APP_JSON } " },
208
+ json_ = "{}" , # avoid unnecessary request on init
202
209
)
203
210
items = search .collection_items (col_id , ** col_args )
204
211
if items .get ("type" ) != "FeatureCollection" or "features" not in items :
@@ -228,13 +235,14 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
228
235
url = api_url ,
229
236
# FIXME: add 'auth' or 'headers' authorization/cookies?
230
237
headers = {"Accept" : ContentType .APP_JSON },
238
+ json_ = "{}" , # avoid unnecessary request on init
231
239
)
232
240
ctype = (col_media_type or [ContentType .IMAGE_COG ])[0 ]
233
241
ext = get_extension (ctype , dot = False )
234
- path = os .path .join (output_dir , f"map .{ ext } " )
242
+ path = os .path .join (output_dir , f"coverage .{ ext } " )
235
243
with open (path , mode = "wb" ) as file :
236
- data = cast (io .BytesIO , cov .coverage (col_id )).getbuffer ()
237
- file .write (data ) # type: ignore
244
+ data = cast (io .BytesIO , cov .coverage (col_id , ** col_args )).getbuffer ()
245
+ file .write (data )
238
246
_ , file_fmt = get_cwl_file_format (ctype )
239
247
file_obj = {"class" : PACKAGE_FILE_TYPE , "path" : f"file://{ path } " , "format" : file_fmt }
240
248
resolved_files .append (file_obj )
@@ -244,13 +252,14 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
244
252
url = api_url ,
245
253
# FIXME: add 'auth' or 'headers' authorization/cookies?
246
254
headers = {"Accept" : ContentType .APP_JSON },
255
+ json_ = "{}" , # avoid unnecessary request on init
247
256
)
248
257
ctype = (col_media_type or [ContentType .IMAGE_COG ])[0 ]
249
258
ext = get_extension (ctype [0 ], dot = False )
250
259
path = os .path .join (output_dir , f"map.{ ext } " )
251
260
with open (path , mode = "wb" ) as file :
252
- data = cast (io .BytesIO , maps .map (col_id )).getbuffer ()
253
- file .write (data ) # type: ignore
261
+ data = cast (io .BytesIO , maps .map (col_id , ** col_args )).getbuffer ()
262
+ file .write (data )
254
263
_ , file_fmt = get_cwl_file_format (ctype )
255
264
file_obj = {"class" : PACKAGE_FILE_TYPE , "path" : f"file://{ path } " , "format" : file_fmt }
256
265
resolved_files .append (file_obj )
@@ -271,6 +280,27 @@ def process_collection(collection_input, input_definition, output_dir, logger=LO
271
280
return resolved_files
272
281
273
282
283
+ def parse_collection_parameters (parameters ):
284
+ # type: (Dict[str, JSON]) -> Dict[str, JSON]
285
+ """
286
+ Applies any relevant conversions of known parameters between allowed request format and expected utilities.
287
+ """
288
+ if not parameters :
289
+ return {}
290
+
291
+ subset = parameters .get ("subset" )
292
+ if subset and isinstance (subset , str ):
293
+ subset_dims = {}
294
+ for item in subset .split ("," ):
295
+ dim , span = item .split ("(" , 1 )
296
+ span = span .split (")" , 1 )[0 ]
297
+ ranges = span .split (":" )
298
+ subset_dims [dim ] = list (ranges )
299
+ parameters ["subset" ] = subset_dims
300
+
301
+ return parameters
302
+
303
+
274
304
def process_cwl (collection_input , input_definition , output_dir ):
275
305
# type: (JobValueCollection, ProcessInputOutputItem, Path) -> CWL_IO_ValueMap
276
306
files = process_collection (collection_input , input_definition , output_dir )
0 commit comments