Skip to content

Commit cbf22cf

Browse files
authored
Merge pull request #259 from tyler-c2s/view-apply-function
handle optional arguments in View Extension
2 parents 1174848 + 72808cb commit cbf22cf

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [unreleased]
4+
5+
### Fixed
6+
7+
- Fix handling of optional properties when using apply on view extension ([#259](https://github.com/stac-utils/pystac/pull/259))
8+
39
## [v0.5.4]
410

511
### Added

pystac/extensions/view.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import pystac
12
from pystac import Extensions
23
from pystac.item import Item
34
from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject)
5+
from typing import Optional
46

57

68
class ViewItemExt(ItemExtension):
@@ -28,11 +30,11 @@ def __init__(self, item):
2830
self.item = item
2931

3032
def apply(self,
31-
off_nadir=None,
32-
incidence_angle=None,
33-
azimuth=None,
34-
sun_azimuth=None,
35-
sun_elevation=None):
33+
off_nadir: Optional[float] = None,
34+
incidence_angle: Optional[float] = None,
35+
azimuth: Optional[float] = None,
36+
sun_azimuth: Optional[float] = None,
37+
sun_elevation: Optional[float] = None):
3638
"""Applies View Geometry extension properties to the extended Item.
3739
3840
Args:
@@ -49,11 +51,21 @@ def apply(self,
4951
sun_elevation (float): Sun elevation angle. The angle from the tangent of the scene
5052
center point to the sun. Measured from the horizon in degrees (0-90).
5153
"""
52-
self.off_nadir = off_nadir
53-
self.incidence_angle = incidence_angle
54-
self.azimuth = azimuth
55-
self.sun_azimuth = sun_azimuth
56-
self.sun_elevation = sun_elevation
54+
if (off_nadir is None and incidence_angle is None and azimuth is None
55+
and sun_azimuth is None and sun_elevation is None):
56+
raise pystac.STACError(
57+
'Must provide at least one of: off_nadir, incidence_angle, azimuth, sun_azimuth, sun_elevation' # noqa: E501
58+
)
59+
if off_nadir:
60+
self.off_nadir = off_nadir
61+
if incidence_angle:
62+
self.incidence_angle = incidence_angle
63+
if azimuth:
64+
self.azimuth = azimuth
65+
if sun_azimuth:
66+
self.sun_azimuth = sun_azimuth
67+
if sun_elevation:
68+
self.sun_elevation = sun_elevation
5769

5870
@property
5971
def off_nadir(self):

tests/extensions/test_view.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ def test_apply(self):
2929
sun_azimuth=2.0,
3030
sun_elevation=1.0)
3131

32+
def test_apply_one(self):
33+
item = next(TestCases.test_case_2().get_all_items())
34+
with self.assertRaises(ExtensionError):
35+
item.ext.view
36+
37+
item.ext.enable(Extensions.VIEW)
38+
item.ext.view.apply(off_nadir=1.0)
39+
40+
@unittest.expectedFailure
41+
def test_apply_none(self):
42+
item = next(TestCases.test_case_2().get_all_items())
43+
with self.assertRaises(ExtensionError):
44+
item.ext.view
45+
46+
item.ext.enable(Extensions.VIEW)
47+
item.ext.view.apply(
48+
off_nadir=None,
49+
incidence_angle=None,
50+
azimuth=None,
51+
sun_azimuth=None,
52+
sun_elevation=None,
53+
)
54+
3255
def test_validate_view(self):
3356
item = pystac.read_file(self.example_uri)
3457
item.validate()

0 commit comments

Comments
 (0)