Coverage for gws-app/gws/plugin/template/map/__init__.py: 0%

38 statements  

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

1"""Map-only template""" 

2 

3import re 

4 

5import gws 

6import gws.base.template 

7import gws.lib.htmlx 

8import gws.lib.mime 

9import gws.gis.render 

10 

11gws.ext.new.template('map') 

12 

13 

14class Config(gws.base.template.Config): 

15 pass 

16 

17 

18class Props(gws.base.template.Props): 

19 pass 

20 

21 

22class Object(gws.base.template.Object): 

23 

24 def render(self, tri): 

25 notify = tri.notify or (lambda *args: None) 

26 mp = tri.maps[0] 

27 

28 mri = gws.MapRenderInput( 

29 backgroundColor=mp.backgroundColor, 

30 bbox=mp.bbox, 

31 center=mp.center, 

32 crs=tri.crs, 

33 dpi=tri.dpi, 

34 mapSize=self.pageSize, 

35 notify=notify, 

36 planes=mp.planes, 

37 project=tri.project, 

38 rotation=mp.rotation, 

39 scale=mp.scale, 

40 user=tri.user, 

41 ) 

42 

43 notify('begin_print') 

44 notify('begin_page') 

45 notify('begin_map') 

46 

47 mro = gws.gis.render.render_map(mri) 

48 html = gws.gis.render.output_to_html_string(mro, wrap='fixed') 

49 

50 notify('end_map') 

51 notify('end_page') 

52 notify('finalize_print') 

53 

54 if not tri.mimeOut or tri.mimeOut == gws.lib.mime.HTML: 

55 notify('end_print') 

56 return gws.ContentResponse(mime=gws.lib.mime.HTML, content=html) 

57 

58 if tri.mimeOut == gws.lib.mime.PDF: 

59 res_path = gws.u.printtemp('map.pdf') 

60 gws.lib.htmlx.render_to_pdf( 

61 html, 

62 out_path=res_path, 

63 page_size=self.pageSize, 

64 ) 

65 notify('end_print') 

66 return gws.ContentResponse(contentPath=res_path) 

67 

68 if tri.mimeOut == gws.lib.mime.PNG: 

69 res_path = gws.u.printtemp('map.png') 

70 gws.lib.htmlx.render_to_png( 

71 html, 

72 out_path=res_path, 

73 page_size=self.pageSize, 

74 ) 

75 notify('end_print') 

76 return gws.ContentResponse(contentPath=res_path) 

77 

78 raise gws.Error(f'invalid output mime: {tri.mimeOut!r}')