diff --git a/tests/datasets/test_utils.py b/tests/datasets/test_utils.py index ad98f2f0706..ba8ebc873f9 100644 --- a/tests/datasets/test_utils.py +++ b/tests/datasets/test_utils.py @@ -331,6 +331,46 @@ def test_and_no_intersection( ): bbox1 & bbox2 + @pytest.mark.parametrize( + "test_input,expected", + [ + # Rectangular prism + ((0, 1, 0, 1, 0, 1), 1), + ((0, 2, 0, 3, 0, 4), 6), + # Plane + ((0, 0, 0, 1, 0, 1), 0), + # Line + ((0, 0, 0, 0, 0, 1), 0), + # Point + ((0, 0, 0, 0, 0, 0), 0), + ], + ) + def test_area( + self, test_input: Tuple[float, float, float, float, float, float], expected: int + ) -> None: + bbox = BoundingBox(*test_input) + assert bbox.area == expected + + @pytest.mark.parametrize( + "test_input,expected", + [ + # Rectangular prism + ((0, 1, 0, 1, 0, 1), 1), + ((0, 2, 0, 3, 0, 4), 24), + # Plane + ((0, 0, 0, 1, 0, 1), 0), + # Line + ((0, 0, 0, 0, 0, 1), 0), + # Point + ((0, 0, 0, 0, 0, 0), 0), + ], + ) + def test_volume( + self, test_input: Tuple[float, float, float, float, float, float], expected: int + ) -> None: + bbox = BoundingBox(*test_input) + assert bbox.volume == expected + @pytest.mark.parametrize( "test_input,expected", [ diff --git a/torchgeo/datasets/utils.py b/torchgeo/datasets/utils.py index c5deff34470..6e9de5a0476 100644 --- a/torchgeo/datasets/utils.py +++ b/torchgeo/datasets/utils.py @@ -352,6 +352,32 @@ def __and__(self, other: "BoundingBox") -> "BoundingBox": except ValueError: raise ValueError(f"Bounding boxes {self} and {other} do not overlap") + @property + def area(self) -> float: + """Area of bounding box. + + Area is defined as spatial area. + + Returns: + area + + .. versionadded:: 0.3 + """ + return (self.maxx - self.minx) * (self.maxy - self.miny) + + @property + def volume(self) -> float: + """Volume of bounding box. + + Volume is defined as spatial area times temporal range. + + Returns: + volume + + .. versionadded:: 0.3 + """ + return self.area * (self.maxt - self.mint) + def intersects(self, other: "BoundingBox") -> bool: """Whether or not two bounding boxes intersect.