Skip to content

Commit 06b0e69

Browse files
committed
incorporate geopandas boolean into function arguments and ensure user knows when they will receive a pandas df
1 parent bd82c49 commit 06b0e69

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

dataretrieval/waterdata_helpers.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
import re
1111
try:
1212
import geopandas as gpd
13-
gpd = True
13+
geopd = True
1414
except ImportError:
15-
warnings.warn("Geopandas is not installed. Data frames containing geometry will be returned as pandas DataFrames.", ImportWarning)
16-
gpd = False
15+
geopd = False
1716

1817

1918

@@ -384,7 +383,7 @@ def _next_req_url(resp: httpx.Response) -> Optional[str]:
384383
return next_url
385384
return None
386385

387-
def _get_resp_data(resp: httpx.Response) -> pd.DataFrame:
386+
def _get_resp_data(resp: httpx.Response, geopd: bool) -> pd.DataFrame:
388387
"""
389388
Extracts and normalizes data from an httpx.Response object containing GeoJSON features.
390389
@@ -402,7 +401,7 @@ def _get_resp_data(resp: httpx.Response) -> pd.DataFrame:
402401
return pd.DataFrame()
403402

404403
# If geopandas not installed, return a pandas dataframe
405-
if not gpd:
404+
if not geopd:
406405
df = pd.json_normalize(
407406
body["features"],
408407
sep="_")
@@ -422,7 +421,7 @@ def _get_resp_data(resp: httpx.Response) -> pd.DataFrame:
422421

423422
return df
424423

425-
def _walk_pages(req: httpx.Request, max_results: Optional[int], client: Optional[httpx.Client] = None) -> pd.DataFrame:
424+
def _walk_pages(geopd: bool, req: httpx.Request, max_results: Optional[int], client: Optional[httpx.Client] = None) -> pd.DataFrame:
426425
"""
427426
Iterates through paginated API responses and aggregates the results into a single DataFrame.
428427
@@ -452,6 +451,9 @@ def _walk_pages(req: httpx.Request, max_results: Optional[int], client: Optional
452451
"""
453452
print(f"Requesting:\n{req.url}")
454453

454+
if not geopd:
455+
print("Geopandas is not installed. Data frames containing geometry will be returned as pandas DataFrames.")
456+
455457
# Get first response from client
456458
# using GET or POST call
457459
client = client or httpx.Client()
@@ -465,14 +467,14 @@ def _walk_pages(req: httpx.Request, max_results: Optional[int], client: Optional
465467
content = req.content if method == "POST" else None
466468

467469
if max_results is None or pd.isna(max_results):
468-
dfs = _get_resp_data(resp)
470+
dfs = _get_resp_data(resp, geopd=geopd)
469471
curr_url = _next_req_url(resp)
470472
failures = []
471473
while curr_url:
472474
try:
473475
resp = client.request(method, curr_url, headers=headers, content=content if method == "POST" else None)
474476
if resp.status_code != 200: raise Exception(_error_body(resp))
475-
df1 = _get_resp_data(resp)
477+
df1 = _get_resp_data(resp, geopd=geopd)
476478
dfs = pd.concat([dfs, df1], ignore_index=True)
477479
curr_url = _next_req_url(resp)
478480
except Exception:
@@ -483,7 +485,7 @@ def _walk_pages(req: httpx.Request, max_results: Optional[int], client: Optional
483485
return dfs
484486
else:
485487
resp.raise_for_status()
486-
return _get_resp_data(resp)
488+
return _get_resp_data(resp, geopd=geopd)
487489

488490
def _deal_with_empty(return_list: pd.DataFrame, properties: Optional[List[str]], service: str) -> pd.DataFrame:
489491
"""
@@ -604,7 +606,7 @@ def get_ogc_data(args: Dict[str, Any], output_id: str, service: str) -> pd.DataF
604606
# Build API request
605607
req = _construct_api_requests(**args)
606608
# Run API request and iterate through pages if needed
607-
return_list = _walk_pages(req, max_results)
609+
return_list = _walk_pages(geopd=geopd, req=req, max_results=max_results)
608610
# Manage some aspects of the returned dataset
609611
return_list = _deal_with_empty(return_list, properties, service)
610612
if convertType:

0 commit comments

Comments
 (0)