Skip to content

Planar slope returns a valid value for a NoData (NaN) center cell #2761

@brendancol

Description

@brendancol

Describe the bug

Planar slope can return a finite slope for an input NoData (NaN) cell. The _cpu() kernel in xrspatial/slope.py reads the 8 surrounding neighbours but never looks at the center cell data[y, x]. So when the center is NaN but its 8 neighbours are valid, the slope formula returns a real number instead of NaN. The GPU planar path (_gpu / _run_gpu) has the same gap.

This bites downstream hydrology: a hole in a DEM silently turns into "valid flat terrain", which throws off flow routing and fill.

The geodesic method doesn't have this problem because it includes the center cell in its 9-cell plane fit, so a NaN center comes back as NaN.

Steps to reproduce

import numpy as np, xarray as xr
from xrspatial import slope

data = np.zeros((5, 5), dtype=np.float64)
data[2, 2] = np.nan          # single hole in an otherwise flat raster
agg = xr.DataArray(data, attrs={'res': (1, 1)})
result = slope(agg)
print(result.data[2, 2])     # prints 0.0, expected nan

Expected behavior

A NaN center cell should yield NaN at that cell, matching the geodesic method and normal NoData semantics.

Additional context

The fix needs to cover both the planar CPU path (_cpu / @ngjit) and the planar GPU path (cupy/cuda), pinned with regression tests across the supported backends.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggpuCuPy / CUDA GPU support

    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