Skip to content

Commit

Permalink
test: add a test with a georef roi
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Dec 19, 2024
1 parent 4cb28af commit 5506a18
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions tests/test_raster/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestRaster:
aster_dem_path = examples.get_path("exploradores_aster_dem")
aster_outlines_path = examples.get_path("exploradores_rgi_outlines")

roi = {"left": 100, "bottom": 100, "right": 400, "top": 400}
roi_pix = {"x": 50, "y": 100, "w": 400, "h": 300}

@pytest.mark.parametrize("example", [landsat_b4_path, aster_dem_path]) # type: ignore
def test_init(self, example: str) -> None:
Expand Down Expand Up @@ -72,8 +72,10 @@ def test_init(self, example: str) -> None:
)

# Passing a raster with a roi
r2_roi = gu.Raster(r0, roi=self.roi)
r0_roi = gu.Raster(example, roi=self.roi)
roi_pix = self.roi_pix.copy()
r2_roi = gu.Raster(r0, roi=roi_pix)
roi_pix = self.roi_pix.copy()
r0_roi = gu.Raster(example, roi=roi_pix)
assert r2_roi.raster_equal(r0_roi)

# For re-instantiation via Raster (r2 above), we check the behaviour:
Expand Down Expand Up @@ -312,37 +314,35 @@ def test_raster_with_roi(self, example: str) -> None:
"""
Test loading raster data with a region of interest.
"""

roi_pix = self.roi_pix.copy()
# load a raster (r1) and the same with a roi (r2)
r1 = gu.Raster(example, load_data=True)
data_test = r1.data[
..., r1.height - self.roi["top"] : r1.height - self.roi["bottom"], self.roi["left"] : self.roi["right"]
...,
roi_pix["y"] : roi_pix["y"] + roi_pix["h"],
roi_pix["x"] : roi_pix["x"] + roi_pix["w"],
]
r2 = gu.Raster(example, load_data=True, roi=self.roi)
r2 = gu.Raster(example, load_data=True, roi=roi_pix)

# Check the height and the width of the raster with roi
expected_height = self.roi["top"] - self.roi["bottom"]
expected_width = self.roi["right"] - self.roi["left"]

assert r2.height == expected_height
assert r2.width == expected_width

assert r2.data.shape[1] == expected_height
assert r2.data.shape[2] == expected_width if r2.data.ndim == 3 else r2.data.shape[0] == expected_height
assert r2.height == roi_pix["h"]
assert r2.width == roi_pix["w"]

# Check that the roi has been applied as expected
assert np.array_equal(r2.data, data_test)

# Check that the transform has been correctly modified
pixel_size_x = r1.transform.a
pixel_size_y = r1.transform.e
expected_transform = rio.transform.Affine(r1.transform.a, 0, roi_pix["left"], 0, r1.transform.e, roi_pix["top"])

new_origin_x = r1.transform.c + self.roi["left"] * pixel_size_x
new_origin_y = r1.transform.f + (r1.height - self.roi["top"]) * pixel_size_y
assert r2.transform == expected_transform

expected_transform = rio.transform.Affine(pixel_size_x, 0, new_origin_x, 0, pixel_size_y, new_origin_y)
# Check the behavior with georeferenced coordinates
left, top = r1.transform * (roi_pix["x"], roi_pix["y"])
right, bottom = r1.transform * (roi_pix["x"] + roi_pix["w"], roi_pix["y"] + roi_pix["h"])
roi_georef = {"left": left, "bottom": bottom, "right": right, "top": top, "crs": r1.crs}
r3 = gu.Raster(example, load_data=True, roi=roi_georef)

assert r2.transform == expected_transform
assert r3.raster_equal(r2)

@pytest.mark.parametrize("example", [landsat_b4_path, aster_dem_path, landsat_rgb_path]) # type: ignore
def test_load_only_mask(self, example: str) -> None:
Expand Down Expand Up @@ -1880,9 +1880,11 @@ def test_from_array(self, example: str) -> None:
assert out_img.data.mask[0, 0]

# Test with roi
roi_pix = self.roi_pix.copy()
img = gu.Raster(example)
out_img = gu.Raster.from_array(img.data, img.transform, img.crs, nodata=img.nodata, roi=self.roi)
roi_img = gu.Raster(example, roi=self.roi)
out_img = gu.Raster.from_array(img.data, img.transform, img.crs, nodata=img.nodata, roi=roi_pix)
roi_pix = self.roi_pix.copy()
roi_img = gu.Raster(example, roi=roi_pix)
assert roi_img.raster_equal(out_img)

# Check that error is raised if the transform is not affine
Expand Down

0 comments on commit 5506a18

Please sign in to comment.