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 - support to instantiate a raster directly from an Xarray object #519

Closed
geograFie opened this issue Mar 17, 2024 · 2 comments · Fixed by #521
Closed

Raster - support to instantiate a raster directly from an Xarray object #519

geograFie opened this issue Mar 17, 2024 · 2 comments · Fixed by #521
Labels
enhancement Feature improvement or request priority

Comments

@geograFie
Copy link

Awesome utility! I am using it in combination with xDEM.

However, it would be great if there was support for xr.DataArray object type in Raster.from_array(). I want to be able to directly convert from Xarray to Raster without having to store and read from a file or performing other conversions.

Currently, I solved this by reading the data and Affine transformation using rio from my xr.DataArray:

dem = xr.DataArray(...)
crs = CRS.from_wkt(dem['spatial_ref'].attrs['crs_wkt'])
epsg = crs.to_epsg()
affine = dem.rio.transform()

raster = gu.Raster.from_array(
    data=dem.data,
    transform=affine,
    crs=epsg
)

x_dem = xdem.DEM(raster)

It would be nice to have something like this instead:

dem = xr.DataArray(...)

raster = gu.Raster.from_array(dem)
OR  
raster = gu.Raster.from_xarray(dem)

x_dem = xdem.DEM(raster)
@geograFie geograFie added the enhancement Feature improvement or request label Mar 17, 2024
@rhugonnet
Copy link
Contributor

Thanks for opening this @geograFie!

You're right, we need to add a from_xarray() function, it's not the first time we get that request! I'll move this to a higher priority so that it is available in the next release 🙂.

We are also writing an Xarray accessor mirroring the Raster/DEM APIs, so soon you'll be able to call any GeoUtils/xDEM function from your Xarray object directly!
For instance:

ds = xr.DataArray(...)

prox = ds.gu.proximity()
curv = ds.dem.curvature()

Hopefully we'll be done by the end of summer. The accessor names will likely be gu and dem! See #383 and GlacioHack/xdem#392.

@geograFie
Copy link
Author

Perfect!

Thanks for the fast response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature improvement or request priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants