-
Notifications
You must be signed in to change notification settings - Fork 4
/
ellipsoid_mp.py
279 lines (220 loc) · 8.44 KB
/
ellipsoid_mp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# SPDX-FileCopyrightText: Steven Ward
# SPDX-License-Identifier: OSL-3.0
# pylint: disable=invalid-name
# pylint: disable=line-too-long
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=no-member
__author__ = 'Steven Ward'
__license__ = 'OSL-3.0'
import gmpy2
# https://gmpy2.readthedocs.io/en/latest/mpfr.html
gmpy2.set_context(gmpy2.ieee(256))
# pylint: disable=too-many-instance-attributes
class Ellipsoid:
# pylint: disable=too-many-locals
def __init__(self,
_a: gmpy2.mpfr,
_f_recip: gmpy2.mpfr,
_GM: gmpy2.mpfr = gmpy2.mpfr('3.986004418E14'),
_omega: gmpy2.mpfr = gmpy2.mpfr('7.292115E-5')):
# defining parameters
a = _a # semi-major axis (equatorial radius of the earth) (meters)
f = 1 / _f_recip # (a-b)/a # flattening factor of the earth
GM = _GM # geocentric gravitational constant (m³/s²)
omega = _omega # nominal mean angular velocity of the earth (rad/s)
# derived geometric constants
b = a*(1-f) # semi-minor axis (meters)
a2 = a*a # a squared
b2 = b*b # b squared
fp = f/(1-f) # (a-b)/b # second flattening
n = f/(2-f) # (a-b)/(a+b) # third flattening
e2 = f*(2-f) # (a2-b2)/a2 # first eccentricity squared
e = gmpy2.sqrt(e2) # first eccentricity
ep2 = e2/(1-e2) # (a2-b2)/b2 # second eccentricity squared
ep = gmpy2.sqrt(ep2) # second eccentricity
epp2 = e2/(2-e2) # (a2-b2)/(a2+b2) # third eccentricity squared
epp = gmpy2.sqrt(epp2) # third eccentricity
c2 = a2 - b2 # linear eccentricity squared
c = gmpy2.sqrt(c2) # linear eccentricity
alpha = gmpy2.asin(e) # angular eccentricity # acos(b/a)
# derived physical constants
gamma_e = gmpy2.mpfr('9.7803253359') # normal gravity at the equator (on the ellipsoid) (m/s²)
gamma_p = gmpy2.mpfr('9.8321849379') # normal gravity at the poles (on the ellipsoid) (m/s²)
k = (1 - f) * gamma_p / gamma_e - 1 # Somigliana's Formula - normal gravity formula constant
m = omega * omega * a2 * b / GM # normal gravity formula constant
self.a = a
self.f = f
self.GM = GM
self.omega = omega
self.b = b
self.a2 = a2
self.b2 = b2
self.fp = fp
self.n = n
self.e2 = e2
self.e = e
self.ep2 = ep2
self.ep = ep
self.epp2 = epp2
self.epp = epp
self.c2 = c2
self.c = c
self.alpha = alpha
self.gamma_e = gamma_e
self.gamma_p = gamma_p
self.k = k
self.m = m
def get_Rn(self, sin_lat: gmpy2.mpfr) -> gmpy2.mpfr:
d2 = 1 - self.e2 * sin_lat * sin_lat
d = gmpy2.sqrt(d2)
return self.a / d
def get_R(self, sin_lat: gmpy2.mpfr) -> gmpy2.mpfr:
return self.get_Rn(sin_lat) * gmpy2.sqrt(1 - self.e2 * sin_lat * sin_lat * (2 - self.e2))
def get_Rm(self, sin_lat: gmpy2.mpfr) -> gmpy2.mpfr:
d2 = 1 - self.e2 * sin_lat * sin_lat
d = gmpy2.sqrt(d2)
return self.a * (1 - self.e2) / (d2 * d)
def get_gamma(self, sin_lat: gmpy2.mpfr) -> gmpy2.mpfr:
d2 = 1 - self.e2 * sin_lat * sin_lat
d = gmpy2.sqrt(d2)
return self.gamma_e * (1 + self.k * sin_lat * sin_lat) / d
def get_gamma_h(self, sin_lat: gmpy2.mpfr, ht: gmpy2.mpfr) -> gmpy2.mpfr:
return self.get_gamma(sin_lat) * (1
- 2 * ht * (1 + self.f + self.m - 2 * self.f * sin_lat * sin_lat) / self.a
+ 3 * ht * ht / self.a2)
# pylint: disable=too-many-arguments
def get_ht(self, w: gmpy2.mpfr, z: gmpy2.mpfr, sin_lat: gmpy2.mpfr, cos_lat: gmpy2.mpfr, Rn: gmpy2.mpfr) -> gmpy2.mpfr:
# pylint: disable=no-else-return
# https://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html
# cos(45°) == 1/√(2)
if cos_lat > 1 / gmpy2.sqrt(2): # Equatorial
return w / cos_lat - Rn
else: # Polar
return z / sin_lat - Rn * (1 - self.e2)
def geodetic_to_ecef(self, lat_deg: gmpy2.mpfr, lon_deg: gmpy2.mpfr, ht: gmpy2.mpfr) -> tuple:
lat_rad = gmpy2.radians(lat_deg)
lon_rad = gmpy2.radians(lon_deg)
sin_lat = gmpy2.sin(lat_rad)
cos_lat = gmpy2.cos(lat_rad)
sin_lon = gmpy2.sin(lon_rad)
cos_lon = gmpy2.cos(lon_rad)
Rn = self.get_Rn(sin_lat)
x = (Rn + ht) * cos_lat * cos_lon
y = (Rn + ht) * cos_lat * sin_lon
z = (Rn * (1 - self.e2) + ht) * sin_lat
return (x, y, z)
def geodetic_2d_to_ecef(self, lat_deg: gmpy2.mpfr, ht: gmpy2.mpfr) -> tuple:
lat_rad = gmpy2.radians(lat_deg)
sin_lat = gmpy2.sin(lat_rad)
cos_lat = gmpy2.cos(lat_rad)
Rn = self.get_Rn(sin_lat)
w = (Rn + ht) * cos_lat
z = (Rn * (1 - self.e2) + ht) * sin_lat
return (w, z)
def ecef_to_geodetic(self, x: gmpy2.mpfr, y: gmpy2.mpfr, z: gmpy2.mpfr) -> tuple:
'''Olson, D. K. (1996). Converting Earth-Centered, Earth-Fixed Coordinates to Geodetic Coordinates. IEEE Transactions on Aerospace and Electronic Systems, 32(1), 473–476. https://doi.org/10.1109/7.481290
Converted to Python and modified by Steven Ward. No rights reserved.
'''
w2 = x * x + y * y
w = gmpy2.sqrt(w2)
z2 = z * z
lon_rad = gmpy2.atan2(y, x)
a1 = self.a * self.e2
a2 = a1 * a1
a3 = a1 * self.e2 / 2
a4 = 2.5 * a2
a5 = a1 + a3
#a6 = (1 - self.e2)
r2 = w2 + z2
r = gmpy2.sqrt(r2)
s2 = z2 / r2
c2 = w2 / r2
u = a2 / r
v = a3 - a4 / r
s = 0
c = 0
ss = 0
# cos(45°)² == ½
if c2 > 0.5: # Equatorial
s = (z / r) * (1 + c2 * (a1 + u + s2 * v) / r)
lat_rad = gmpy2.asin(s)
ss = s * s
c = gmpy2.sqrt(1 - ss)
else: # Polar
c = (w / r) * (1 - s2 * (a5 - u - c2 * v) / r)
lat_rad = gmpy2.acos(c)
ss = 1 - c * c
s = gmpy2.sqrt(ss)
if z < 0:
lat_rad = -lat_rad
s = -s
d2 = 1 - self.e2 * ss
Rn = self.a / gmpy2.sqrt(d2)
Rm = (1 - self.e2) * Rn / d2
rf = (1 - self.e2) * Rn
u = w - Rn * c
v = z - rf * s
f = c * u + s * v
m = c * v - s * u
p = m / (Rm + f)
lat_rad += p
ht = f + m * p / 2
return (gmpy2.degrees(lat_rad), gmpy2.degrees(lon_rad), ht)
def ecef_2d_to_geodetic(self, w: gmpy2.mpfr, z: gmpy2.mpfr) -> tuple:
'''Olson, D. K. (1996). Converting Earth-Centered, Earth-Fixed Coordinates to Geodetic Coordinates. IEEE Transactions on Aerospace and Electronic Systems, 32(1), 473–476. https://doi.org/10.1109/7.481290
Converted to Python and modified by Steven Ward. No rights reserved.
'''
w2 = w * w
z2 = z * z
a1 = self.a * self.e2
a2 = a1 * a1
a3 = a1 * self.e2 / 2
a4 = 2.5 * a2
a5 = a1 + a3
#a6 = (1 - self.e2)
r2 = w2 + z2
r = gmpy2.sqrt(r2)
s2 = z2 / r2
c2 = w2 / r2
u = a2 / r
v = a3 - a4 / r
s = 0
c = 0
ss = 0
# cos(45°)² == ½
if c2 > 0.5: # Equatorial
s = (z / r) * (1 + c2 * (a1 + u + s2 * v) / r)
lat_rad = gmpy2.asin(s)
ss = s * s
c = gmpy2.sqrt(1 - ss)
else: # Polar
c = (w / r) * (1 - s2 * (a5 - u - c2 * v) / r)
lat_rad = gmpy2.acos(c)
ss = 1 - c * c
s = gmpy2.sqrt(ss)
if z < 0:
lat_rad = -lat_rad
s = -s
d2 = 1 - self.e2 * ss
Rn = self.a / gmpy2.sqrt(d2)
Rm = (1 - self.e2) * Rn / d2
rf = (1 - self.e2) * Rn
u = w - Rn * c
v = z - rf * s
f = c * u + s * v
m = c * v - s * u
p = m / (Rm + f)
lat_rad += p
ht = f + m * p / 2
return (gmpy2.degrees(lat_rad), ht)
WGS84 = Ellipsoid(gmpy2.mpfr('6_378_137.0'), gmpy2.mpfr('298.257223563'))
'''
Source:
NGA.STND.0036_1.0.0_WGS84 2014-07-08
Appendix C.1
Reference Ellipsoid Names and Constants
Used for Datum Transformations*
* Refer to Appendices D, E, and F.
'''