CNES / rastertools

Python and CLI tools for radiometric indices and zonal statistics computation on raster imagery.
Apache License 2.0
1 stars 2 forks source link

Xarray interface #12

Open guillaumeeb opened 1 week ago

guillaumeeb commented 1 week ago

The goal would be to replace the current approach using multiprocessing and internal mechanisms by an Xarray interface. This would enable the possibility to use Dask, stream the processing, but also keep results in memory.

queyruto commented 1 week ago

I began this work a while ago. Be careful: it won't be possible to use xarray to compute zonalstats. You will then have to allow two ways to open the product: as a classical numpy array using rasterio, or as an xarray using rioxarray.

In rasterproc.py, just add something like:

    def open_xarray(self,
                    bands: Union[str, List[str]] = "all",
                    masks: Union[str, List[str]] = "all",
                    roi: Union[Path, str] = None,
                    chunks: Union[int, Dict[str, int], Tuple[int]] = None):
        """Proxy method to xarray.open_rasterio(rasterproduct.get_raster(...))"""
        raster = self.get_raster(bands=bands, masks=masks, roi=roi, create_maskband=True)
        xa = rioxarray.open_rasterio(raster, masked=True, chunks=chunks)
        ds = xa.to_dataset(dim="band")
        ds = ds.rename({b + 1: self.rastertype.get_band_id(self.channels[b])
                        for b in range(len(self.channels))})
        return ds