From 3069be1602bf0c5213101151fd82963d42031d69 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Thu, 27 Jan 2022 23:53:25 -0600 Subject: [PATCH 1/3] BoundingBox: add volume attribute --- tests/datasets/test_utils.py | 22 ++++++++++++++++++++++ torchgeo/datasets/utils.py | 13 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/datasets/test_utils.py b/tests/datasets/test_utils.py index ad98f2f0706..8476f08933b 100644 --- a/tests/datasets/test_utils.py +++ b/tests/datasets/test_utils.py @@ -331,6 +331,28 @@ 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), 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..33f76a9802e 100644 --- a/torchgeo/datasets/utils.py +++ b/torchgeo/datasets/utils.py @@ -352,6 +352,19 @@ def __and__(self, other: "BoundingBox") -> "BoundingBox": except ValueError: raise ValueError(f"Bounding boxes {self} and {other} do not overlap") + @property + def volume(self) -> float: + """Volume of bounding box. + + Returns: + volume + + .. versionadded:: 0.3 + """ + return ( + (self.maxx - self.minx) * (self.maxy - self.miny) * (self.maxt - self.mint) + ) + def intersects(self, other: "BoundingBox") -> bool: """Whether or not two bounding boxes intersect. From d9623dd013b97046d399f15bb46ca7e5add1240e Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Fri, 28 Jan 2022 14:03:13 -0600 Subject: [PATCH 2/3] Style fixes --- tests/datasets/test_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/datasets/test_utils.py b/tests/datasets/test_utils.py index 8476f08933b..7a35c0bdddb 100644 --- a/tests/datasets/test_utils.py +++ b/tests/datasets/test_utils.py @@ -346,9 +346,7 @@ def test_and_no_intersection( ], ) def test_volume( - self, - test_input: Tuple[float, float, float, float, float, float], - expected: int, + self, test_input: Tuple[float, float, float, float, float, float], expected: int ) -> None: bbox = BoundingBox(*test_input) assert bbox.volume == expected From d93701631fad788c9aed2d908480685823988687 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Tue, 8 Feb 2022 16:35:24 -0600 Subject: [PATCH 3/3] BoundingBox: add area attribute --- tests/datasets/test_utils.py | 20 ++++++++++++++++++++ torchgeo/datasets/utils.py | 19 ++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/datasets/test_utils.py b/tests/datasets/test_utils.py index 7a35c0bdddb..ba8ebc873f9 100644 --- a/tests/datasets/test_utils.py +++ b/tests/datasets/test_utils.py @@ -331,6 +331,26 @@ 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", [ diff --git a/torchgeo/datasets/utils.py b/torchgeo/datasets/utils.py index 33f76a9802e..6e9de5a0476 100644 --- a/torchgeo/datasets/utils.py +++ b/torchgeo/datasets/utils.py @@ -352,18 +352,31 @@ 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.maxx - self.minx) * (self.maxy - self.miny) * (self.maxt - self.mint) - ) + return self.area * (self.maxt - self.mint) def intersects(self, other: "BoundingBox") -> bool: """Whether or not two bounding boxes intersect.