Skip to content

Commit 3a26dde

Browse files
committed
Merge remote-tracking branch 'upstream/main' into fix/wqp-metadata-site-info
2 parents 0c0de2a + dd70cfa commit 3a26dde

7 files changed

Lines changed: 343 additions & 158 deletions

File tree

dataretrieval/nldi.py

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
NLDI_API_BASE_URL = "https://api.water.usgs.gov/nldi/linked-data"
1414
_AVAILABLE_DATA_SOURCES = None
1515
_CRS = "EPSG:4326"
16+
_VALID_NAVIGATION_MODES = ("UM", "DM", "UT", "DD")
1617

1718

1819
def _query_nldi(url, query_params, error_message):
@@ -79,19 +80,14 @@ def get_flowlines(
7980
... comid=13294314, navigation_mode="UM"
8081
... )
8182
"""
82-
# validate the navigation mode
83-
_validate_navigation_mode(navigation_mode)
84-
# validate the feature source and comid
83+
navigation_mode = _validate_navigation_mode(navigation_mode)
8584
_validate_feature_source_comid(feature_source, feature_id, comid)
8685
if feature_source:
87-
# validate the feature source
8886
_validate_data_source(feature_source)
89-
9087
url = f"{NLDI_API_BASE_URL}/{feature_source}/{feature_id}/navigation"
91-
query_params = {"distance": str(distance), "trimStart": str(trim_start).lower()}
9288
else:
9389
url = f"{NLDI_API_BASE_URL}/comid/{comid}/navigation"
94-
query_params = {"distance": str(distance)}
90+
query_params = {"distance": str(distance), "trimStart": str(trim_start).lower()}
9591

9692
url += f"/{navigation_mode}/flowlines"
9793
if stop_comid is not None:
@@ -232,43 +228,35 @@ def get_features(
232228
>>> gdf = dataretrieval.nldi.get_features(lat=43.073051, long=-89.401230)
233229
"""
234230

235-
# check only one origin is provided
236-
if (lat and long is None) or (long and lat is None):
231+
if (lat is None) != (long is None):
237232
raise ValueError("Both lat and long are required")
238233

