Skip to content

Commit

Permalink
Merge pull request #259 from tyler-c2s/view-apply-function
Browse files Browse the repository at this point in the history
handle optional arguments in View Extension
  • Loading branch information
lossyrob authored Feb 16, 2021
2 parents 1174848 + 72808cb commit cbf22cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 22 additions & 10 deletions pystac/extensions/view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pystac
from pystac import Extensions
from pystac.item import Item
from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject)
from typing import Optional


class ViewItemExt(ItemExtension):
Expand Down Expand Up @@ -28,11 +30,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:
Expand All @@ -49,11 +51,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):
Expand Down
23 changes: 23 additions & 0 deletions tests/extensions/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cbf22cf

Please sign in to comment.