Coordinates

 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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
class Degrees(float):
sides = 'EW'
@property
def degree(self):
return int(self)
@property
def minute(self):
return int(self % 1 * 60)
@property
def second(self):
return int(self % 1 * 60 % 1 * 60)
@property
def side(self):
return self.sides[self < 0]
def __repr__(self):
return '''{0.degree}° {0.minute}' {0.second}" {0.side}'''.format(self)
__str__ = __repr__
def __add__(self, value):
return self.__class__(float.__add__(self, value))
def __sub__(self, value):
return self.__class__(float.__sub__(self, value))
def __mul__(self, value):
return self.__class__(float.__mul__(self, value))
def __truediv__(self, value):
return self.__class__(float.__truediv__(self, value))
def deg_min(self):
return int(self), int(self.minute)
class Longitude(Degrees):
sides = 'EW'
def __add__(self, other):
if self.degree + other > 90:
self.degree = float(self.degree) - 90
if self.degree + other < 0:
self.degree = float(self.degree) + 90
return self.__class__(float.__add__(self, other))
class Lattitude(Degrees):
sides = 'NS'
if __name__ == '__main__':
print(Longitude(89.5) + Longitude(1))