Coverage for gws-app/gws/gis/mpx/__init__.py: 0%

38 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1import time 

2 

3import gws 

4import gws.config 

5import gws.lib.net 

6 

7 

8 

9class ServiceException(Exception): 

10 pass 

11 

12 

13def _call(service, params): 

14 url = getattr(gws.config.root().app, 'mpxUrl') + '/' + service 

15 resp = gws.lib.net.http_request(url, params=params) 

16 if resp.content_type.startswith('image'): 

17 return resp.content 

18 raise ServiceException(resp.text) 

19 

20 

21_retry_count = 3 

22_retry_pause = 5 

23_request_number = 0 

24 

25 

26def _call_with_retry(service, params): 

27 global _request_number 

28 

29 _request_number += 1 

30 rc = 0 

31 

32 while True: 

33 try: 

34 return _call(service, params) 

35 except Exception as exc: 

36 gws.log.exception() 

37 err = repr(exc) 

38 

39 if rc >= _retry_count: 

40 gws.log.error(f'MAPPROXY_ERROR: {_request_number}/{rc} FAILED error={err} {params!r}') 

41 return 

42 

43 gws.log.error(f'MAPPROXY_ERROR: {_request_number}/{rc} retry error={err}') 

44 time.sleep(_retry_pause) 

45 rc += 1 

46 

47 

48def wms_request(layer_uid, bounds: gws.Bounds, width, height, forward=None): 

49 params = { 

50 'bbox': bounds.extent, 

51 'width': width, 

52 'height': height, 

53 'crs': bounds.crs.epsg, 

54 'service': 'WMS', 

55 'request': 'GetMap', 

56 'version': '1.3.0', 

57 'format': 'image/png', 

58 'transparent': 'true', 

59 'styles': '', 

60 'layers': layer_uid 

61 } 

62 if forward: 

63 params.update(forward) 

64 return _call_with_retry('wms', params) 

65 

66 

67def wmts_request(source_uid, x, y, z, tile_matrix, tile_size): 

68 params = { 

69 'tilecol': x, 

70 'tilerow': y, 

71 'tilematrix': z, 

72 'service': 'WMTS', 

73 'request': 'GetTile', 

74 'version': '1.0.0', 

75 'format': 'image/png', 

76 'tilematrixset': tile_matrix, 

77 'style': 'default', 

78 'layer': source_uid 

79 } 

80 return _call_with_retry('ows', params)