From 84fcfe2e1cd23ea32f1de242277af97b04b24192 Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Fri, 10 Feb 2023 10:12:02 +0100 Subject: [PATCH 1/5] #213 - adding kwargs to rasterio call --- pysheds/io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pysheds/io.py b/pysheds/io.py index b91b96a..bf866c0 100644 --- a/pysheds/io.py +++ b/pysheds/io.py @@ -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 @@ -298,7 +299,7 @@ def to_raster(data, file_name, target_view=None, profile=None, blockxsize=256, 'height' : height, 'width' : width } - profile.update(profile_updates) + profile.update(profile_updates | kwargs) with rasterio.open(file_name, 'w', **profile) as dst: dst.write(np.asarray(data), 1) From e91cf10275ad9d4ccfd30a5df852b23503d51c73 Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Fri, 10 Feb 2023 10:27:10 +0100 Subject: [PATCH 2/5] #213 - pgrid.to_raster calls io.to_raster --- pysheds/pgrid.py | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/pysheds/pgrid.py b/pysheds/pgrid.py index 82574c1..c68a70e 100644 --- a/pysheds/pgrid.py +++ b/pysheds/pgrid.py @@ -2591,35 +2591,19 @@ def to_raster(self, data_name, file_name, profile=None, view=True, blockxsize=25 dtype: numpy datatype Desired datatype of the output array. """ - # TODO: Should probably replace with input handler to remain consistent - if view: - data = self.view(data_name, apply_mask=apply_mask, nodata=nodata, - interpolation=interpolation, as_crs=as_crs, kx=kx, ky=ky, s=s, - tolerance=tolerance, dtype=dtype, **kwargs) - else: - data = getattr(self, data_name) - height, width = data.shape - default_blockx = width - default_profile = { - 'driver' : 'GTiff', - 'blockxsize' : blockxsize, - 'blockysize' : blockysize, - 'count': 1, - 'tiled' : True - } - if not profile: - profile = default_profile - profile_updates = { - 'crs' : data.crs.srs, - 'transform' : data.affine, - 'dtype' : data.dtype.name, - 'nodata' : data.nodata, - 'height' : height, - 'width' : width - } - profile.update(profile_updates) - with rasterio.open(file_name, 'w', **profile) as dst: - dst.write(np.asarray(data), 1) + if target_view is None: + target_view = self.viewfinder + return pysheds.io.to_raster(data, file_name, target_view=target_view, + profile=profile, blockxsize=blockxsize, + blockysize=blockysize, + interpolation=interpolation, + apply_input_mask=apply_input_mask, + apply_output_mask=apply_output_mask, + inherit_nodata=inherit_nodata, + affine=affine, shape=shape, crs=crs, + mask=mask, nodata=nodata, dtype=dtype, + **kwargs) + def extract_profiles(self, fdir, mask, dirmap=None, nodata_in=None, routing='d8', apply_mask=True, ignore_metadata=False, **kwargs): From c3c6ea0437452dd6d7c3cbdd0dbdfbef4ee92a9a Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Mon, 13 Feb 2023 14:31:12 +0100 Subject: [PATCH 3/5] Reversed changes to pgrid.py --- pysheds/pgrid.py | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/pysheds/pgrid.py b/pysheds/pgrid.py index c68a70e..82574c1 100644 --- a/pysheds/pgrid.py +++ b/pysheds/pgrid.py @@ -2591,19 +2591,35 @@ def to_raster(self, data_name, file_name, profile=None, view=True, blockxsize=25 dtype: numpy datatype Desired datatype of the output array. """ - if target_view is None: - target_view = self.viewfinder - return pysheds.io.to_raster(data, file_name, target_view=target_view, - profile=profile, blockxsize=blockxsize, - blockysize=blockysize, - interpolation=interpolation, - apply_input_mask=apply_input_mask, - apply_output_mask=apply_output_mask, - inherit_nodata=inherit_nodata, - affine=affine, shape=shape, crs=crs, - mask=mask, nodata=nodata, dtype=dtype, - **kwargs) - + # TODO: Should probably replace with input handler to remain consistent + if view: + data = self.view(data_name, apply_mask=apply_mask, nodata=nodata, + interpolation=interpolation, as_crs=as_crs, kx=kx, ky=ky, s=s, + tolerance=tolerance, dtype=dtype, **kwargs) + else: + data = getattr(self, data_name) + height, width = data.shape + default_blockx = width + default_profile = { + 'driver' : 'GTiff', + 'blockxsize' : blockxsize, + 'blockysize' : blockysize, + 'count': 1, + 'tiled' : True + } + if not profile: + profile = default_profile + profile_updates = { + 'crs' : data.crs.srs, + 'transform' : data.affine, + 'dtype' : data.dtype.name, + 'nodata' : data.nodata, + 'height' : height, + 'width' : width + } + profile.update(profile_updates) + with rasterio.open(file_name, 'w', **profile) as dst: + dst.write(np.asarray(data), 1) def extract_profiles(self, fdir, mask, dirmap=None, nodata_in=None, routing='d8', apply_mask=True, ignore_metadata=False, **kwargs): From b180c289e7227d7c036e9aeb06c6e5bea535b3a9 Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Wed, 15 Feb 2023 19:40:49 +0100 Subject: [PATCH 4/5] Using syntax for Python <=3.8 --- pysheds/io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pysheds/io.py b/pysheds/io.py index bf866c0..b5238da 100644 --- a/pysheds/io.py +++ b/pysheds/io.py @@ -299,7 +299,8 @@ def to_raster(data, file_name, target_view=None, profile=None, blockxsize=256, 'height' : height, 'width' : width } - profile.update(profile_updates | kwargs) + profile.update(profile_updates) + profile.update(kwargs) with rasterio.open(file_name, 'w', **profile) as dst: dst.write(np.asarray(data), 1) From 029746643f60e83d9d650e7823b0f5bf37539e99 Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Thu, 16 Mar 2023 10:35:51 +0100 Subject: [PATCH 5/5] Added a test for using kwargs to be passed to rasterio.open --- tests/test_grid.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_grid.py b/tests/test_grid.py index 23bc342..291a350 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -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)