Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle optional arguments in View Extension #259

Merged
merged 7 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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