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

38 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Map-only templates. 

2 

3The ``map`` template renders only the first map of the render input, without 

4any page layout. The map fills the template ``pageSize``. The output is 

5HTML, PDF or PNG, depending on the ``mimeOut`` of the render input; HTML is 

6the default. 

7 

8Example:: 

9 

10 printers+ { 

11 template { 

12 type "map" 

13 title "Map only" 

14 mimeTypes [ "application/pdf" ] 

15 } 

16 } 

17""" 

18 

19import re 

20 

21import gws 

22import gws.base.template 

23import gws.lib.htmlx 

24import gws.lib.mime 

25import gws.gis.render 

26 

27 

28@gws.ext.config.template('map') 

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

30 """Template that renders only the map.""" 

31 pass 

32 

33 

34@gws.ext.props.template('map') 

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

36 pass 

37 

38 

39@gws.ext.object.template('map') 

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

41 """Template that renders only the map, as HTML, PDF or PNG.""" 

42 

43 def render(self, tri): 

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

45 mp = tri.maps[0] 

46 

47 mri = gws.MapRenderInput( 

48 backgroundColor=mp.backgroundColor, 

49 bbox=mp.bbox, 

50 center=mp.center, 

51 targetCrs=tri.crs, 

52 dpi=tri.dpi, 

53 mapSize=self.pageSize, 

54 notify=notify, 

55 planes=mp.planes, 

56 project=tri.project, 

57 rotation=mp.rotation, 

58 scale=mp.scale, 

59 user=tri.user, 

60 ) 

61 

62 notify('begin_print') 

63 notify('begin_map') 

64 

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

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

67 

68 notify('end_map') 

69 notify('finalize_print') 

70 

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

72 notify('end_print') 

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

74 

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

76 res_path = gws.u.ephemeral_path('map.pdf') 

77 gws.lib.htmlx.render_to_pdf( 

78 html, 

79 out_path=res_path, 

80 page_size=self.pageSize, 

81 ) 

82 notify('end_print') 

83 return gws.ContentResponse(contentPath=res_path) 

84 

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

86 res_path = gws.u.ephemeral_path('map.png') 

87 gws.lib.htmlx.render_to_png( 

88 html, 

89 out_path=res_path, 

90 page_size=self.pageSize, 

91 ) 

92 notify('end_print') 

93 return gws.ContentResponse(contentPath=res_path) 

94 

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