From 31eef48671083c8ef2b48d03ad5740d69b518f36 Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:23:33 -0500 Subject: [PATCH 1/7] Update view extension apply method - allow for optional arguments in apply - raise error if all arguments are None --- pystac/extensions/view.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index 6b3e76132..bf6bc507d 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -28,11 +28,11 @@ def __init__(self, item): self.item = item def apply(self, - off_nadir=None, - incidence_angle=None, - azimuth=None, - sun_azimuth=None, - sun_elevation=None): + off_nadir: Optional[float] = None, + incidence_angle: Optional[float] = None, + azimuth: Optional[float] = None, + sun_azimuth: Optional[float] = None, + sun_elevation: Optional[float] = None): """Applies View Geometry extension properties to the extended Item. Args: @@ -49,11 +49,21 @@ def apply(self, sun_elevation (float): Sun elevation angle. The angle from the tangent of the scene center point to the sun. Measured from the horizon in degrees (0-90). """ - self.off_nadir = off_nadir - self.incidence_angle = incidence_angle - self.azimuth = azimuth - self.sun_azimuth = sun_azimuth - self.sun_elevation = sun_elevation + if (off_nadir is None and incidence_angle is None and azimuth is None + and sun_azimuth is None and sun_elevation is None): + raise pystac.STACError( + 'Must provide at least one of: off_nadir, incidence_angle, azimuth, sun_azimuth, sun_elevation' # noqa: E501 + ) + if off_nadir: + self.off_nadir = off_nadir + if incidence_angle: + self.incidence_angle = incidence_angle + if azimuth: + self.azimuth = azimuth + if sun_azimuth: + self.sun_azimuth = sun_azimuth + if sun_elevation: + self.sun_elevation = sun_elevation @property def off_nadir(self): From fb566fc44e9462e4f2a4af34a0ea21ecb68d30d0 Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:26:01 -0500 Subject: [PATCH 2/7] add missing import to view --- pystac/extensions/view.py | 1 + tests/extensions/test_view.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index bf6bc507d..3d46671f9 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -1,6 +1,7 @@ from pystac import Extensions from pystac.item import Item from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject) +from typing import Optional class ViewItemExt(ItemExtension): diff --git a/tests/extensions/test_view.py b/tests/extensions/test_view.py index 0ce629298..24030899f 100644 --- a/tests/extensions/test_view.py +++ b/tests/extensions/test_view.py @@ -29,6 +29,29 @@ def test_apply(self): sun_azimuth=2.0, sun_elevation=1.0) + def test_apply_one(self): + item = next(TestCases.test_case_2().get_all_items()) + with self.assertRaises(ExtensionError): + item.ext.view + + item.ext.enable(Extensions.VIEW) + item.ext.view.apply(off_nadir=1.0) + + @unittest.expectedFailure + def test_apply_none(self): + item = next(TestCases.test_case_2().get_all_items()) + with self.assertRaises(ExtensionError): + item.ext.view + + item.ext.enable(Extensions.VIEW) + item.ext.view.apply( + off_nadir=None, + incidence_angle=None, + azimuth=None, + sun_azimuth=None, + sun_elevation=None, + ) + def test_validate_view(self): item = pystac.read_file(self.example_uri) item.validate() From e894952a4560d0649373fb770a28f2e04a6499fe Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:26:26 -0500 Subject: [PATCH 3/7] Revert "add missing import to view" This reverts commit fb566fc44e9462e4f2a4af34a0ea21ecb68d30d0. --- pystac/extensions/view.py | 1 - tests/extensions/test_view.py | 23 ----------------------- 2 files changed, 24 deletions(-) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index 3d46671f9..bf6bc507d 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -1,7 +1,6 @@ from pystac import Extensions from pystac.item import Item from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject) -from typing import Optional class ViewItemExt(ItemExtension): diff --git a/tests/extensions/test_view.py b/tests/extensions/test_view.py index 24030899f..0ce629298 100644 --- a/tests/extensions/test_view.py +++ b/tests/extensions/test_view.py @@ -29,29 +29,6 @@ def test_apply(self): sun_azimuth=2.0, sun_elevation=1.0) - def test_apply_one(self): - item = next(TestCases.test_case_2().get_all_items()) - with self.assertRaises(ExtensionError): - item.ext.view - - item.ext.enable(Extensions.VIEW) - item.ext.view.apply(off_nadir=1.0) - - @unittest.expectedFailure - def test_apply_none(self): - item = next(TestCases.test_case_2().get_all_items()) - with self.assertRaises(ExtensionError): - item.ext.view - - item.ext.enable(Extensions.VIEW) - item.ext.view.apply( - off_nadir=None, - incidence_angle=None, - azimuth=None, - sun_azimuth=None, - sun_elevation=None, - ) - def test_validate_view(self): item = pystac.read_file(self.example_uri) item.validate() From b81363552f5dc535c6c02b51c7980f4aa6995522 Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:27:31 -0500 Subject: [PATCH 4/7] add missing import --- pystac/extensions/view.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index bf6bc507d..3d46671f9 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -1,6 +1,7 @@ from pystac import Extensions from pystac.item import Item from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject) +from typing import Optional class ViewItemExt(ItemExtension): From dad49c78b585790a11466e0386481d456baaede1 Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:27:45 -0500 Subject: [PATCH 5/7] add tests for updated apply function --- tests/extensions/test_view.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/extensions/test_view.py b/tests/extensions/test_view.py index 0ce629298..24030899f 100644 --- a/tests/extensions/test_view.py +++ b/tests/extensions/test_view.py @@ -29,6 +29,29 @@ def test_apply(self): sun_azimuth=2.0, sun_elevation=1.0) + def test_apply_one(self): + item = next(TestCases.test_case_2().get_all_items()) + with self.assertRaises(ExtensionError): + item.ext.view + + item.ext.enable(Extensions.VIEW) + item.ext.view.apply(off_nadir=1.0) + + @unittest.expectedFailure + def test_apply_none(self): + item = next(TestCases.test_case_2().get_all_items()) + with self.assertRaises(ExtensionError): + item.ext.view + + item.ext.enable(Extensions.VIEW) + item.ext.view.apply( + off_nadir=None, + incidence_angle=None, + azimuth=None, + sun_azimuth=None, + sun_elevation=None, + ) + def test_validate_view(self): item = pystac.read_file(self.example_uri) item.validate() From 94f35e1f641f2242c262ff3705cd032147b406c4 Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 9 Feb 2021 12:31:01 -0500 Subject: [PATCH 6/7] missing pystac import for view error raise --- pystac/extensions/view.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index 3d46671f9..bd0a2ae00 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -1,3 +1,4 @@ +import pystac from pystac import Extensions from pystac.item import Item from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject) From 72808cb35d806a0069c8cdac72b9780eff7e195f Mon Sep 17 00:00:00 2001 From: Tyler Anderson <54593691+tyler-c2s@users.noreply.github.com> Date: Tue, 16 Feb 2021 17:36:43 -0500 Subject: [PATCH 7/7] add view fix to changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 187a70261..966f4651e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [unreleased] + +### Fixed + +- Fix handling of optional properties when using apply on view extension ([#259](https://github.com/stac-utils/pystac/pull/259)) + ## [v0.5.4] ### Added