Skip to content

Commit 8add2ae

Browse files
authored
Add .ext to stac objects (#1717)
1 parent 75c771e commit 8add2ae

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/pystac/asset.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .writer import Writer
1919

2020
if TYPE_CHECKING:
21+
from .extensions.ext import AssetExt
2122
from .stac_object import STACObject
2223

2324

@@ -132,6 +133,18 @@ def to_dict(self) -> dict[str, Any]:
132133
data = super().to_dict()
133134
return {"href": self.href, **data}
134135

136+
@property
137+
def ext(self) -> AssetExt:
138+
"""Accessor for extension classes on this asset
139+
140+
Example::
141+
142+
asset.ext.proj.code = "EPSG:4326"
143+
"""
144+
from pystac.extensions.ext import AssetExt
145+
146+
return AssetExt(stac_object=self)
147+
135148

136149
class Assets(Protocol):
137150
assets: dict[str, Asset]

src/pystac/catalog.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import copy
44
import warnings
55
from enum import StrEnum
6-
from typing import Any, ClassVar, override
6+
from typing import TYPE_CHECKING, Any, ClassVar, override
77

88
from typing_extensions import deprecated
99

@@ -13,6 +13,9 @@
1313
from .link import Link
1414
from .rel_type import RelType
1515

16+
if TYPE_CHECKING:
17+
from .extensions.ext import CatalogExt
18+
1619

1720
class Catalog(Container):
1821
type: ClassVar[STAC_OBJECT_TYPE] = "Catalog"
@@ -83,6 +86,18 @@ def to_dict(
8386
data["description"] = self.description
8487
return data
8588

89+
@property
90+
def ext(self) -> CatalogExt:
91+
"""Accessor for extension classes on this catalog
92+
93+
Example::
94+
95+
print(collection.ext.version)
96+
"""
97+
from pystac.extensions.ext import CatalogExt
98+
99+
return CatalogExt(stac_object=self)
100+
86101

87102
@deprecated("CatalogType is deprecated")
88103
class CatalogType(StrEnum):

src/pystac/collection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .utils import to_datetime_str
2727

2828
if TYPE_CHECKING:
29+
from .extensions.ext import CollectionExt
2930
from .item import Item
3031
from .item_collection import ItemCollection
3132

@@ -218,6 +219,18 @@ def update_extent_from_items(self) -> None:
218219
"""
219220
self.extent = Extent.from_items(self.get_items(recursive=True))
220221

222+
@property
223+
def ext(self) -> CollectionExt:
224+
"""Accessor for extension classes on this collection
225+
226+
Example::
227+
228+
print(collection.ext.xarray)
229+
"""
230+
from pystac.extensions.ext import CollectionExt
231+
232+
return CollectionExt(stac_object=self)
233+
221234

222235
class Extent:
223236
def __init__(

src/pystac/item.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
if TYPE_CHECKING:
3131
from .collection import Collection
32+
from .extensions.ext import ItemExt
3233

3334

3435
class Item(STACObject, Assets):
@@ -241,6 +242,18 @@ def to_dict(
241242
def __geo_interface__(self) -> dict[str, Any]:
242243
return self.to_dict(include_self_link=False)
243244

245+
@property
246+
def ext(self) -> ItemExt:
247+
"""Accessor for extension classes on this item
248+
249+
Example::
250+
251+
item.ext.proj.code = "EPSG:4326"
252+
"""
253+
from pystac.extensions.ext import ItemExt
254+
255+
return ItemExt(stac_object=self)
256+
244257

245258
@final
246259
class Properties(

src/pystac/link.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
if TYPE_CHECKING:
1818
from . import Catalog, Collection, Item
19+
from .extensions.ext import LinkExt
1920
from .stac_object import STACObject
2021

2122
HIERARCHICAL_LINKS = [
@@ -318,3 +319,15 @@ def canonical(
318319
title=title,
319320
media_type=MediaType.JSON,
320321
)
322+
323+
@property
324+
def ext(self) -> LinkExt:
325+
"""Accessor for extension classes on this link
326+
327+
Example::
328+
329+
link.ext.file.size = 8675309
330+
"""
331+
from pystac.extensions.ext import LinkExt
332+
333+
return LinkExt(stac_object=self)

0 commit comments

Comments
 (0)