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
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
1# -*- coding: utf-8 -*-
2"""This module is responsible to handle a datum."""
3from string import Template
5from .body import IAU_REPORT
6from .body import IBody
7from .body import ReferenceShape
10class Anchor:
11 """Anchor."""
13 TEMPLATE = '\tANCHOR["$name"]'
15 def __init__(self, name: str = ""):
16 """Creates an anchor
18 Args:
19 name (str, optional): name of the anchor. Defaults to "".
20 """
21 self.__name = name
23 @property
24 def name(self) -> str:
25 """Anchor name.
27 :getter: Returns the anchor name
28 :type: str
29 """
30 return self.__name
32 def wkt(self) -> str:
33 """Returns the WKT.
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
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 """
54 TEMPLATE = """DATUM["$datum_name ($version)",
55 \t$body$anchor],
56 \tPRIMEM["Reference Meridian", 0,
57 ANGLEUNIT["degree", 0.0174532925199433, ID["EPSG", 9122]]]"""
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]]]"""
64 def __init__(
65 self, name: str, body: IBody, anchor: Anchor, template: str
66 ) -> None:
67 """Creates a datum description for a body.
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
80 @staticmethod
81 def create(
82 name: str,
83 body: IBody,
84 anchor: Anchor,
85 ) -> "Datum":
86 """Create a datum
88 Args:
89 name (str): datum name
90 body (IBody): body description
91 anchor (Anchor): anchor description
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
103 @property
104 def name(self) -> str:
105 """Datum name.
107 :getter: Returns the datum name
108 :type: str
109 """
110 return self.__name
112 @property
113 def body(self) -> IBody:
114 """Body description.
116 :getter: Returns the body description
117 :type: IBody
118 """
119 return self.__body
121 @property
122 def anchor(self) -> Anchor:
123 """Anchor name.
125 :getter: Returns the anchor description
126 :type: Anchor
127 """
128 return self.__anchor
130 def wkt(self) -> str:
131 """Returns the datum WKT.
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