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

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 grid: Optional[gws.base.layer.GridConfig] 

17 """source grid""" 

18 

19 

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

21 protocol = gws.OwsProtocol.WMTS 

22 

23 tileMatrixSets: list[gws.TileMatrixSet] 

24 grids: list[gws.TileGrid] 

25 

26 def configure(self): 

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

28 

29 self.metadata = cc.metadata 

30 self.sourceLayers = cc.sourceLayers 

31 self.version = cc.version 

32 self.tileMatrixSets = cc.tileMatrixSets 

33 

34 self.configure_operations(cc.operations) 

35 

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 ) 

43 

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 

47 

48 if resource_url: 

49 return ( 

50 resource_url 

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

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

53 

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 } 

66 

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) 

70 

71 # {} should not be encoded 

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