Skip to content

Commit d3c2322

Browse files
committed
Add resize method for rpc rasters and unit test
1 parent 766e122 commit d3c2322

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

telluric/georaster.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,14 @@ def _resize(self, ratio_x, ratio_y, resampling):
14081408
"""Return raster resized by ratio."""
14091409
new_width = int(np.ceil(self.width * ratio_x))
14101410
new_height = int(np.ceil(self.height * ratio_y))
1411-
dest_affine = self.affine * Affine.scale(1 / ratio_x, 1 / ratio_y)
1411+
1412+
if self.crs is None and self.rpcs is not None:
1413+
# resize raster with rpcs
1414+
dest_rpcs = self._resize_rpcs(ratio_x, ratio_y)
1415+
dest_affine = self.affine
1416+
else:
1417+
dest_affine = self.affine * Affine.scale(1 / ratio_x, 1 / ratio_y)
1418+
dest_rpcs = self.rpcs
14121419

14131420
window = rasterio.windows.Window(0, 0, self.width, self.height)
14141421
resized_raster = self.get_window(
@@ -1417,10 +1424,20 @@ def _resize(self, ratio_x, ratio_y, resampling):
14171424
ysize=new_height,
14181425
resampling=resampling,
14191426
affine=dest_affine,
1427+
rpcs=dest_rpcs
14201428
)
14211429

14221430
return resized_raster
14231431

1432+
def _resize_rpcs(self, ratio_x, ratio_y):
1433+
"""resize raster by using its rpcs"""
1434+
dest_rpcs = copy(self.rpcs)
1435+
dest_rpcs.line_off = dest_rpcs.line_off * ratio_y
1436+
dest_rpcs.samp_off = dest_rpcs.samp_off * ratio_x
1437+
dest_rpcs.line_scale = dest_rpcs.line_scale * ratio_y
1438+
dest_rpcs.samp_scale = dest_rpcs.samp_scale * ratio_x
1439+
return dest_rpcs
1440+
14241441
def to_pillow_image(self, return_mask=False):
14251442
"""Return Pillow. Image, and optionally also mask."""
14261443
img = np.rollaxis(np.rollaxis(self.image.data, 2), 2)
@@ -1997,7 +2014,7 @@ def _read_with_mask(raster, masked):
19972014

19982015
def get_window(self, window, bands=None,
19992016
xsize=None, ysize=None,
2000-
resampling=Resampling.cubic, masked=None, affine=None
2017+
resampling=Resampling.cubic, masked=None, affine=None, rpcs=None
20012018
):
20022019
"""Get window from raster.
20032020
@@ -2008,6 +2025,8 @@ def get_window(self, window, bands=None,
20082025
:param resampling: which Resampling to use on reading, default Resampling.cubic
20092026
:param masked: if True uses the maks, if False doesn't use the mask, if None looks to see if there is a mask,
20102027
if mask exists using it, the default None
2028+
:param affine: Set destination affine otherwise calculate from output window shape
2029+
:param rpcs: If not none set destination rpcs
20112030
:return: GeoRaster2 of tile
20122031
"""
20132032
bands = bands or list(range(1, self.num_bands + 1))
@@ -2029,7 +2048,7 @@ def get_window(self, window, bands=None,
20292048
array = raster.read(bands, **read_params)
20302049
nodata = 0 if not np.ma.isMaskedArray(array) else None
20312050
affine = affine or self._calculate_new_affine(window, out_shape[2], out_shape[1])
2032-
raster = self.copy_with(image=array, affine=affine, nodata=nodata)
2051+
raster = self.copy_with(image=array, affine=affine, nodata=nodata, rpcs=rpcs)
20332052

20342053
return raster
20352054

tests/test_georaster.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ def test_resize(raster):
253253
with pytest.raises(GeoRaster2Error):
254254
raster.resize(ratio_x=2)
255255

256+
# test rpcs resize
257+
rpcs_raster = GeoRaster2.open("tests/data/raster/grayscale.tif")
258+
test_raster = rpcs_raster.resize(ratio=0.5)
259+
assert ((test_raster.width == 0.5 * rpcs_raster.width)
260+
and (test_raster.rpcs.line_off == 0.5 * rpcs_raster.rpcs.line_off)
261+
and (test_raster.rpcs.samp_off == 0.5 * rpcs_raster.rpcs.samp_off)
262+
and (test_raster.rpcs.line_scale == 0.5 * rpcs_raster.rpcs.line_scale)
263+
and (test_raster.rpcs.samp_scale == 0.5 * rpcs_raster.rpcs.samp_scale))
264+
256265

257266
def test_to_pillow_image():
258267
# without mask:

0 commit comments

Comments
 (0)