44import json
55import os
66from copy import copy
7- from dataclasses import dataclass , field
87from enum import Enum
98from typing import Any , Callable , Dict , List , Literal , Optional , Type
109from urllib .parse import urlencode
1110
1211import jinja2
1312import rasterio
13+ from attrs import define , field
1414from cachetools import TTLCache , cached
1515from cachetools .keys import hashkey
1616from cogeo_mosaic .backends import BaseBackend
3838 DefaultDependency ,
3939 TileParams ,
4040)
41- from titiler .core .factory import BaseTilerFactory , img_endpoint_params
41+ from titiler .core .factory import TilerFactory , img_endpoint_params
4242from titiler .core .models .mapbox import TileJSON
4343from titiler .core .resources .enums import ImageType , MediaType , OptionalHeader
4444from titiler .core .resources .responses import GeoJSONResponse , XMLResponse
@@ -95,17 +95,17 @@ def get_dependency_params(*, dependency: Callable, query_params: Dict) -> Any:
9595 return
9696
9797
98- @dataclass
99- class MosaicTilerFactory (BaseTilerFactory ):
98+ @define ( kw_only = True )
99+ class MosaicTilerFactory (TilerFactory ):
100100 """Custom MosaicTiler for STACAPI Mosaic Backend."""
101101
102102 path_dependency : Callable [..., APIParams ] = STACApiParams
103103
104104 search_dependency : Callable [..., Dict ] = STACSearchParams
105105
106- # In this factory, `reader ` should be a Mosaic Backend
106+ # In this factory, `backend ` should be a Mosaic Backend
107107 # https://developmentseed.org/cogeo-mosaic/advanced/backends/
108- reader : Type [BaseBackend ] = STACAPIBackend
108+ backend : Type [BaseBackend ] = STACAPIBackend
109109
110110 # Because the endpoints should work with STAC Items,
111111 # the `layer_dependency` define which query parameters are mandatory/optional to `display` images
@@ -124,6 +124,8 @@ class MosaicTilerFactory(BaseTilerFactory):
124124
125125 templates : Jinja2Templates = DEFAULT_TEMPLATES
126126
127+ optional_headers : List [OptionalHeader ] = field (factory = list )
128+
127129 def get_base_url (self , request : Request ) -> str :
128130 """return endpoints base url."""
129131 base_url = str (request .base_url ).rstrip ("/" )
@@ -219,12 +221,12 @@ def tile(
219221
220222 tms = self .supported_tms .get (tileMatrixSetId )
221223 with rasterio .Env (** env ):
222- with self .reader (
224+ with self .backend (
223225 url = api_params ["api_url" ],
224226 headers = api_params .get ("headers" , {}),
225227 tms = tms ,
226- reader_options = {** reader_params },
227- ** backend_params ,
228+ reader_options = {** reader_params . as_dict () },
229+ ** backend_params . as_dict () ,
228230 ) as src_dst :
229231 if MOSAIC_STRICT_ZOOM and (
230232 z < src_dst .minzoom or z > src_dst .maxzoom
@@ -242,9 +244,9 @@ def tile(
242244 tilesize = scale * 256 ,
243245 pixel_selection = pixel_selection ,
244246 threads = MOSAIC_THREADS ,
245- ** tile_params ,
246- ** layer_params ,
247- ** dataset_params ,
247+ ** tile_params . as_dict () ,
248+ ** layer_params . as_dict () ,
249+ ** dataset_params . as_dict () ,
248250 )
249251
250252 if post_process :
@@ -260,7 +262,7 @@ def tile(
260262 image ,
261263 output_format = format ,
262264 colormap = colormap ,
263- ** render_params ,
265+ ** render_params . as_dict () ,
264266 )
265267
266268 headers : Dict [str , str ] = {}
@@ -724,15 +726,15 @@ def get_layer_from_collections( # noqa: C901
724726 return layers
725727
726728
727- @dataclass
728- class OGCWMTSFactory (BaseTilerFactory ):
729+ @define ( kw_only = True )
730+ class OGCWMTSFactory (TilerFactory ):
729731 """Create /wmts endpoint"""
730732
731733 path_dependency : Callable [..., APIParams ] = STACApiParams
732734
733735 # In this factory, `reader` should be a Mosaic Backend
734736 # https://developmentseed.org/cogeo-mosaic/advanced/backends/
735- reader : Type [BaseBackend ] = STACAPIBackend
737+ backend : Type [BaseBackend ] = STACAPIBackend
736738
737739 query_dependency : Callable [..., Any ] = STACQueryParams
738740
@@ -750,7 +752,7 @@ class OGCWMTSFactory(BaseTilerFactory):
750752 backend_dependency : Type [DefaultDependency ] = DefaultDependency
751753
752754 supported_format : List [str ] = field (
753- default_factory = lambda : [
755+ factory = lambda : [
754756 "image/png" ,
755757 "image/jpeg" ,
756758 "image/jpg" ,
@@ -760,7 +762,7 @@ class OGCWMTSFactory(BaseTilerFactory):
760762 ]
761763 )
762764
763- supported_version : List [str ] = field (default_factory = lambda : ["1.0.0" ])
765+ supported_version : List [str ] = field (factory = lambda : ["1.0.0" ])
764766
765767 templates : Jinja2Templates = DEFAULT_TEMPLATES
766768
@@ -798,7 +800,7 @@ def get_tile( # noqa: C901
798800 y = int (req ["tilerow" ])
799801
800802 tms = self .supported_tms .get (tms_id )
801- with self .reader (
803+ with self .backend (
802804 url = stac_url ,
803805 headers = headers ,
804806 tms = tms ,
@@ -868,9 +870,9 @@ def get_tile( # noqa: C901
868870 search_query = search_query ,
869871 pixel_selection = pixel_selection ,
870872 threads = MOSAIC_THREADS ,
871- ** tile_params ,
872- ** layer_params ,
873- ** dataset_params ,
873+ ** tile_params . as_dict () ,
874+ ** layer_params . as_dict () ,
875+ ** dataset_params . as_dict () ,
874876 )
875877
876878 if post_process := get_dependency_params (
@@ -1387,12 +1389,12 @@ def WMTS_getTile(
13871389
13881390 tms = self .supported_tms .get (tileMatrixSetId )
13891391 with rasterio .Env (** env ):
1390- with self .reader (
1392+ with self .backend (
13911393 url = api_params ["api_url" ],
13921394 headers = api_params .get ("headers" , {}),
13931395 tms = tms ,
1394- reader_options = {** reader_params },
1395- ** backend_params ,
1396+ reader_options = {** reader_params . as_dict () },
1397+ ** backend_params . as_dict () ,
13961398 ) as src_dst :
13971399 if MOSAIC_STRICT_ZOOM and (
13981400 z < src_dst .minzoom or z > src_dst .maxzoom
@@ -1410,9 +1412,9 @@ def WMTS_getTile(
14101412 tilesize = 256 ,
14111413 pixel_selection = pixel_selection ,
14121414 threads = MOSAIC_THREADS ,
1413- ** tile_params ,
1414- ** layer_params ,
1415- ** dataset_params ,
1415+ ** tile_params . as_dict () ,
1416+ ** layer_params . as_dict () ,
1417+ ** dataset_params . as_dict () ,
14161418 )
14171419
14181420 if post_process :
@@ -1428,7 +1430,7 @@ def WMTS_getTile(
14281430 image ,
14291431 output_format = format ,
14301432 colormap = colormap ,
1431- ** render_params ,
1433+ ** render_params . as_dict () ,
14321434 )
14331435
14341436 return Response (content , media_type = media_type )
0 commit comments