mdbartos / pysheds

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

keyword arguments of to_raster are not propagated to rasterio #213

Closed philippkraft closed 1 year ago

philippkraft commented 1 year ago

The call signature of pysheds.io.to_raster includes a **kwargs argument, which is unused.

https://github.com/mdbartos/pysheds/blob/9d960962bfe742c534da6577c3353169e6b6a501/pysheds/io.py#L230-L234

It could easily be used to extent the rasterio profile with additional arguments, eg. compress. It is currently rather cumbersome to save a raster with a compression. @mdbartos, your code is so fast, I have mainly I/O as a bottlenck and compression would be super helpful (I have a workaround). I can create a pull request if wanted, but it would just extend the code creating profile_updates with the kwargs:

  profile_updates = {
        'crs' : data.crs.srs,
        'transform' : data.affine,
        'dtype' : data.dtype.name,
        'nodata' : data.nodata,
        'height' : height,
        'width' : width
    } | kwargs 

Original code:

https://github.com/mdbartos/pysheds/blob/9d960962bfe742c534da6577c3353169e6b6a501/pysheds/io.py#L293-L300

mdbartos commented 1 year ago

Hi @philippkraft, sounds useful. I haven't worked with rasterio in a while, so I am not totally sure I understand the proposed change, but go ahead and create a PR with a proof of concept.

philippkraft commented 1 year ago

The idea is just the same, as in to_ascii. Additional keyword arguments are passed on to rasterio.open (instead to numpy.savetxt as in to_ascii). However, while implementing this, I've seen that the pgrid.to_raster code is duplicating the io.to_raster' code, whilesgrid.to_rastercallsio.to_raster. Shouldpgrid.py` changed in the same way?

https://github.com/mdbartos/pysheds/blob/9d960962bfe742c534da6577c3353169e6b6a501/pysheds/io.py#L275-L303

https://github.com/mdbartos/pysheds/blob/9d960962bfe742c534da6577c3353169e6b6a501/pysheds/pgrid.py#L2595-L2622

https://github.com/mdbartos/pysheds/blob/9d960962bfe742c534da6577c3353169e6b6a501/pysheds/sgrid.py#L366-L377