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

Add variables argument to the DatacubeExtension apply method #782

Merged
merged 4 commits into from
Apr 4, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Fixed

- "How to create STAC catalogs" tutorial ([#775](https://github.com/stac-utils/pystac/pull/775))
- Add a `variables` argument, to accompany `dimensions`, for the `apply` method of stac objects extended with datacube ([#782](https://github.com/stac-utils/pystac/pull/782))

## [v1.4.0]

Expand Down
15 changes: 14 additions & 1 deletion pystac/extensions/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ class VariableType(StringEnum):


class Variable:
"""Object representing a variable in the datacube. The dimensions field lists
zero or more :stac-ext:`Datacube Dimension Object <datacube#dimension-object>`
instances. See the :stac-ext:`Datacube Variable Object
<datacube#variable-object>` docs for details.
"""

properties: Dict[str, Any]

def __init__(self, properties: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -522,15 +528,22 @@ class DatacubeExtension(
>>> dc_ext = DatacubeExtension.ext(item)
"""

def apply(self, dimensions: Dict[str, Dimension]) -> None:
def apply(
self,
dimensions: Dict[str, Dimension],
variables: Optional[Dict[str, Variable]] = None,
) -> None:
"""Applies label extension properties to the extended
:class:`~pystac.Collection`, :class:`~pystac.Item` or :class:`~pystac.Asset`.

Args:
dimensions : Dictionary mapping dimension name to a :class:`Dimension`
object.
variables : Dictionary mapping variable name to a :class:`Variable`
object.
"""
self.dimensions = dimensions
self.variables = variables

@property
def dimensions(self) -> Dict[str, Dimension]:
Expand Down
13 changes: 13 additions & 0 deletions tests/extensions/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ def test_set_variables(self) -> None:
self.assertEqual(
item.properties["cube:variables"], {"temp": new_variable.to_dict()}
)

def test_apply_variables(self) -> None:
item = pystac.Item.from_file(self.example_uri)
cube = DatacubeExtension.ext(item)
variables = cube.variables
assert variables is not None
key, value = variables.popitem()
target = value.to_dict()
cube.variables = None
cube.apply(dimensions={}, variables={key: value})
variables = cube.variables
assert variables is not None
self.assertEqual(target, cube.variables[key].to_dict())