Skip to content

Commit

Permalink
test: add test for the tiling function
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Feb 13, 2025
1 parent c1c6715 commit 5555068
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion tests/test_raster/test_georeferencing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import geoutils as gu
from geoutils import examples
from geoutils.raster.georeferencing import _generate_tiling_grid


class TestGeoreferencing:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -209,3 +210,58 @@ 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)

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

0 comments on commit 5555068

Please sign in to comment.