diff --git a/pyproject.toml b/pyproject.toml index 7abd29e..17b6b7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "yirgacheffe" -version = "0.9.1" +version = "0.9.2" description = "Abstraction of gdal datasets for doing basic math operations" readme = "README.md" authors = [{ name = "Michael Dales", email = "mwd24@cam.ac.uk" }] diff --git a/tests/test_operators.py b/tests/test_operators.py index c27986a..f58adac 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -908,4 +908,32 @@ def test_exp2() -> None: expected = np.exp2(data1) actual = result.read_array(0, 0, 4, 2) - assert (expected == actual).all() \ No newline at end of file + assert (expected == actual).all() + +def test_minimum_layers() -> None: + data1 = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 2.0, 7.0, 8.0]]) + data2 = np.array([[3.0, 2.0, 1.0, 4.0], [8.0, 2.0, 5.0, 7.0]]) + layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1)) + layer2 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data2)) + result = RasterLayer.empty_raster_layer_like(layer1) + + comp = LayerOperation.minimum(layer1, layer2) + comp.save(result) + + expected = np.minimum(data1, data2) + actual = result.read_array(0, 0, 4, 2) + assert (expected == actual).all() + +def test_maximum_layers() -> None: + data1 = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 2.0, 7.0, 8.0]]) + data2 = np.array([[3.0, 2.0, 1.0, 4.0], [8.0, 2.0, 5.0, 7.0]]) + layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1)) + layer2 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data2)) + result = RasterLayer.empty_raster_layer_like(layer1) + + comp = LayerOperation.maximum(layer1, layer2) + comp.save(result) + + expected = np.maximum(data1, data2) + actual = result.read_array(0, 0, 4, 2) + assert (expected == actual).all() diff --git a/yirgacheffe/operators.py b/yirgacheffe/operators.py index a43428b..1e02865 100644 --- a/yirgacheffe/operators.py +++ b/yirgacheffe/operators.py @@ -147,6 +147,22 @@ def where(cond, a, b): other=b ) + @staticmethod + def maximum(a, b): + return LayerOperation( + a, + np.maximum, + b, + ) + + @staticmethod + def minimum(a, b): + return LayerOperation( + a, + np.minimum, + rhs=b, + ) + def __init__(self, lhs, operator=None, rhs=None, other=None): self.ystep = constants.YSTEP @@ -507,3 +523,7 @@ def _eval(self, index, step): result[yoffset][xoffset] = self.operator(lhs_val) return result + +where = LayerOperation.where +minumum = LayerOperation.minimum +maximum = LayerOperation.maximum