Skip to content

Commit

Permalink
Fix/576 AssetDefinition missing create, apply (#768)
Browse files Browse the repository at this point in the history
* Add missing create, apply to AssetDefinition.

* Fix docstring references to old commonmark version.

* Add missing property in unit test for EO Band.

* Changelog

* Changelog.
  • Loading branch information
guidorice authored Mar 30, 2022
1 parent a19365b commit c44c79c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Enum MediaType entry for PDF documents ([#758](https://github.com/stac-utils/pystac/pull/758))
- Updated Link to obtain stac_io from owner root ([#762](https://github.com/stac-utils/pystac/pull/762))
- Updated AssetDefinition to have create, apply methods ([#768](https://github.com/stac-utils/pystac/pull/768))

### Removed

Expand Down
2 changes: 1 addition & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Catalog(STACObject):
Args:
id : Identifier for the catalog. Must be unique within the STAC.
description : Detailed multi-line description to fully explain the catalog.
`CommonMark 0.28 syntax <https://commonmark.org/>`_ MAY be used for rich
`CommonMark 0.29 syntax <https://commonmark.org/>`_ MAY be used for rich
text representation.
title : Optional short descriptive one-line title for the catalog.
stac_extensions : Optional list of extensions the Catalog implements.
Expand Down
2 changes: 1 addition & 1 deletion pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class Collection(Catalog):
Args:
id : Identifier for the collection. Must be unique within the STAC.
description : Detailed multi-line description to fully explain the
collection. `CommonMark 0.28 syntax <https://commonmark.org/>`_ MAY
collection. `CommonMark 0.29 syntax <https://commonmark.org/>`_ MAY
be used for rich text representation.
extent : Spatial and temporal extents that describe the bounds of
all items contained within this Collection.
Expand Down
62 changes: 60 additions & 2 deletions pystac/extensions/item_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,64 @@ def __eq__(self, o: object) -> bool:
return NotImplemented
return self.to_dict() == o.to_dict()

@classmethod
def create(
cls,
title: Optional[str],
description: Optional[str],
media_type: Optional[str],
roles: Optional[List[str]],
) -> "AssetDefinition":
"""
Creates a new asset definition.
Args:
title : Displayed title for clients and users.
description : Description of the Asset providing additional details,
such as how it was processed or created.
`CommonMark 0.29 <http://commonmark.org/>`__ syntax MAY be used
for rich text representation.
media_type : `media type
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/catalog-spec/catalog-spec.md#media-types>`__
of the asset.
roles : `semantic roles
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/item-spec/item-spec.md#asset-role-types>`__
of the asset, similar to the use of rel in links.
"""
asset_defn = cls({})
asset_defn.apply(
title=title, description=description, media_type=media_type, roles=roles
)
return asset_defn

def apply(
self,
title: Optional[str],
description: Optional[str],
media_type: Optional[str],
roles: Optional[List[str]],
) -> None:
"""
Sets the properties for this asset definition.
Args:
title : Displayed title for clients and users.
description : Description of the Asset providing additional details,
such as how it was processed or created.
`CommonMark 0.29 <http://commonmark.org/>`__ syntax MAY be used
for rich text representation.
media_type : `media type
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/catalog-spec/catalog-spec.md#media-types>`__
of the asset.
roles : `semantic roles
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/item-spec/item-spec.md#asset-role-types>`__
of the asset, similar to the use of rel in links.
"""
self.title = title
self.description = description
self.media_type = media_type
self.roles = roles

@property
def title(self) -> Optional[str]:
"""Gets or sets the displayed title for clients and users."""
Expand Down Expand Up @@ -65,7 +123,7 @@ def description(self, v: Optional[str]) -> None:
@property
def media_type(self) -> Optional[str]:
"""Gets or sets the `media type
<https://github.com/radiantearth/stac-spec/tree/v1.0.0-rc.1/catalog-spec/catalog-spec.md#media-types>`__
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/catalog-spec/catalog-spec.md#media-types>`__
of the asset."""
return self.properties.get(ASSET_TYPE_PROP)

Expand All @@ -79,7 +137,7 @@ def media_type(self, v: Optional[str]) -> None:
@property
def roles(self) -> Optional[List[str]]:
"""Gets or sets the `semantic roles
<https://github.com/radiantearth/stac-spec/tree/v1.0.0-rc.1/item-spec/item-spec.md#asset-role-types>`__
<https://github.com/radiantearth/stac-spec/tree/v1.0.0/item-spec/item-spec.md#asset-role-types>`__
of the asset, similar to the use of rel in links."""
return self.properties.get(ASSET_ROLES_PROP)

Expand Down
3 changes: 2 additions & 1 deletion tests/extensions/test_eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ def test_create(self) -> None:
description=Band.band_description("red"),
center_wavelength=0.65,
full_width_half_max=0.1,
solar_illumination=42.0,
)

self.assertEqual(band.name, "B01")
self.assertEqual(band.common_name, "red")
self.assertEqual(band.description, "Common name: red, Range: 0.6 to 0.7")
self.assertEqual(band.center_wavelength, 0.65)
self.assertEqual(band.full_width_half_max, 0.1)

self.assertEqual(band.solar_illumination, 42.0)
self.assertEqual(band.__repr__(), "<Band name=B01>")

def test_band_description_unknown_band(self) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tests/extensions/test_item_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def setUp(self) -> None:
TestCases.get_path("data-files/item-assets/example-landsat8.json")
)

def test_create(self) -> None:
title = "Coastal Band (B1)"
description = "Coastal Band Top Of the Atmosphere"
media_type = "image/tiff; application=geotiff"
roles = ["data"]
asset_defn = AssetDefinition.create(
title=title, description=description, media_type=media_type, roles=roles
)
self.assertEqual(asset_defn.title, title)
self.assertEqual(asset_defn.description, description)
self.assertEqual(asset_defn.media_type, media_type)
self.assertEqual(asset_defn.roles, roles)

def test_title(self) -> None:
asset_defn = AssetDefinition({})
title = "Very Important Asset"
Expand Down

0 comments on commit c44c79c

Please sign in to comment.