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

Raster.to_xarray() not masking NaN nodata values #522

Closed
geograFie opened this issue Mar 18, 2024 · 1 comment · Fixed by #521
Closed

Raster.to_xarray() not masking NaN nodata values #522

geograFie opened this issue Mar 18, 2024 · 1 comment · Fixed by #521
Labels
bug Something isn't working

Comments

@geograFie
Copy link

It seems like NaNs are not masked when using Raster.to_xarray(). The Raster object contains the nodata attribute which is set via Raster.to_rio_dataset(), so not sure what the issue is and if that if that is a common issue, or only happens in my case.

I solved this by manually masking NaN values slope_xr.where(slope_xr != slope_xr.rio.nodata).

%matplotlib inline
import pdemtools
import xdem
from geoutils import Raster
from pyproj import CRS
import matplotlib.pyplot as plt

# download some ArcticDEM data 
bounds = (-2789094.0, 708249.0, -2789044.0, 708299.0)
dem = pdemtools.load.mosaic(
    dataset='arcticdem',
    resolution=2,
    bounds=bounds,
    version='v4.1'
)

# Xarray to Raster
crs = CRS.from_wkt(dem['spatial_ref'].attrs['crs_wkt'])
epsg = crs.to_epsg()
affine = dem.rio.transform()

raster = Raster.from_array(
    data=dem.data,
    crs=epsg,
    transform=affine,
    nodata=-9999.           # set nodata value
)

# Raster to xDEM 
x_dem = xdem.DEM(raster)
print(type(x_dem))
print(x_dem.nodata)

# Compute terrain parameter
slope = xdem.terrain.slope(x_dem)

# Convert back to Xarray
slope_xr = slope.to_xarray()
print(slope_xr.data[0,:3,:3])

This code outputs:
geoutils_to_xarray

@geograFie geograFie added the bug Something isn't working label Mar 18, 2024
@rhugonnet
Copy link
Member

Thanks @geograFie, I also noticed this behaviour yesterday when adding the from_xarray() function to solve #519. Both are now fixed in #521 with more tests, awaiting peer-review before merging and making a new release. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants