fix(nldi): return an empty GeoDataFrame instead of crashing on empty results#302
Draft
thodson-usgs wants to merge 1 commit into
Draft
fix(nldi): return an empty GeoDataFrame instead of crashing on empty results#302thodson-usgs wants to merge 1 commit into
thodson-usgs wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
NLDI can legitimately return no features (e.g. a feature with nothing upstream of it), and
_query_nldireturns{}when a 200 response carries no JSON body (its own comment notes this happens). The four getters then call:which raises an opaque
ValueError: Assigning CRS to a GeoDataFrame without a geometry column is not supportedon a perfectly valid empty result. (as_json=Truealready tolerated empties; only the default GeoDataFrame path crashed.)Fix
Add a shared
_features_to_gdfhelper used by all four getters (get_flowlines,get_basin,get_features,get_features_by_data_source) that returns an emptyGeoDataFrame(geometry=[], crs=_CRS)when there are no features, and otherwise behaves exactly as before.Verification
_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.get_flowlines(navigation_mode="UM", feature_source="nwissite", feature_id="USGS-01031500", distance=10)→ 7-row GeoDataFrame with geometry,EPSG:4326.ruffclean.🤖 Generated with Claude Code