Skip to content

fix(nldi): return an empty GeoDataFrame instead of crashing on empty results#302

Draft
thodson-usgs wants to merge 1 commit into
DOI-USGS:mainfrom
thodson-usgs:fix/nldi-empty-feature-collection
Draft

fix(nldi): return an empty GeoDataFrame instead of crashing on empty results#302
thodson-usgs wants to merge 1 commit into
DOI-USGS:mainfrom
thodson-usgs:fix/nldi-empty-feature-collection

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

Problem

NLDI can legitimately return no features (e.g. a feature with nothing upstream of it), and _query_nldi returns {} when a 200 response carries no JSON body (its own comment notes this happens). The four getters then call:

gdf = gpd.GeoDataFrame.from_features(feature_collection, crs=_CRS)

which raises an opaque ValueError: Assigning CRS to a GeoDataFrame without a geometry column is not supported on a perfectly valid empty result. (as_json=True already tolerated empties; only the default GeoDataFrame path crashed.)

Fix

Add a shared _features_to_gdf helper used by all four getters (get_flowlines, get_basin, get_features, get_features_by_data_source) that returns an empty GeoDataFrame(geometry=[], crs=_CRS) when there are no features, and otherwise behaves exactly as before.

Verification

  • Local (both crashing inputs confirmed pre-fix): _features_to_gdf({}) and _features_to_gdf({"features": []}) → empty GeoDataFrame (0 rows, EPSG:4326), no crash; a populated FeatureCollection → 1-row gdf with a geometry column.
  • Live API (happy path unbroken): get_flowlines(navigation_mode="UM", feature_source="nwissite", feature_id="USGS-01031500", distance=10) → 7-row GeoDataFrame with geometry, EPSG:4326.
  • ruff clean.

🤖 Generated with Claude Code

…results

NLDI can legitimately return no features (e.g. a feature with nothing upstream),
and _query_nldi returns {} when a 200 response carries no JSON body (per its own
comment). The getters then called gpd.GeoDataFrame.from_features(..., crs=_CRS),
which raises "Assigning CRS to a GeoDataFrame without a geometry column is not
supported" — an opaque crash on a valid empty result. (as_json=True already
tolerated empties; the default GeoDataFrame path did not.)

Added a shared _features_to_gdf helper, used by all four getters, that returns
an empty GeoDataFrame with the correct CRS when there are no features.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant