Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(geopandas support): return GeoDataFrame if geopandas is installed #143

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dataretrieval/nldi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

NLDI_API_BASE_URL = 'https://labs.waterdata.usgs.gov/api/nldi/linked-data'
_AVAILABLE_DATA_SOURCES = None
_CRS = "EPSG:4326"


def _query_nldi(url, query_params, error_message):
Expand Down Expand Up @@ -101,7 +102,7 @@ def get_flowlines(
feature_collection = _query_nldi(url, query_params, err_msg)
if as_json:
return feature_collection
gdf = gpd.GeoDataFrame.from_features(feature_collection)
gdf = gpd.GeoDataFrame.from_features(feature_collection, crs=_CRS)
return gdf


Expand Down Expand Up @@ -154,7 +155,7 @@ def get_basin(
feature_collection = _query_nldi(url, query_params, err_msg)
if as_json:
return feature_collection
gdf = gpd.GeoDataFrame.from_features(feature_collection)
gdf = gpd.GeoDataFrame.from_features(feature_collection, crs=_CRS)
return gdf


Expand Down Expand Up @@ -291,7 +292,7 @@ def get_features(
feature_collection = _query_nldi(url, query_params, err_msg)
if as_json:
return feature_collection
gdf = gpd.GeoDataFrame.from_features(feature_collection)
gdf = gpd.GeoDataFrame.from_features(feature_collection, crs=_CRS)
return gdf


Expand Down Expand Up @@ -322,7 +323,7 @@ def get_features_by_data_source(data_source: str) -> gpd.GeoDataFrame:
url = f'{NLDI_API_BASE_URL}/{data_source}'
err_msg = f"Error getting features for data source '{data_source}'"
feature_collection = _query_nldi(url, {}, err_msg)
gdf = gpd.GeoDataFrame.from_features(feature_collection)
gdf = gpd.GeoDataFrame.from_features(feature_collection, crs=_CRS)
return gdf


Expand Down
14 changes: 14 additions & 0 deletions dataretrieval/nwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

from .utils import query

try:
import geopandas as gpd
except ImportError:
gpd = None

WATERDATA_BASE_URL = 'https://nwis.waterdata.usgs.gov/'
WATERDATA_URL = WATERDATA_BASE_URL + 'nwis/'
WATERSERVICE_URL = 'https://waterservices.usgs.gov/nwis/'
Expand All @@ -38,6 +43,7 @@
'water_use',
'ratings',
]
_CRS = "EPSG:4236"


def format_response(
Expand Down Expand Up @@ -71,6 +77,14 @@ def format_response(
if service == 'peaks':
df = preformat_peaks_response(df)

if gpd is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this remove the original lat/lon column?
If so (and perhaps either way), we should make this conversion optional, so that we don't break existing codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This maintains the original lat/long columns. This PR just builds a geometry field from that data and creates a geodataframe. I can add a unit test that checks the return type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated tests for geopandas support

if "dec_lat_va" in list(df):
geoms = gpd.points_from_xy(
df.dec_long_va.values,
df.dec_lat_va.values
)
df = gpd.GeoDataFrame(df, geometry=geoms, crs=_CRS)

# check for multiple sites:
if 'datetime' not in df.columns:
# XXX: consider making site_no index
Expand Down
Loading