mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
702 stars 188 forks source link

Indexing Error in _d8_distance_from_ridge #255

Open JonKing93 opened 1 month ago

JonKing93 commented 1 month ago

The "distance_to_ridge" function produces an off-by-one indexing error for D8 flow routing. For example, the following code triggers the error:

from pysheds.grid import Grid
from math import nan

grid = Grid.from_raster("fdir.tif")
fdir = grid.read_raster("fdir.tif")
weights = grid.read_raster("weights.tif")
assert fdir.shape == weights.shape

# Causes an off-by-one indexing error
grid.distance_to_ridge(
    fdir, weights, nodata_out=nan, routing="d8", dirmap=(3, 2, 1, 8, 7, 6, 5, 4)
)

With error stack:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "path\to\issue.py", line 10, in <module>
    grid.distance_to_ridge(
  File "path\to\Lib\site-packages\pysheds\sgrid.py", line 1649, in distance_to_ridge
    rdist = self._d8_distance_to_ridge(fdir=fdir, weights=weights,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "path\to\Lib\site-packages\pysheds\sgrid.py", line 1676, in _d8_distance_to_ridge
    startnodes = startnodes[(indegree == 0)]
                 ~~~~~~~~~~^^^^^^^^^^^^^^^^^
IndexError: boolean index did not match indexed array along dimension 0; dimension is 6205794 but corresponding boolean dimension is 6205763