Coverage for gws-app/gws/plugin/ows_client/wmts/provider.py: 0%
34 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""WMTS provider"""
3from typing import Optional, cast
5import gws
6import gws.base.layer
7import gws.base.ows.client
8import gws.config.util
9import gws.lib.uom
10import gws.lib.net
12from . import caps
15class Config(gws.base.ows.client.provider.Config):
16 grid: Optional[gws.base.layer.GridConfig]
17 """source grid"""
20class Object(gws.base.ows.client.provider.Object):
21 protocol = gws.OwsProtocol.WMTS
23 tileMatrixSets: list[gws.TileMatrixSet]
24 grids: list[gws.TileGrid]
26 def configure(self):
27 cc = caps.parse(self.get_capabilities())
29 self.metadata = cc.metadata
30 self.sourceLayers = cc.sourceLayers
31 self.version = cc.version
32 self.tileMatrixSets = cc.tileMatrixSets
34 self.configure_operations(cc.operations)
36 def grid_for_tms(self, tms: gws.TileMatrixSet) -> gws.TileGrid:
37 return gws.TileGrid(
38 bounds=gws.Bounds(crs=tms.crs, extent=tms.matrices[0].extent),
39 origin=gws.Origin.nw,
40 resolutions=sorted([gws.lib.uom.scale_to_res(m.scale) for m in tms.matrices], reverse=True),
41 tileSize=tms.matrices[0].tileWidth,
42 )
44 def tile_url_template(self, sl: gws.SourceLayer, tms: gws.TileMatrixSet, style: gws.SourceStyle) -> str:
45 ru = sl.resourceUrls
46 resource_url = ru.get('tile') if ru else None
48 if resource_url:
49 return (
50 resource_url
51 .replace('{TileMatrixSet}', tms.uid)
52 .replace('{Style}', style.name))
54 params = {
55 'SERVICE': gws.OwsProtocol.WMTS,
56 'REQUEST': gws.OwsVerb.GetTile,
57 'VERSION': self.version,
58 'LAYER': sl.name,
59 'FORMAT': sl.imageFormat or 'image/jpeg',
60 'TILEMATRIXSET': tms.uid,
61 'STYLE': style.name,
62 'TILEMATRIX': '{TileMatrix}',
63 'TILECOL': '{TileCol}',
64 'TILEROW': '{TileRow}',
65 }
67 op = self.get_operation(gws.OwsVerb.GetTile)
68 args = self.prepare_operation(op, params=params)
69 url = gws.lib.net.add_params(args.url, args.params)
71 # {} should not be encoded
72 return url.replace('%7B', '{').replace('%7D', '}')