2
2
3
3
from __future__ import annotations
4
4
5
- from typing import Any , Dict , Iterable , List , Optional , Set , Union
5
+ from typing import (
6
+ Any ,
7
+ Dict ,
8
+ Generic ,
9
+ Iterable ,
10
+ List ,
11
+ Optional ,
12
+ Set ,
13
+ TypeVar ,
14
+ Union ,
15
+ cast ,
16
+ )
6
17
7
18
import pystac
8
19
from pystac .extensions .base import (
11
22
SummariesExtension ,
12
23
)
13
24
from pystac .extensions .hooks import ExtensionHooks
25
+ import pystac .extensions .item_assets as item_assets
14
26
from pystac .utils import StringEnum , get_opt , get_required , map_opt
15
27
28
+ T = TypeVar ("T" , pystac .Asset , item_assets .AssetDefinition )
29
+
16
30
SCHEMA_URI = "https://stac-extensions.github.io/raster/v1.1.0/schema.json"
17
31
SCHEMA_URIS = [
18
32
"https://stac-extensions.github.io/raster/v1.0.0/schema.json" ,
@@ -644,38 +658,27 @@ def to_dict(self) -> Dict[str, Any]:
644
658
645
659
646
660
class RasterExtension (
647
- PropertiesExtension , ExtensionManagementMixin [Union [pystac .Item , pystac .Collection ]]
661
+ Generic [T ],
662
+ PropertiesExtension ,
663
+ ExtensionManagementMixin [Union [pystac .Item , pystac .Collection ]],
648
664
):
649
665
"""An abstract class that can be used to extend the properties of an
650
- :class:`~pystac.Item` or :class:`~pystac.Asset` with properties from
666
+ :class:`~pystac.Item`, :class:`~pystac.Asset`, or
667
+ :class:`~pystac.extension.item_assets.AssetDefinition` with properties from
651
668
the :stac-ext:`Raster Extension <raster>`. This class is generic over
652
669
the type of STAC Object to be extended (e.g. :class:`~pystac.Item`,
653
670
:class:`~pystac.Asset`).
654
671
655
672
This class will generally not be used directly. Instead, use the concrete
656
673
implementation associated with the STAC Object you want to extend (e.g.
657
- :class:`~ItemRasterExtension` to extend an :class:`~pystac.Item`).
674
+ :class:`~ItemRasterExtension` to extend an :class:`~pystac.Item`). You may
675
+ prefer to use the `ext` class method of this class to construct the correct
676
+ instance type for you.
658
677
"""
659
678
660
- asset_href : str
661
- """The ``href`` value of the :class:`~pystac.Asset` being extended."""
662
-
663
679
properties : Dict [str , Any ]
664
680
"""The :class:`~pystac.Asset` fields, including extension properties."""
665
681
666
- additional_read_properties : Optional [Iterable [Dict [str , Any ]]] = None
667
- """If present, this will be a list containing 1 dictionary representing the
668
- properties of the owning :class:`~pystac.Item`."""
669
-
670
- def __init__ (self , asset : pystac .Asset ):
671
- self .asset_href = asset .href
672
- self .properties = asset .extra_fields
673
- if asset .owner and isinstance (asset .owner , pystac .Item ):
674
- self .additional_read_properties = [asset .owner .properties ]
675
-
676
- def __repr__ (self ) -> str :
677
- return "<AssetRasterExtension Asset href={}>" .format (self .asset_href )
678
-
679
682
def apply (self , bands : List [RasterBand ]) -> None :
680
683
"""Applies raster extension properties to the extended :class:`pystac.Item` or
681
684
:class:`pystac.Asset`.
@@ -715,7 +718,7 @@ def get_schema_uris(cls) -> List[str]:
715
718
return SCHEMA_URIS
716
719
717
720
@classmethod
718
- def ext (cls , obj : pystac . Asset , add_if_missing : bool = False ) -> RasterExtension :
721
+ def ext (cls , obj : T , add_if_missing : bool = False ) -> RasterExtension [ T ] :
719
722
"""Extends the given STAC Object with properties from the :stac-ext:`Raster
720
723
Extension <raster>`.
721
724
@@ -727,7 +730,12 @@ def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> RasterExtension
727
730
"""
728
731
if isinstance (obj , pystac .Asset ):
729
732
cls .validate_owner_has_extension (obj , add_if_missing )
730
- return cls (obj )
733
+ return cast (RasterExtension [T ], AssetRasterExtension (obj ))
734
+ elif isinstance (obj , item_assets .AssetDefinition ):
735
+ cls .validate_has_extension (
736
+ cast (Union [pystac .Item , pystac .Collection ], obj .owner ), add_if_missing
737
+ )
738
+ return cast (RasterExtension [T ], ItemAssetsRasterExtension (obj ))
731
739
else :
732
740
raise pystac .ExtensionTypeError (
733
741
f"Raster extension does not apply to type '{ type (obj ).__name__ } '"
@@ -741,6 +749,46 @@ def summaries(
741
749
return SummariesRasterExtension (obj )
742
750
743
751
752
+ class AssetRasterExtension (RasterExtension [pystac .Asset ]):
753
+ asset_href : str
754
+ """The ``href`` value of the :class:`~pystac.Asset` being extended."""
755
+
756
+ properties : Dict [str , Any ]
757
+ """The :class:`~pystac.Asset` fields, including extension properties."""
758
+
759
+ additional_read_properties : Optional [Iterable [Dict [str , Any ]]] = None
760
+ """If present, this will be a list containing 1 dictionary representing the
761
+ properties of the owning :class:`~pystac.Item`."""
762
+
763
+ def __init__ (self , asset : pystac .Asset ):
764
+ self .asset_href = asset .href
765
+ self .properties = asset .extra_fields
766
+ if asset .owner and isinstance (asset .owner , pystac .Item ):
767
+ self .additional_read_properties = [asset .owner .properties ]
768
+
769
+ def __repr__ (self ) -> str :
770
+ return "<AssetRasterExtension Asset href={}>" .format (self .asset_href )
771
+
772
+
773
+ class ItemAssetsRasterExtension (RasterExtension [item_assets .AssetDefinition ]):
774
+ asset_definition : item_assets .AssetDefinition
775
+ """A reference to the :class:`~pystac.extensions.item_assets.AssetDefinition`
776
+ being extended."""
777
+
778
+ properties : Dict [str , Any ]
779
+ """The :class:`~pystac.extensions.item_assets.AssetDefinition` fields, including
780
+ extension properties."""
781
+
782
+ def __init__ (self , item_asset : item_assets .AssetDefinition ):
783
+ self .properties = item_asset .properties
784
+ self .asset_definition = item_asset
785
+
786
+ def __repr__ (self ) -> str :
787
+ return "<ItemAssetsRasterExtension AssetDefinition={}>" .format (
788
+ self .asset_definition
789
+ )
790
+
791
+
744
792
class SummariesRasterExtension (SummariesExtension ):
745
793
"""A concrete implementation of :class:`~SummariesExtension` that extends
746
794
the ``summaries`` field of a :class:`~pystac.Collection` to include properties
0 commit comments