Coverage for gws-app/gws/base/legend/core.py: 0%

54 statements  

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

1from typing import Optional 

2 

3import gws 

4import gws.lib.image 

5import gws.lib.mime 

6 

7 

8class Props(gws.Props): 

9 type: str 

10 

11 

12class Config(gws.ConfigWithAccess): 

13 """Layer legend confuguration.""" 

14 

15 cacheMaxAge: gws.Duration = '1d' 

16 """max cache age for remote legends""" 

17 options: Optional[dict] 

18 """provider-dependent legend options""" 

19 

20 

21class Object(gws.Legend): 

22 """Generic legend object.""" 

23 

24 cacheMaxAge: int 

25 options: dict 

26 

27 def configure(self): 

28 self.options = self.cfg('options', default={}) 

29 self.cacheMaxAge = self.cfg('cacheMaxAge', default=3600 * 24) 

30 

31 

32def output_to_bytes(lro: gws.LegendRenderOutput) -> Optional[bytes]: 

33 img = output_to_image(lro) 

34 return img.to_bytes() if img else None 

35 

36 

37def output_to_image(lro: gws.LegendRenderOutput) -> Optional[gws.Image]: 

38 if lro.image: 

39 return lro.image 

40 if lro.image_path: 

41 return gws.lib.image.from_path(lro.image_path) 

42 if lro.html: 

43 return None 

44 

45 

46def output_to_image_path(lro: gws.LegendRenderOutput) -> Optional[str]: 

47 if lro.image: 

48 img_path = gws.u.printtemp('legend.png') 

49 return lro.image.to_path(img_path, gws.lib.mime.PNG) 

50 if lro.image_path: 

51 return lro.image_path 

52 if lro.html: 

53 return None 

54 

55 

56def combine_outputs(lros: list[gws.LegendRenderOutput], options: dict = None) -> Optional[gws.LegendRenderOutput]: 

57 imgs = gws.u.compact(output_to_image(lro) for lro in lros) 

58 img = _combine_images(imgs, options) 

59 if not img: 

60 return None 

61 return gws.LegendRenderOutput(image=img, size=img.size()) 

62 

63 

64def _combine_images(images: list[gws.Image], options: dict = None) -> Optional[gws.Image]: 

65 if not images: 

66 return None 

67 # @TODO other combination options 

68 return _combine_vertically(images) 

69 

70 

71def _combine_vertically(images: list[gws.Image]): 

72 ws = [img.size()[0] for img in images] 

73 hs = [img.size()[1] for img in images] 

74 

75 comp = gws.lib.image.from_size((max(ws), sum(hs))) 

76 y = 0 

77 for img in images: 

78 comp.paste(img, (0, y)) 

79 y += img.size()[1] 

80 

81 return comp