Skip to content

hydro: missing _validate_raster on secondary DataArray args #1425

@brendancol

Description

@brendancol

Description

Several xrspatial.hydro public functions accept a primary raster (validated correctly) plus a secondary xarray.DataArray argument that is never run through _validate_raster. Passing a non-DataArray, a 1-D DataArray, or a None for the secondary arg raises a confusing AttributeError from inside the implementation rather than a clean ValueError from the public API.

Affected:

Function Validated Unvalidated arg
watershed_d8 / watershed_dinf / watershed_mfd flow-direction pour_points
snap_pour_point_d8 flow_accum pour_points
flow_path_d8 / flow_path_dinf / flow_path_mfd flow-direction start_points
stream_link_d8 / stream_link_dinf / stream_link_mfd flow-direction flow_accum
stream_order_d8 / stream_order_dinf / stream_order_mfd flow-direction flow_accum

Examples that should fail cleanly today but do not:

  • watershed_d8(fd, pour_points=None) -> AttributeError: 'NoneType' object has no attribute 'data'
  • flow_path_d8(fd, start_points=np.zeros(5)) -> AttributeError
  • stream_link_d8(fd, flow_accum=xr.DataArray(np.zeros(5))) -> IndexError from dims[-2]

Expected behavior

Each function calls _validate_raster on the secondary DataArray argument up front, mirroring how the primary raster is already validated. Bad input raises ValueError / TypeError with a clear message naming the offending parameter.

Proposed fix

Add _validate_raster(<arg>, func_name=..., name=<arg-name>) immediately after the existing _validate_raster call in each function listed above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions