Skip to content

Add comprehensive input validation and error messages across public API #903

@brendancol

Description

@brendancol

Problem

Input validation is inconsistent across the library. Some functions do thorough checking (e.g., zonal.crosstab validates types, dimensions, and shapes), while many others do no validation at all and produce cryptic errors.

Examples

  • slope(), aspect(), hillshade(), curvature(): No validation that input is 2D, no check for numeric dtype. Passing a string array or 3D array produces a confusing numba TypingError deep in JIT-compiled code.
  • proximity(): Validates dims match but doesn't check that coordinates are monotonically increasing (required for correct distance calculation).
  • cost_distance(): No validation that friction surface has matching shape/chunks.
  • multispectral.*: Validates array shapes match but not that values are in expected ranges.

The diagnostics.py module exists but is not integrated into any function.

Proposed Fix

  1. Add _validate_raster(agg, ndim=2, name='raster') utility in xrspatial/utils.py that checks:

    • Is a DataArray
    • Correct ndim
    • Numeric dtype
    • Has coordinates
    • Coordinates are monotonic (where required)
  2. Call at the top of each public function with function-specific constraints.

  3. Ensure all error messages include: function name, parameter name, expected type/value, actual type/value.

Scope

This is a mechanical change across many files, but each individual change is small. Could be split into per-module PRs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiAPI design and consistencyenhancementNew feature or requestinput-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