Skip to content

Commit

Permalink
Merge pull request #214 from philippkraft/i213_to_raster_kwargs
Browse files Browse the repository at this point in the history
I213 to raster kwargs
  • Loading branch information
mdbartos authored Mar 16, 2023
2 parents 76eebd1 + e273780 commit 3085126
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pysheds/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def to_raster(data, file_name, target_view=None, profile=None, blockxsize=256,
Value indicating no data in output Raster (overrides target_view.nodata)
dtype : numpy datatype
Desired datatype of the output array.
kwargs : Keyword arguments passed to rasterio.open together with the profile, eg. compress='lzw'
"""
if target_view is None:
target_view = data.viewfinder
Expand Down Expand Up @@ -299,6 +300,7 @@ def to_raster(data, file_name, target_view=None, profile=None, blockxsize=256,
'width' : width
}
profile.update(profile_updates)
profile.update(kwargs)
with rasterio.open(file_name, 'w', **profile) as dst:
dst.write(np.asarray(data), 1)

12 changes: 12 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,18 @@ def test_to_raster():
fdir_out = grid.read_raster('test_dir.tif')
assert((fdir_out == grid.view(fdir)).all())

def test_to_raster_kwargs():
"""
Test if kwargs of the "to_raster" method are passed to rasterio
"""
import rasterio as rio
catch = d.catch
grid.clip_to(catch)
grid.to_raster(fdir, 'test_dir.tif', target_view=fdir.viewfinder,
blockxsize=16, blockysize=16, compress='LZW')
with rio.open('test_dir.tif') as ds:
assert ds.profile['compress'] == 'lzw'

# def test_from_raster():
# grid.clip_to('catch')
# grid.to_raster('dir', 'test_dir.tif', view=False, apply_mask=False, blockxsize=16, blockysize=16)
Expand Down

0 comments on commit 3085126

Please sign in to comment.