11"""Custom STAC reader."""
22
3- from typing import Any , Dict , Optional , Set , Type
3+ from typing import Any , Dict , Optional , Sequence , Set , Type
44
55import attr
66import pystac
77import rasterio
88from morecantile import TileMatrixSet
9- from rasterio .crs import CRS
10- from rio_tiler .constants import WEB_MERCATOR_TMS , WGS84_CRS
11- from rio_tiler .errors import InvalidAssetName
9+ from rio_tiler .constants import WEB_MERCATOR_TMS
1210from rio_tiler .io import BaseReader , Reader , stac
13- from rio_tiler .types import AssetInfo
1411
1512from titiler .stacapi .settings import STACSettings
1613
@@ -25,77 +22,30 @@ class STACReader(stac.STACReader):
2522
2623 """
2724
28- tms : TileMatrixSet = attr .ib (default = WEB_MERCATOR_TMS )
29- minzoom : int = attr .ib ()
30- maxzoom : int = attr .ib ()
25+ input : pystac .Item = attr .ib ()
3126
32- geographic_crs : CRS = attr .ib (default = WGS84_CRS )
27+ tms : TileMatrixSet = attr .ib (default = WEB_MERCATOR_TMS )
28+ minzoom : int = attr .ib (default = None )
29+ maxzoom : int = attr .ib (default = None )
3330
3431 include_assets : Optional [Set [str ]] = attr .ib (default = None )
3532 exclude_assets : Optional [Set [str ]] = attr .ib (default = None )
3633
3734 include_asset_types : Set [str ] = attr .ib (default = stac .DEFAULT_VALID_TYPE )
3835 exclude_asset_types : Optional [Set [str ]] = attr .ib (default = None )
3936
37+ assets : Sequence [str ] = attr .ib (init = False )
38+ default_assets : Optional [Sequence [str ]] = attr .ib (default = None )
39+
4040 reader : Type [BaseReader ] = attr .ib (default = Reader )
4141 reader_options : Dict = attr .ib (factory = dict )
4242
43- fetch_options : Dict = attr .ib (factory = dict )
44-
4543 ctx : Any = attr .ib (default = rasterio .Env )
4644
4745 item : pystac .Item = attr .ib (init = False )
46+ fetch_options : Dict = attr .ib (factory = dict )
4847
4948 def __attrs_post_init__ (self ):
5049 """Fetch STAC Item and get list of valid assets."""
5150 self .item = self .input
5251 super ().__attrs_post_init__ ()
53-
54- @minzoom .default
55- def _minzoom (self ):
56- return self .tms .minzoom
57-
58- @maxzoom .default
59- def _maxzoom (self ):
60- return self .tms .maxzoom
61-
62- def _get_asset_info (self , asset : str ) -> AssetInfo :
63- """Validate asset names and return asset's url.
64-
65- Args:
66- asset (str): STAC asset name.
67-
68- Returns:
69- str: STAC asset href.
70-
71- """
72- if asset not in self .assets :
73- raise InvalidAssetName (
74- f"'{ asset } ' is not valid, should be one of { self .assets } "
75- )
76-
77- asset_info = self .item .assets [asset ]
78- extras = asset_info .extra_fields
79-
80- url = asset_info .get_absolute_href () or asset_info .href
81- if alternate := stac_config .alternate_url :
82- url = asset_info .to_dict ()["alternate" ][alternate ]["href" ]
83-
84- info = AssetInfo (
85- url = url ,
86- metadata = extras ,
87- )
88-
89- if head := extras .get ("file:header_size" ):
90- info ["env" ] = {"GDAL_INGESTED_BYTES_AT_OPEN" : head }
91-
92- if bands := extras .get ("raster:bands" ):
93- stats = [
94- (b ["statistics" ]["minimum" ], b ["statistics" ]["maximum" ])
95- for b in bands
96- if {"minimum" , "maximum" }.issubset (b .get ("statistics" , {}))
97- ]
98- if len (stats ) == len (bands ):
99- info ["dataset_statistics" ] = stats
100-
101- return info
0 commit comments