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

Fix unit test
  • Loading branch information
enomis-dev committed Mar 21, 2022
1 parent 43daf85 commit 12832b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 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.
21 changes: 15 additions & 6 deletions tests/test_georaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
some_raster_shrunk_mask = some_raster_multiband.copy_with(
image=np.ma.array(some_raster_multiband.image.data, mask=np.ma.nomask))

footprint_rpcs_raster = Polygon([[111.39116744, 34.92250008],
[111.44875176, 34.92250008],
[111.44875176, 34.90383334],
[111.39116744, 34.90383334]])
footprint_rpcs_raster = Polygon([[0.9505318899, 42.53085970],
[1.0180672794, 42.53085970],
[1.0180672794, 42.51064739],
[0.9505318899, 42.51064739]])


def test_construction():
Expand Down 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 Expand Up @@ -868,8 +877,8 @@ def test_reproject_rpcs():
raster = GeoRaster2.open("tests/data/raster/nir.tif")
reprojected = raster.reproject(dst_crs=WEB_MERCATOR_CRS,
rpcs=raster.rpcs)
assert reprojected.shape == (1, 2072, 5241)
assert reprojected.mean()[0] == pytest.approx(724.4861459505134, 1e-4)
assert reprojected.shape == (1, 2129, 5243)
assert reprojected.mean()[0] == pytest.approx(1726.74841213949, 1e-4)


def test_reproject_when_having_no_data_values_in_image():
Expand Down

0 comments on commit 12832b6

Please sign in to comment.