Coverage for csvforwkt/datum.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

45 statements  

1# -*- coding: utf-8 -*- 

2"""This module is responsible to handle a datum.""" 

3from string import Template 

4 

5from .body import IAU_REPORT 

6from .body import IBody 

7from .body import ReferenceShape 

8 

9 

10class Anchor: 

11 """Anchor.""" 

12 

13 TEMPLATE = '\tANCHOR["$name"]' 

14 

15 def __init__(self, name: str = ""): 

16 """Creates an anchor 

17 

18 Args: 

19 name (str, optional): name of the anchor. Defaults to "". 

20 """ 

21 self.__name = name 

22 

23 @property 

24 def name(self) -> str: 

25 """Anchor name. 

26 

27 :getter: Returns the anchor name 

28 :type: str 

29 """ 

30 return self.__name 

31 

32 def wkt(self) -> str: 

33 """Returns the WKT. 

34 

35 Returns: 

36 str: WKT of Anchor 

37 """ 

38 anchor: str 

39 if self.name in ("", "nan : nan"): 

40 anchor = "" 

41 else: 

42 anchor_template: Template = Template(Anchor.TEMPLATE) 

43 anchor = "\n\t" + anchor_template.substitute(name=self.name) 

44 return anchor 

45 

46 

47class Datum: 

48 """A datum is a model of a planet that is used in mapping. The datum 

49 consists of a series of numbers that define the shape and size of the 

50 ellipsoid. A datum is chosen to give the best possible fit to the true 

51 shape of the planet. 

52 """ 

53 

54 TEMPLATE = """DATUM["$datum_name ($version)", 

55 \t$body$anchor], 

56 \tPRIMEM["Reference Meridian", 0, 

57 ANGLEUNIT["degree", 0.0174532925199433, ID["EPSG", 9122]]]""" 

58 

59 TEMPLATE_SPHERE = """DATUM["$datum_name ($version) - Sphere", 

60 \t$body$anchor], 

61 \tPRIMEM["Reference Meridian", 0, 

62 ANGLEUNIT["degree", 0.0174532925199433, ID["EPSG", 9122]]]""" 

63 

64 def __init__( 

65 self, name: str, body: IBody, anchor: Anchor, template: str 

66 ) -> None: 

67 """Creates a datum description for a body. 

68 

69 Args: 

70 name (str): datum name 

71 body (IBody): body 

72 anchor (Anchor): anchor 

73 template (str): template 

74 """ 

75 self.__name: str = name 

76 self.__body: IBody = body 

77 self.__anchor: Anchor = anchor 

78 self.__template: str = template 

79 

80 @staticmethod 

81 def create( 

82 name: str, 

83 body: IBody, 

84 anchor: Anchor, 

85 ) -> "Datum": 

86 """Create a datum 

87 

88 Args: 

89 name (str): datum name 

90 body (IBody): body description 

91 anchor (Anchor): anchor description 

92 

93 Returns: 

94 Datum: datum description 

95 """ 

96 result: Datum 

97 if body.shape == ReferenceShape.SPHERE: 

98 result = Datum(name, body, anchor, Datum.TEMPLATE_SPHERE) 

99 else: 

100 result = Datum(name, body, anchor, Datum.TEMPLATE) 

101 return result 

102 

103 @property 

104 def name(self) -> str: 

105 """Datum name. 

106 

107 :getter: Returns the datum name 

108 :type: str 

109 """ 

110 return self.__name 

111 

112 @property 

113 def body(self) -> IBody: 

114 """Body description. 

115 

116 :getter: Returns the body description 

117 :type: IBody 

118 """ 

119 return self.__body 

120 

121 @property 

122 def anchor(self) -> Anchor: 

123 """Anchor name. 

124 

125 :getter: Returns the anchor description 

126 :type: Anchor 

127 """ 

128 return self.__anchor 

129 

130 def wkt(self) -> str: 

131 """Returns the datum WKT. 

132 

133 Returns: 

134 str: WKT description 

135 """ 

136 datum_template = Template(self.__template) 

137 datum = datum_template.substitute( 

138 version=IAU_REPORT.VERSION, 

139 datum_name=self.name, 

140 body=self.body.wkt() 

141 if self.anchor.wkt() == "" 

142 else self.body.wkt() + ",", 

143 anchor=self.anchor.wkt(), 

144 ) 

145 return datum