Coverage for gws-app/gws/plugin/ows_client/wmts/provider.py: 0%

34 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""WMTS provider""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.base.layer 

7import gws.base.ows.client 

8import gws.config.util 

9import gws.lib.uom 

10import gws.lib.net 

11 

12from . import caps 

13 

14 

15class Config(gws.base.ows.client.provider.Config): 

16 """WMTS provider configuration.""" 

17 

18 grid: Optional[gws.base.layer.GridConfig] 

19 """Source grid.""" 

20 

21 

22class Object(gws.base.ows.client.provider.Object): 

23 protocol = gws.OwsProtocol.WMTS 

24 

25 tileMatrixSets: list[gws.TileMatrixSet] 

26 grids: list[gws.TileGrid] 

27 

28 def configure(self): 

29 cc = caps.parse(self.get_capabilities()) 

30 

31 self.metadata = cc.metadata 

32 self.sourceLayers = cc.sourceLayers 

33 self.version = cc.version 

34 self.tileMatrixSets = cc.tileMatrixSets 

35 

36 self.configure_operations(cc.operations) 

37 

38 def grid_for_tms(self, tms: gws.TileMatrixSet) -> gws.TileGrid: 

39 return gws.TileGrid( 

40 bounds=gws.Bounds(crs=tms.crs, extent=tms.matrices[0].extent), 

41 origin=gws.Origin.nw, 

42 resolutions=sorted([gws.lib.uom.scale_to_res(m.scale) for m in tms.matrices], reverse=True), 

43 tileSize=tms.matrices[0].tileWidth, 

44 ) 

45 

46 def tile_url_template(self, sl: gws.SourceLayer, tms: gws.TileMatrixSet, style: gws.SourceStyle) -> str: 

47 ru = sl.resourceUrls 

48 resource_url = ru.get('tile') if ru else None 

49 

50 if resource_url: 

51 return ( 

52 resource_url 

53 .replace('{TileMatrixSet}', tms.uid) 

54 .replace('{Style}', style.name)) 

55 

56 params = { 

57 'SERVICE': gws.OwsProtocol.WMTS, 

58 'REQUEST': gws.OwsVerb.GetTile, 

59 'VERSION': self.version, 

60 'LAYER': sl.name, 

61 'FORMAT': sl.imageFormat or 'image/jpeg', 

62 'TILEMATRIXSET': tms.uid, 

63 'STYLE': style.name, 

64 'TILEMATRIX': '{TileMatrix}', 

65 'TILECOL': '{TileCol}', 

66 'TILEROW': '{TileRow}', 

67 } 

68 

69 op = self.get_operation(gws.OwsVerb.GetTile) 

70 args = self.prepare_operation(op, params=params) 

71 url = gws.lib.net.add_params(args.url, args.params) 

72 

73 # {} should not be encoded 

74 return url.replace('%7B', '{').replace('%7D', '}')