Skip to content

Commit 5b01881

Browse files
committed
Implement #370 for Projection extension
1 parent 1123517 commit 5b01881

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pystac/extensions/projection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ def transform(self, v: Optional[List[float]]) -> None:
237237
def get_schema_uri(cls) -> str:
238238
return SCHEMA_URI
239239

240-
@staticmethod
241-
def ext(obj: T) -> "ProjectionExtension[T]":
240+
@classmethod
241+
def ext(cls, obj: T) -> "ProjectionExtension[T]":
242242
"""Extends the given STAC Object with properties from the :stac-ext:`Projection
243243
Extension <projection>`.
244244
@@ -250,12 +250,14 @@ def ext(obj: T) -> "ProjectionExtension[T]":
250250
pystac.ExtensionTypeError : If an invalid object type is passed.
251251
"""
252252
if isinstance(obj, pystac.Item):
253+
cls.validate_has_extension(obj)
253254
return cast(ProjectionExtension[T], ItemProjectionExtension(obj))
254255
elif isinstance(obj, pystac.Asset):
256+
cls.validate_has_extension(obj)
255257
return cast(ProjectionExtension[T], AssetProjectionExtension(obj))
256258
else:
257259
raise pystac.ExtensionTypeError(
258-
f"File extension does not apply to type {type(obj)}"
260+
f"Projection extension does not apply to type {type(obj)}"
259261
)
260262

261263
@staticmethod

tests/extensions/test_projection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,24 @@ def test_transform(self) -> None:
406406
# Validate
407407
proj_item.validate()
408408

409+
def test_extension_not_implemented(self) -> None:
410+
# Should raise exception if Item does not include extension URI
411+
item = pystac.Item.from_file(self.example_uri)
412+
item.stac_extensions.remove(ProjectionExtension.get_schema_uri())
413+
414+
with self.assertRaises(pystac.ExtensionNotImplemented):
415+
_ = ProjectionExtension.ext(item)
416+
417+
# Should raise exception if owning Item does not include extension URI
418+
asset = item.assets["B8"]
419+
420+
with self.assertRaises(pystac.ExtensionNotImplemented):
421+
_ = ProjectionExtension.ext(asset)
422+
423+
# Should succeed if Asset has no owner
424+
ownerless_asset = pystac.Asset.from_dict(asset.to_dict())
425+
_ = ProjectionExtension.ext(ownerless_asset)
426+
409427

410428
class ProjectionSummariesTest(unittest.TestCase):
411429
def setUp(self) -> None:

0 commit comments

Comments
 (0)