From b52fca04ed54fb3ecac818f2378ceb5c420bf226 Mon Sep 17 00:00:00 2001 From: "Rodrigo E. Principe" Date: Fri, 17 Jan 2025 10:55:39 -0300 Subject: [PATCH] feat: add region param to image regression fixture --- pytest_gee/image_regression.py | 11 +++++++++-- tests/test_pytest_gee.py | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pytest_gee/image_regression.py b/pytest_gee/image_regression.py index e455cdd..5d4c850 100644 --- a/pytest_gee/image_regression.py +++ b/pytest_gee/image_regression.py @@ -2,7 +2,7 @@ import os from contextlib import suppress -from typing import Optional +from typing import Optional, Union import ee import requests @@ -23,6 +23,7 @@ def check( fullpath: Optional[os.PathLike] = None, scale: Optional[int] = 30, viz_params: Optional[dict] = None, + region: Optional[Union[ee.FeatureCollection, ee.Feature, ee.Geometry]] = None, ): """Check the given image against a previously recorded version, or generate a new file. @@ -40,9 +41,15 @@ def check( fullpath: complete path to use as a reference file. This option will ignore ``datadir`` fixture when reading *expected* files but will still use it to write *obtained* files. Useful if a reference file is located in the session data dir for example. scale: The scale to use for the thumbnail. viz_params: The visualization parameters to use for the thumbnail. If not given, the min and max values of the image will be used. + region:The region to use for clipping the image. If not given, the image's region will be used. """ # rescale the original image - geometry = data_image.geometry() + if isinstance(region, (ee.FeatureCollection, ee.Feature)): + geometry = region.geometry() + elif isinstance(region, ee.Geometry): + geometry = region + else: + geometry = data_image.geometry() data_image = data_image.clipToBoundsAndScale(geometry, scale=scale) # build the different filename to be consistent between our 3 checks diff --git a/tests/test_pytest_gee.py b/tests/test_pytest_gee.py index c8fe954..97c94de 100644 --- a/tests/test_pytest_gee.py +++ b/tests/test_pytest_gee.py @@ -112,3 +112,10 @@ def test_image_regression_with_viz(ee_image_regression): palette = ["#000004", "#2C105C", "#711F81", "#B63679", "#EE605E", "#FDAE78", "#FCFDBF"] viz = {"bands": ["nd"], "min": 0.0122, "max": 1.237, "palette": palette} ee_image_regression.check(image, scale=1000, viz_params=viz) + + +def test_image_regression_with_region(ee_image_regression): + """Test the image_regression fixture.""" + image = ee.Image(landsat_image).normalizedDifference(["SR_B5", "SR_B4"]) + vatican = ee.Geometry.Point([12.453585, 41.903115]).buffer(100) + ee_image_regression.check(image, scale=30, region=vatican)