Skip to content

polygonize: reject non-finite simplify_tolerance (nan, inf) #2575

@brendancol

Description

@brendancol

Describe the bug
polygonize's simplify_tolerance only rejects negative values. Non-finite values slip through:

  • nan: silently disables simplification. The gating check is simplify_tolerance > 0, which is False for nan, so the call returns unsimplified polygons even though the caller asked for simplification.
  • inf (positive): collapses every polygon to nothing and returns empty output.
  • -inf: caught by the existing < 0 check, but only by accident of the sign.

The same module already validates atol and rtol as finite + non-negative. simplify_tolerance should follow the same pattern.

Expected behavior
Passing simplify_tolerance=nan, simplify_tolerance=inf, or simplify_tolerance=-inf should raise ValueError with a clear message, matching the existing atol/rtol validation.

Reproducer

import numpy as np
import xarray as xr
from xrspatial import polygonize

raster = xr.DataArray(np.array([[0, 1], [1, 0]], dtype=np.float32))
polygonize(raster, simplify_tolerance=float('nan'))  # silently no-op, should raise
polygonize(raster, simplify_tolerance=float('inf'))  # empty output, should raise

Fix
Mirror the atol/rtol validation block at xrspatial/polygonize.py:2013 and apply it to simplify_tolerance at xrspatial/polygonize.py:1999.

Additional context
Surfaced during an input-validation audit of polygonize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions