-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatitude.py
More file actions
511 lines (433 loc) · 20.5 KB
/
latitude.py
File metadata and controls
511 lines (433 loc) · 20.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import math
class Latitude:
"""
class that represents the Latitude Concept i.e. the distance in Degrees between Equator and a parallel.
"""
def __init__(self, degrees: float = 0.0, minutes: float = 0.0, seconds: float = 0.0, sign: str = 'N'):
""" Constructor for the class
:type degrees: float
:type minutes: float
:type seconds: float
:type sign: str
"""
self.__dict__['degrees'] = 0.0
self.__dict__['minutes'] = 0.0
self.__dict__['seconds'] = 0.0
# to avoid warnings I define the attributes with self
self.degrees = 0.0
self.minutes = 0.0
self.seconds = 0.0
self.sign = sign
self.degrees += degrees
self.minutes += minutes
self.seconds += seconds
def __setattr__(self, key, value):
if 'sign' == key:
try:
value = str(value)
except ValueError as e:
raise ValueError("Sign must be a string N (North) or S (South)") from e
else:
if "N" == value:
super().__setattr__(key, value)
elif "S" == value:
super().__setattr__(key, value)
else:
raise ValueError("Sign must be a string N (North) or S (South)")
elif 'degrees' == key:
try:
value = abs(float(value))
except ValueError as e:
raise ValueError("degrees must be a numerical value") from e
else:
if value > 90:
raise ValueError("Latitude cannot be greater than 90 degrees")
elif 90 == value and (self.minutes > 0 or self.seconds > 0):
raise ValueError("Latitude cannot be greater than 90 degrees")
else:
frac, deg = math.modf(value)
frac, minutes = math.modf(self.minutes + (frac * 60))
sec = self.seconds + (frac * 60)
minutes += sec // 60
sec %= 60
deg += minutes // 60
minutes %= 60
if deg > 90 or (90 == deg and (minutes > 0 or sec > 0)):
raise ValueError("Latitude cannot be greater than 180 degrees")
else:
super().__setattr__('degrees', deg)
super().__setattr__('minutes', minutes)
super().__setattr__('seconds', sec)
elif 'minutes' == key:
try:
value = abs(float(value))
except ValueError as e:
raise ValueError("minutes must be a numerical value") from e
else:
degs = self.degrees
frac, mins = math.modf(value)
secs = self.seconds + (frac * 60)
if secs >= 60:
mins += secs // 60
secs %= 60
if mins >= 60:
degs = self.degrees + (mins // 60)
mins %= 60
if degs > 90 or (90 == degs and (mins > 0 or secs > 0)):
raise ValueError("Latitude cannot be greater than 90 degrees")
else:
super().__setattr__('degrees', degs)
super().__setattr__('minutes', mins)
super().__setattr__('seconds', secs)
elif 'seconds' == key:
try:
value = abs(float(value))
except ValueError as e:
raise ValueError("seconds must be a numerical value") from e
else:
degs = self.degrees
mins = self.minutes
if value >= 60:
mins = self.minutes + (value // 60)
value %= 60
if mins >= 60:
degs = self.degrees + (mins // 60)
mins %= 60
if degs > 90 or (90 == degs and (mins > 0 or value > 0)):
raise ValueError("Latitude cannot be greater than 90 degrees")
else:
super().__setattr__('degrees', degs)
super().__setattr__('minutes', mins)
super().__setattr__('seconds', value)
else:
super().__setattr__(key, value)
def __str__(self):
if 0 == self.degrees and 0 == self.minutes and 0 == self.seconds:
return '{}° {}\' {}" {}'.format(int(self.degrees), int(self.minutes), round(self.seconds, 2), 'N/S')
else:
return '{}° {}\' {}" {}'.format(int(self.degrees), int(self.minutes), round(self.seconds, 2), self.sign)
def __float__(self):
value = self.degrees + (self.minutes / 60) + (self.seconds / 3600)
value = value if self.sign == 'N' else value * -1
return value
def __add__(self, other):
"""
Method that permits to sum a Latitude with a LatitudeDistance or a tuple like this: (deg, mins, secs, sign).
Cannot sum Latitude with Latitude because this operation has no graphical sense so to do an operation like
that you should cast the Latitude type values in float values.
:param other: LatitudeDistance obj or tuple that will be interpreted like a LongitudeDistance obj
:return: Latitude type
"""
if type(other) == type(self):
raise TypeError('cannot sum two Latitude objects: it has no graphic sense, to do that cast Longitudes into'
'floats value')
elif type(other) == tuple:
try:
a = abs(float(other[0]))
b = abs(float(other[1]))
c = abs(float(other[2]))
d = str(other[3])
except ValueError as e:
raise ValueError('the tuple must be like: '
'(degrees: float, minutes: float, seconds: float, sign: str)') from e
else:
other = LatitudeDistance((a, b, c, d))
return self.__add__(other)
elif type(other) == LatitudeDistance:
return float_to_latitude(float(self) + float(other))
else:
raise TypeError('cannot sum Latitude with {}'.format(type(other)))
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
"""
Method that permits to subtract 'something' to a Latitude object, if other is another Latitude object
the method returns a LatitudeDistance object containing the distance between other and self Latitudes
else if other is of type LatitudeDistance the method returns a Latitude value representing the parallel
that is at LatitudeDistance from self Latitude, else if other is a tuple like (degs, mins, secs, sign)
it is interpreted like a Latitude object so it's returned a LatitudeDistance object.
:param other: LatitudeDistance value or tuple like: (deg, mins, secs, sign).
:return: the LatitudeDistance between other and self longitudes
"""
if type(other) == type(self):
# the distance between other and self is returned because:
# if the user write lonDist = longB - longA he wants the distance between
# longA and longB (Mathematically it is what is obtained by this operation)
return LatitudeDistance(other, self)
elif type(other) == tuple:
if len(other) == 4:
try:
a = float(other[0])
b = float(other[1])
c = float(other[2])
d = str(other[3])
except ValueError as e:
raise ValueError('tuple must be of type (degrees, minutes, seconds, sign)') from e
else:
return LatitudeDistance(Latitude(a, b, c, d), self)
else:
raise ValueError('tuple must be like (degrees, minutes, seconds,sign)')
elif type(other) == LatitudeDistance:
a = float(self)
b = float(other)
return float_to_latitude(a - b)
else:
raise TypeError("cannot sub {} with {}".format(type(self), type(other)))
def __rsub__(self, other):
if type(other) == tuple and len(other) != 4:
b = self
a = LatitudeDistance(other)
return a - b
else:
raise TypeError("cannot rsub {} with {}".format(type(self), type(other)))
class LatitudeDistance:
"""
class to represent the distance between two different parallels. It can be from 0 degrees to 180 degrees North or
South.
"""
def __init__(self, a=(0, 0, 0, 'E'), b=None):
self.__dict__['degrees'] = 0.0
self.__dict__['minutes'] = 0.0
self.__dict__['seconds'] = 0.0
self.__dict__['sign'] = 'N'
self.degrees = 0.0
self.minutes = 0.0
self.seconds = 0.0
self.sign = 'N'
if type(a) == Latitude and type(b) == Latitude:
value = float(b) - float(a)
self.sign = 'N' if value > 0 else 'S'
self.degrees = abs(value)
elif type(a) == Latitude and type(b) == tuple:
try:
deg, mins, sec, sign = abs(float(b[0])), abs(float(b[1])), abs(float(b[2])), str(b[3])
except (ValueError, IndexError) as e:
raise ValueError('Tuple must be like (degrees, minutes, seconds, sign)') from e
else:
if sign != 'N' and sign != 'S':
raise ValueError('Sign must be N North or S South')
b = deg + mins / 60 + sec / 3600 if 'N' == sign else (deg + mins / 60 + sec / 3600) * -1
value = b - float(a)
self.sign = 'N' if value > 0 else 'S'
self.degrees = abs(value)
elif type(a) == tuple and type(b) == tuple:
try:
a_deg, a_min, a_sec, a_sign = abs(float(a[0])), abs(float(a[1])), abs(float(a[2])), str(a[3])
b_deg, b_min, b_sec, b_sign = abs(float(b[0])), abs(float(b[1])), abs(float(b[2])), str(b[3])
except (ValueError, IndexError)as e:
raise ValueError('Tuple must be like (degrees, minutes, seconds, sign)') from e
else:
if (a_sign != 'N' and a_sign != 'S') or (b_sign != 'N' and b_sign != 'S'):
raise ValueError('Sign must be N North or S South')
a = a_deg + a_min / 60 + a_sec / 3600 if 'N' == a_sign else (a_deg + a_min / 60 + a_sec / 3600) * -1
b = b_deg + b_min / 60 + b_sec / 3600 if 'N' == b_sign else (b_deg + b_min / 60 + b_sec / 3600) * -1
self.sign = 'N' if (b - a) > 0 else 'S'
self.degrees = abs(b - a)
elif type(a) == tuple and (b is None):
try:
deg, mins, sec, sign = abs(float(a[0])), abs(float(a[1])), abs(float(a[2])), str(a[3])
except (ValueError, IndexError) as e:
raise ValueError('Tuple must be like (degrees, minutes, seconds, sign)') from e
else:
if sign != 'N' and sign != 'S':
raise ValueError('Sign must be N North or S South')
self.degrees += deg
self.minutes += mins
self.seconds += sec
self.sign = sign
else:
raise TypeError("Cannot make a LatitudeDistance between {} and {}".format(type(a), type(b)))
def __setattr__(self, key, value):
if 'sign' == key:
try:
value = str(value)
except ValueError as e:
raise ValueError("Sign must be a string N North or S South") from e
else:
if value != 'N' and value != 'S':
raise ValueError("Sign must be a string N North or S South")
else:
super().__setattr__(key, value)
elif 'degrees' == key:
try:
value = abs(float(value))
except ValueError as e:
raise ValueError("degrees must be a numerical value") from e
else:
if value > 180:
raise ValueError('Latitude distance cannot be greater than 180 degrees')
elif 180 == value and (self.minutes > 0 or self.seconds > 0):
raise ValueError('Latitude distance cannot be greater than 180 degrees')
else:
frac, deg = math.modf(value)
frac, mins = math.modf(self.minutes + (frac * 60))
sec = self.seconds + (frac * 60)
mins += sec // 60
sec %= 60
deg += mins // 60
mins %= 60
if deg > 180 or (180 == deg and (mins > 0 or sec > 0)):
raise ValueError('Latitude distance cannot be greater than 180 degrees')
else:
super().__setattr__('degrees', deg)
super().__setattr__('minutes', mins)
super().__setattr__('seconds', sec)
elif 'minutes' == key:
try:
value = abs(float(value))
except ValueError as e:
raise ValueError("minutes must be a numerical value") from e
else:
frac, mins = math.modf(value)
secs = self.seconds + (frac * 60)
degs = self.degrees
if secs > 60:
mins += secs // 60
secs %= 60
if mins >= 60:
degs = self.degrees + (mins // 60)
mins %= 60
if degs > 180 or (180 == degs and (mins > 0 or secs > 0)):
raise ValueError('Latitude distance cannot be greater than 180 degrees')
else:
super().__setattr__('degrees', degs)
super().__setattr__('minutes', mins)
super().__setattr__('seconds', secs)
elif 'seconds' == key:
try:
secs = abs(float(value))
except ValueError as e:
raise ValueError("seconds must be a numerical value") from e
else:
mins = self.minutes
degs = self.degrees
if secs > 60:
mins = self.minutes + (value // 60)
secs %= 60
if mins > 60:
degs = self.degrees + (mins // 60)
mins %= 60
if degs > 180 or (180 == degs and (mins > 0 or secs > 0)):
raise ValueError('Latitude distance cannot be greater than 180 degrees')
else:
super().__setattr__('degrees', degs)
super().__setattr__('minutes', mins)
super().__setattr__('seconds', secs)
else:
super().__setattr__(key, value)
def __str__(self):
if 0 == self.degrees and 0 == self.minutes and 0 == self.seconds:
return '{}° {}\' {}" {}'.format(int(self.degrees), int(self.minutes), round(self.seconds, 2), 'N/S')
else:
return '{}° {}\' {}" {}'.format(int(self.degrees), int(self.minutes), round(self.seconds, 2), self.sign)
def __float__(self):
value = self.degrees + (self.minutes / 60) + (self.seconds / 3600)
return value if self.sign == 'N' else value * -1
def __add__(self, other):
"""
Method that permits to sum a LatitudeDistance with Latitude, LatitudeDistance or tuple like (degrees, minutes
seconds, sign). Methods returns always a LatitudeDistance object
:param other: LatitudeDistance obj or tuple that will be interpreted like a LatitudeDistance obj
:return: LatitudeDistance type
"""
if type(self) == type(other):
return float_to_latitude_distance(float(other) + float(self))
elif type(other) == Latitude:
return float_to_latitude_distance(float(other) + float(self))
elif type(other) == tuple:
if len(other) != 4:
raise TypeError('tuple must be like (degrees, minutes, seconds, sing)')
else:
try:
a, b, c, d = abs(float(other[0])), abs(float(other[1])), abs(float(other[2])), str(other[3])
except (ValueError, IndexError) as e:
raise ValueError('tuple must be like (degrees, minutes, seconds, sing)') from e
else:
if d != 'N' and d != 'S':
raise ValueError('Sign must be N north or S south')
other = a + b / 60 + c / 3600 if 'N' == d else (a + b / 60 + c / 3600) * -1
return float_to_latitude_distance(float(self) + other)
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
"""
Method that permits to subtract Latitude or LatitudeDistance or tuple of type (degrees, minutes, secons, sign)
to self obj.
:param other: Latitude or LatitudeDistance or tuple object
:return: LatitudeDistance object
"""
if type(other) == type(self):
return float_to_latitude_distance(float(self) - float(other))
elif type(other) == Latitude:
return float_to_latitude_distance(float(self) - float(other))
elif type(other) == tuple:
if len(other) != 4:
raise TypeError('tuple must be like (degrees, minutes, seconds, sing)')
else:
try:
a, b, c, d = abs(float(other[0])), abs(float(other[1])), abs(float(other[2])), str(other[3])
except (ValueError, IndexError) as e:
raise ValueError('tuple must be like (degrees, minutes, seconds, sing)') from e
else:
if d != 'N' and d != 'S':
raise ValueError('Sign must be N north or S south')
other = a + b / 60 + c / 3600 if 'N' == d else (a + b / 60 + c / 3600) * -1
return float_to_latitude_distance(float(self) - other)
def __rsub__(self, other):
if type(other) == tuple:
if len(other) != 4:
raise TypeError('tuple must be like (degrees, minutes, seconds, sing)')
else:
try:
a, b, c, d = abs(float(other[0])), abs(float(other[1])), abs(float(other[2])), str(other[3])
except (ValueError, IndexError) as e:
raise ValueError('tuple must be like (degrees, minutes, seconds, sing)') from e
else:
return LatitudeDistance((a, b, c, d)).__sub__(self)
def float_to_latitude(value: float = 0.0) -> Latitude:
"""
function that return a Latitude object starting from a float value
:param value:
:return:
"""
try:
value = float(value)
except ValueError as e:
raise ValueError('value must be a numerical value') from e
else:
sign = 'N' if value >= 0 else 'S'
return Latitude(degrees=abs(value), sign=sign)
def float_to_latitude_distance(value: float = 0.0) -> LatitudeDistance:
"""
function that returns the LatitudeDistance value starting from a float value
:param value:
:return:
"""
try:
value = float(value)
except ValueError as e:
raise ValueError('value must be a numerical value') from e
else:
sign = 'N' if value >= 0 else 'S'
return LatitudeDistance((value, 0, 0, sign))
def meridional_part(latitude: Latitude) -> float:
"""
this function return the meridional part of a Latitude object
:param latitude: Latitude object
:return: float value of meridional part
"""
if type(latitude) != Latitude:
raise TypeError('cannot make meridional part from {} the argument must be Latitude'.format(type(latitude)))
# variables:
sign = latitude.sign
latitude = math.radians(abs(float(latitude)))
# constants:
briggs_log_module = 0.43429
eccentricity_2 = 0.00672
# variables and logic operation:
first = 10800 / (math.pi * briggs_log_module) * \
math.log(math.tan(math.radians(45) + (latitude / 2)), 10)
second = (10800 / math.pi) * (eccentricity_2 * math.sin(latitude) + (1 / 3) *
(eccentricity_2 ** 2) * (math.sin(latitude) ** 3) + (1 / 5) *
(eccentricity_2 ** 4) * (math.sin(latitude) ** 5))
return round(first - second, 1) if 'N' == sign else round(first - second, 1) * -1