Skip to content

Commit

Permalink
Add resize method for rpc rasters and unit test
Browse files Browse the repository at this point in the history
Upload new rpcs raster
  • Loading branch information
enomis-dev committed Mar 21, 2022
1 parent 43daf85 commit 7633736
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
25 changes: 22 additions & 3 deletions telluric/georaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,14 @@ def _resize(self, ratio_x, ratio_y, resampling):
"""Return raster resized by ratio."""
new_width = int(np.ceil(self.width * ratio_x))
new_height = int(np.ceil(self.height * ratio_y))
dest_affine = self.affine * Affine.scale(1 / ratio_x, 1 / ratio_y)

if self.crs is None and self.rpcs is not None:
# resize raster with rpcs
dest_rpcs = self._resize_rpcs(ratio_x, ratio_y)
dest_affine = self.affine
else:
dest_affine = self.affine * Affine.scale(1 / ratio_x, 1 / ratio_y)
dest_rpcs = self.rpcs

window = rasterio.windows.Window(0, 0, self.width, self.height)
resized_raster = self.get_window(
Expand All @@ -1417,10 +1424,20 @@ def _resize(self, ratio_x, ratio_y, resampling):
ysize=new_height,
resampling=resampling,
affine=dest_affine,
rpcs=dest_rpcs
)

return resized_raster

def _resize_rpcs(self, ratio_x, ratio_y):
"""resize raster by using its rpcs"""
dest_rpcs = copy(self.rpcs)
dest_rpcs.line_off = dest_rpcs.line_off * ratio_y
dest_rpcs.samp_off = dest_rpcs.samp_off * ratio_x
dest_rpcs.line_scale = dest_rpcs.line_scale * ratio_y
dest_rpcs.samp_scale = dest_rpcs.samp_scale * ratio_x
return dest_rpcs

def to_pillow_image(self, return_mask=False):
"""Return Pillow. Image, and optionally also mask."""
img = np.rollaxis(np.rollaxis(self.image.data, 2), 2)
Expand Down Expand Up @@ -1991,7 +2008,7 @@ def _read_with_mask(raster, masked):

def get_window(self, window, bands=None,
xsize=None, ysize=None,
resampling=Resampling.cubic, masked=None, affine=None
resampling=Resampling.cubic, masked=None, affine=None, rpcs=None
):
"""Get window from raster.
Expand All @@ -2002,6 +2019,8 @@ def get_window(self, window, bands=None,
:param resampling: which Resampling to use on reading, default Resampling.cubic
:param masked: if True uses the maks, if False doesn't use the mask, if None looks to see if there is a mask,
if mask exists using it, the default None
:param affine: Set destination affine otherwise calculate from output window shape
:param rpcs: If not none set destination rpcs
:return: GeoRaster2 of tile
"""
bands = bands or list(range(1, self.num_bands + 1))
Expand All @@ -2023,7 +2042,7 @@ def get_window(self, window, bands=None,
array = raster.read(bands, **read_params)
nodata = 0 if not np.ma.isMaskedArray(array) else None
affine = affine or self._calculate_new_affine(window, out_shape[2], out_shape[1])
raster = self.copy_with(image=array, affine=affine, nodata=nodata)
raster = self.copy_with(image=array, affine=affine, nodata=nodata, rpcs=rpcs)

return raster

Expand Down
Binary file modified tests/data/raster/nir.tif
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_georaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ def test_resize(raster):
with pytest.raises(GeoRaster2Error):
raster.resize(ratio_x=2)

# test rpcs resize
rpcs_raster = GeoRaster2.open("tests/data/raster/nir.tif")
test_raster = rpcs_raster.resize(ratio=0.5)
assert ((test_raster.width == 0.5 * rpcs_raster.width)
and (test_raster.rpcs.line_off == 0.5 * rpcs_raster.rpcs.line_off)
and (test_raster.rpcs.samp_off == 0.5 * rpcs_raster.rpcs.samp_off)
and (test_raster.rpcs.line_scale == 0.5 * rpcs_raster.rpcs.line_scale)
and (test_raster.rpcs.samp_scale == 0.5 * rpcs_raster.rpcs.samp_scale))


def test_to_pillow_image():
# without mask:
Expand Down

0 comments on commit 7633736

Please sign in to comment.