Skip to content

hydro: scalar parameters not validated against NaN/Inf and bad ranges #1427

@brendancol

Description

@brendancol

Description

Several xrspatial.hydro public functions accept scalar parameters that are not range-checked. Non-finite values (NaN, Inf) or out-of-domain values pass the existing checks and silently produce wrong output (all-NaN rasters, no-op behavior, or undefined comparisons).

Function Parameter Current check Bad input that slips through
flow_direction_mfd p (exponent) p <= 0 p=NaN (NaN <= 0 is False), p=Inf
snap_pour_point_d8 search_radius none 0, negative, 5.5, NaN
hand_d8 / hand_dinf / hand_mfd threshold none NaN, Inf (silent all-NaN output)
fill_d8 z_limit none NaN (revert never fires), negative (reverts everything)

Examples:

  • flow_direction_mfd(agg, p=float('nan')) -> all weights become NaN, output is silently all-NaN.
  • snap_pour_point_d8(fa, pp, search_radius=0) -> no-op, output equals input.
  • hand_d8(fd, fa, el, threshold=float('nan')) -> fa >= NaN always False, returns NaN everywhere.
  • fill_d8(dem, z_limit=float('nan')) -> out - dem > NaN always False, behaves as if z_limit=None.

Expected behavior

Each parameter is validated up front. Non-finite values and out-of-range values raise a clean ValueError with a message that names the parameter.

Proposed fix

  • flow_direction_mfd: replace if p <= 0 with if not (np.isfinite(p) and p > 0).
  • snap_pour_point_d8: enforce isinstance(search_radius, (int, np.integer)) and search_radius >= 1.
  • hand_*: enforce np.isfinite(threshold) (semantic range allows 0).
  • fill_d8: when z_limit is not None, enforce np.isfinite(z_limit) and z_limit >= 0.

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