Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of sphgeom. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22"""Add support for YAML serialization of regions.""" 

23 

24__all__ = () 

25 

26import yaml 

27from .region import Region 

28from .convexPolygon import ConvexPolygon 

29from .ellipse import Ellipse 

30from .circle import Circle 

31from .box import Box 

32from .htmPixelization import HtmPixelization 

33from .q3cPixelization import Q3cPixelization 

34from .mq3cPixelization import Mq3cPixelization 

35 

36YamlLoaders = (yaml.Loader, yaml.CLoader, yaml.FullLoader, yaml.SafeLoader, yaml.UnsafeLoader) 

37 

38 

39# Regions 

40 

41def region_representer(dumper, data): 

42 """Represent a sphgeom region object in a form suitable for YAML. 

43 

44 Stores the region as a mapping with a single ``encoded`` key 

45 storing the hex encoded byte string. 

46 """ 

47 encoded = data.encode() 

48 return dumper.represent_mapping(f"lsst.sphgeom.{type(data).__name__}", 

49 {"encoded": encoded.hex()}) 

50 

51 

52def region_constructor(loader, node): 

53 """Construct a sphgeom region from YAML""" 

54 mapping = loader.construct_mapping(node) 

55 encoded = bytes.fromhex(mapping["encoded"]) 

56 # The generic Region base class can instantiate a region of the 

57 # correct type. 

58 return Region.decode(encoded) 

59 

60 

61# Register all the region classes with the same constructor and representer 

62for region_class in (ConvexPolygon, Ellipse, Circle, Box): 

63 yaml.add_representer(region_class, region_representer) 

64 

65 for loader in YamlLoaders: 

66 yaml.add_constructor(f"lsst.sphgeom.{region_class.__name__}", region_constructor, Loader=loader) 

67 

68 

69# Pixelization schemes 

70 

71def pixel_representer(dumper, data): 

72 """Represent a pixelization in YAML 

73 

74 Stored as the pixelization level in a mapping with a single key 

75 ``level``. 

76 """ 

77 return dumper.represent_mapping(f"lsst.sphgeom.{type(data).__name__}", 

78 {"level": data.getLevel()}) 

79 

80 

81def pixel_constructor(loader, node): 

82 """Construct a pixelization object from YAML. 

83 """ 

84 mapping = loader.construct_mapping(node) 

85 

86 className = node.tag 

87 pixelMap = {"lsst.sphgeom.Q3cPixelization": Q3cPixelization, 

88 "lsst.sphgeom.Mq3cPixelization": Mq3cPixelization, 

89 "lsst.sphgeom.HtmPixelization": HtmPixelization, 

90 } 

91 

92 if className not in pixelMap: 

93 raise RuntimeError(f"Encountered unexpected class {className} associated with" 

94 " sphgeom pixelization YAML constructor") 

95 

96 return pixelMap[className](mapping["level"]) 

97 

98 

99# All the pixelization schemes use the same approach with getLevel 

100for pixelSchemeCls in (HtmPixelization, Q3cPixelization, Mq3cPixelization): 

101 yaml.add_representer(pixelSchemeCls, pixel_representer) 

102 for loader in YamlLoaders: 

103 yaml.add_constructor(f"lsst.sphgeom.{pixelSchemeCls.__name__}", pixel_constructor, Loader=loader)