Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to exclude NoData values when calculating the flow direction of DEM? #256

Open
mht2953658596 opened this issue Jul 15, 2024 · 9 comments

Comments

@mht2953658596
Copy link

When using PyShed to calculate the flow direction of DEM, I found that the flow direction was also calculated for areas where DEM values were NoData. How can I avoid this situation?

When I set NoData using the following code:

nodata = gdal.Open(raster_path)tGetRasterBand(1).ReadAsArray().GetNoDataValue()
grid = Grid.from_raster(raster_path, nodata=nodata)
dem = grid.read_raster(self.raster_path, band=1, nodata=grid.nodata)
flooded_dem = grid.fill_depressions(dem)
inflated_dem = grid.resolve_flats(flooded_dem)
fdir = grid.flowdir(inflated_dem)

it reports the following error:

image

@mht2953658596
Copy link
Author

If I don't set the NoData value, it will report the same error.

@mht2953658596
Copy link
Author

@cheginit

@phillipaarestrup
Copy link

I have found the same issue and also when loading some rasters. I think some of this is related to a new update in the Numpy package from 1.2x -> 2.0.0 (released in june). I only have these issues with the new version of Numpy and not when I downgrade it to 1.2x.

Your screenshow shows that you get a typeerror in sview.py which is a result of this function call np.can_cast(viewfinder.nodata, obj.dtype, casting='safe') in line 85

See also the discussion here on fx the function can_cast, which is used for checks in the pysheds package: numpy/numpy#24856

@mht2953658596
Copy link
Author

@phillipaarestrup But my NumPy version is 1.24.3, and I am still encountering this issue.

@JaweedNazary
Copy link

I tried NumPy 2.0 and the nodata representation error pops up when running flodir. However, with NumPy 1.26, it is working pretty good,

@JaweedNazary
Copy link

@mht2953658596 try converting your raster to float before using the code.

JaweedNazary added a commit to JaweedNazary/pysheds that referenced this issue Jul 26, 2024
Changes the TypeError message while casting from viewfinder.nodata to obj data type. This help with issue mdbartos#256 How to exclude NoData values when calculating the flow direction of DEM.
@col16
Copy link

col16 commented Sep 18, 2024

Here's a workaround: you can pass the parameter nodata_out=np.int64(0):

fdir = grid.flowdir(inflated_dem, nodata_out=np.int64(0))

The same issue affects the accumulation function, and the workaround is the same:
acc = grid.accumulation(fdir, nodata_out=np.int64(0))

@hydro-coop
Copy link

@col16 thank you for providing a workaround. I used it for both the flow direction and accumulation functions and was finally able to avoid the 'nodata' TypeError.

Continuing with the workflow, I still received the error message when trying to run the snap_to_mask function. Is there a workaround for this function or for the catchment function (unsure yet if that will raise the same issue). I tried the same as above but was unsuccessful.

Thank you in advance.

@hydro-coop
Copy link

Update: I found that downgrading to Numpy 1.26 allowed me to complete the delineation without using any provided workarounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants