From b787b68f051255940dbd334a1c1b24f74f95fdc6 Mon Sep 17 00:00:00 2001 From: vschaffn Date: Thu, 13 Feb 2025 17:31:50 +0100 Subject: [PATCH] test: add test for the tiling function --- tests/test_raster/test_georeferencing.py | 59 +++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/test_raster/test_georeferencing.py b/tests/test_raster/test_georeferencing.py index fe2fcba9..1a16a936 100644 --- a/tests/test_raster/test_georeferencing.py +++ b/tests/test_raster/test_georeferencing.py @@ -5,6 +5,7 @@ import geoutils as gu from geoutils import examples +from geoutils.raster.georeferencing import _generate_tiling_grid class TestGeoreferencing: @@ -173,7 +174,7 @@ def test_xy2ij(self) -> None: @pytest.mark.parametrize("example", [landsat_b4_path, aster_dem_path, landsat_rgb_path]) # type: ignore def test_coords(self, example: str) -> None: - img = gu.Raster(self.landsat_b4_path) + img = gu.Raster(example) # With lower left argument xx0, yy0 = img.coords(grid=False, force_offset="ll") @@ -209,3 +210,59 @@ def test_coords(self, example: str) -> None: xxgrid, yygrid = img.coords(grid=True, force_offset="ll") assert np.array_equal(xxgrid, np.repeat(xx0[np.newaxis, :], img.height, axis=0)) assert np.array_equal(yygrid, np.flipud(np.repeat(yy0[:, np.newaxis], img.width, axis=1))) + + @pytest.mark.parametrize("overlap", [0, 5]) # type: ignore + def test_tiling(self, overlap: int) -> None: + + # Test with mock data + tiling_grid_mock = _generate_tiling_grid(0, 0, 100, 100, 50, 50, overlap) + if overlap == 0: + expected_tiling = np.array([[[0, 50, 0, 50], [0, 50, 50, 100]], [[50, 100, 0, 50], [50, 100, 50, 100]]]) + assert np.array_equal(tiling_grid_mock, expected_tiling) + elif overlap == 5: + expected_tiling = np.array( + [ + [[0, 50, 0, 50], [0, 50, 45, 95], [0, 50, 90, 100]], + [[45, 95, 0, 50], [45, 95, 45, 95], [45, 95, 90, 100]], + [[90, 100, 0, 50], [90, 100, 45, 95], [90, 100, 90, 100]], + ] + ) + assert np.array_equal(tiling_grid_mock, expected_tiling) + + # Test with real data + img = gu.Raster(self.landsat_b4_path) + + # Define tiling parameters + row_split, col_split = 100, 100 + row_max, col_max = img.shape + + # Generate the tiling grid + tiling_grid = _generate_tiling_grid(0, 0, row_max, col_max, row_split, col_split, overlap) + + # Calculate expected number of tiles + nb_row_tiles = np.ceil(row_max / (row_split - overlap)).astype(int) + nb_col_tiles = np.ceil(col_max / (col_split - overlap)).astype(int) + + # Check that the tiling grid has the expected shape + assert tiling_grid.shape == (nb_row_tiles, nb_col_tiles, 4) + + # Check the boundaries of the first and last tile + assert np.array_equal(tiling_grid[0, 0], np.array([0, min(row_split, row_max), 0, min(col_split, col_max)])) + assert np.array_equal( + tiling_grid[-1, -1], + np.array( + [ + (nb_row_tiles - 1) * (row_split - overlap), + row_max, + (nb_col_tiles - 1) * (col_split - overlap), + col_max, + ] + ), + ) + + # Check if overlap is consistent between tiles + for row in range(nb_row_tiles - 1): + assert tiling_grid[row + 1, 0, 0] == tiling_grid[row, 0, 1] - overlap + + for col in range(nb_col_tiles - 1): # Skip last tile in column + assert tiling_grid[0, col + 1, 2] == tiling_grid[0, col, 3] - overlap