239-
if lat:
240-
if comid:
234+
if lat is not None:
235+
if comid is not None:
241236
raise ValueError(
242237
"Provide only one origin type - comid cannot be provided"
243238
" with lat or long"
244239
)
245-
if feature_source or feature_id:
240+
if feature_source is not None or feature_id is not None:
246241
raise ValueError(
247242
"Provide only one origin type - feature_source and feature_id cannot"
248243
" be provided with lat or long"
249244
)
250-
251-
if not lat:
252-
if (comid or data_source) and navigation_mode is None:
245+
url = f"{NLDI_API_BASE_URL}/comid/position"
246+
query_params = {"coords": f"POINT({long} {lat})"}
247+
err_msg = f"Error getting features for lat '{lat}' and long '{long}'"
248+
else:
249+
if (comid is not None or data_source is not None) and navigation_mode is None:
253250
raise ValueError(
254251
"navigation_mode is required if comid or data_source is provided"
255252
)
256-
# validate the feature source and comid
257253
_validate_feature_source_comid(feature_source, feature_id, comid)
258-
# validate the data source
259-
if data_source:
254+
if data_source is not None:
260255
_validate_data_source(data_source)
261-
# validate feature source
262-
_validate_data_source(feature_source)
263-
# validate the navigation mode
264-
if navigation_mode:
265-
_validate_navigation_mode(navigation_mode)
266-
267-
if lat:
268-
url = f"{NLDI_API_BASE_URL}/comid/position"
269-
query_params = {"coords": f"POINT({long} {lat})"}
270-
else:
256+
if feature_source is not None:
257+
_validate_data_source(feature_source)
271258
if navigation_mode:
259+
navigation_mode = _validate_navigation_mode(navigation_mode)
272260
if feature_source:
273261
url = f"{NLDI_API_BASE_URL}/{feature_source}/{feature_id}/navigation"
274262
else:
@@ -280,19 +268,7 @@ def get_features(
280268
else:
281269
url = f"{NLDI_API_BASE_URL}/{feature_source}/{feature_id}"
282270
query_params = {}
283-
284-
if lat:
285-
err_msg = f"Error getting features for lat '{lat}' and long '{long}'"
286-
elif feature_source:
287-
err_msg = (
288-
f"Error getting features for feature source '{feature_source}'"
289-
f" and feature_id '{feature_id}, and data source '{data_source}'"
290-
)
291-
else:
292-
err_msg = (
293-
f"Error getting features for comid '{comid}'"
294-
f" and data source '{data_source}'"
295-
)
271+
err_msg = _features_err_msg(feature_source, feature_id, comid, data_source)
296272

297273
feature_collection = _query_nldi(url, query_params, err_msg)
298274
if as_json:
@@ -413,28 +389,26 @@ def search(
413389
... )
414390
415391
"""
416-
if (lat and long is None) or (long and lat is None):
392+
if (lat is None) != (long is None):
417393
raise ValueError("Both lat and long are required")
418394

419-
# validate find
420395
find = find.lower()
421396
if find not in ("basin", "flowlines", "features"):
422397
raise ValueError(
423398
f"Invalid value for find: {find} - allowed values are:"
424399
f" 'basin', 'flowlines', or 'features'"
425400
)
426-
if lat and find != "features":
401+
if lat is not None and find != "features":
427402
raise ValueError(
428403
f"Invalid value for find: {find} - lat/long is to get features not {find}"
429404
)
430-
if comid and find == "basin":
405+
if comid is not None and find == "basin":
431406
raise ValueError(
432407
"Invalid value for find: basin - comid is to get features"
433408
" or flowlines not basin"
434409
)
435410

436-
if lat:
437-
# get features by hydrologic location
411+
if lat is not None:
438412
return get_features(lat=lat, long=long, as_json=True)
439413

440414
if find == "basin":
@@ -443,6 +417,11 @@ def search(
443417
)
444418

445419
if find == "flowlines":
420+
if navigation_mode is None:
421+
raise ValueError(
422+
"navigation_mode is required for find='flowlines';"
423+
f" allowed values are {_VALID_NAVIGATION_MODES}"
424+
)
446425
return get_flowlines(
447426
navigation_mode=navigation_mode,
448427
distance=distance,
@@ -475,18 +454,36 @@ def _validate_data_source(data_source: str):
475454
url, {}, "Error getting available data sources"
476455
)
477456
_AVAILABLE_DATA_SOURCES = [ds["source"] for ds in available_data_sources]
478-
if data_source not in _AVAILABLE_DATA_SOURCES:
479-
err_msg = (
480-
f"Invalid data source '{data_source}'."
481-
f" Available data sources are: {_AVAILABLE_DATA_SOURCES}"
482-
)
483-
raise ValueError(err_msg)
457+
458+
if data_source not in _AVAILABLE_DATA_SOURCES:
459+
err_msg = (
460+
f"Invalid data source '{data_source}'."
461+
f" Available data sources are: {_AVAILABLE_DATA_SOURCES}"
462+
)
463+
raise ValueError(err_msg)
484464

485465

486-
def _validate_navigation_mode(navigation_mode: str):
487-
navigation_mode = navigation_mode.upper()
488-
if navigation_mode not in ("UM", "DM", "UT", "DD"):
489-
raise ValueError(f"Invalid navigation mode '{navigation_mode}'")
466+
def _features_err_msg(feature_source, feature_id, comid, data_source) -> str:
467+
if feature_source is not None:
468+
return (
469+
f"Error getting features for feature source '{feature_source}'"
470+
f" and feature_id '{feature_id}', and data source '{data_source}'"
471+
)
472+
return f"Error getting features for comid '{comid}' and data source '{data_source}'"
473+
474+
475+
def _validate_navigation_mode(navigation_mode: str | None) -> str:
476+
if navigation_mode is None:
477+
raise ValueError(
478+
f"navigation_mode is required; allowed values are {_VALID_NAVIGATION_MODES}"
479+
)
480+
normalized = navigation_mode.upper()
481+
if normalized not in _VALID_NAVIGATION_MODES:
482+
raise ValueError(
483+
f"Invalid navigation mode '{navigation_mode}';"
484+
f" allowed values are {_VALID_NAVIGATION_MODES}"
485+
)
486+
return normalized
490487

491488

492489
def _validate_feature_source_comid(

dataretrieval/waterdata/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,9 @@ def get_reference_table(
15421542
else:
15431543
output_id = f"{collection.replace('-', '_')}"
15441544

1545-
query_args = query or {}
1545+
query_args = dict(query) if query else {}
1546+
if limit is not None:
1547+
query_args["limit"] = limit
15461548
return get_ogc_data(args=query_args, output_id=output_id, service=collection)
15471549

15481550

0 commit comments

Comments
 (0)