Skip to content

Commit

Permalink
feat: add region param to image regression fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
fitoprincipe committed Jan 17, 2025
1 parent 0ea9fa5 commit b52fca0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pytest_gee/image_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
from contextlib import suppress
from typing import Optional
from typing import Optional, Union

import ee
import requests
Expand All @@ -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.
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_pytest_gee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b52fca0

Please sign in to comment.