summaryrefslogtreecommitdiffstats
path: root/coord.py
blob: fb0043611085507180819f9533a15e4c7cb9670c (plain)
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
#!/usr/bin/python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 :

import math

class Coord:
    """
    This class is a representation of a coordinate in the WSG84-system.
    lat is the Latitude (in Radians) and lon the Longitude.
    """
    lat = 0
    lon = 0

    def __init__(self, pLat, pLon):
        """
        Just converts the arguments to radians and sets the properties.
        """
        # ValueError if too big or small, or if not a number
        self.lat = math.radians(pLat)
        self.lon = math.radians(pLon)

    @classmethod
    def get_with_unit(cls, pLat, pLon):
        """
        Converts tuples in the form of ((LatDeg, LatUnit), (LonDeg, LonUnit))
        into positive/negative degrees.
        Returns a tuple (LatDeg, LonDeg)
        """
        (LatDeg, LatUnit) = pLat
        (LonDeg, LonUnit) = pLon

        if LatUnit == 'S':
            LatDeg *= -1
        elif LatUnit == 'N':
            pass
        else:
            raise ValueError

        if LonUnit == 'W':
            LonDeg *= -1
        elif LonUnit == 'E':
            pass
        else:
            raise ValueError

        return (LatDeg, LonDeg)

    @classmethod
    def fromD_unit(cls, pLat, pLon):
        """
        Initializes the class with a tuple in the form of
        ((LatDeg, LatUnit), (LonDeg, LonUnit))
        """
        (LatDeg, LatUnit) = pLat
        (LonDeg, LonUnit) = pLon

        (LatDeg, LonDeg) = cls.get_with_unit((LatDeg, LatUnit),
                                             (LonDeg, LonUnit))

        return cls(LatDeg, LonDeg)

    @classmethod
    def fromDM(cls, pLat, pLon):
        """
        Initializes the class with a tuple in the form of
        ((LatDeg, LatMin), (LonDeg, LonMin))
        """
        # ValueError if len() not 2 or not numbers
        (LatDeg, LatMin) = pLat
        (LonDeg, LonMin) = pLon

        decLat = LatDeg + (1/60*LatMin)
        decLon = LonDeg + (1/60*LonMin)

        return cls(decLat, decLon)

    @classmethod
    def fromDM_unit(cls, pLat, pLon):
        """
        Initializes the class with a tuple in the form of
        ((LatDeg, LatMin, LatUnit), (LonDeg, LonMin, LonUnit))
        """

        (LatDeg, LatMin, LatUnit) = pLat
        (LonDeg, LonMin, LonUnit) = pLon

        (LatDeg, LonDeg) = cls.get_with_unit((LatDeg, LatUnit),
                                             (LonDeg, LonUnit))

        return cls.fromDM((LatDeg, LatMin), (LonDeg, LonMin))

    @classmethod
    def fromDMS(cls, pLat, pLon):
        """
        Initializes the class with a tuple in the form of
        ((LatDeg, LatMin, LatSec), (LonDeg, LonMin, LonSec))
        """
        # ValueError if len() not 3 or not numbers
        (LatDeg, LatMin, LatSec) = pLat
        (LonDeg, LonMin, LonSec) = pLon

        decLat = LatDeg + (1/60*LatMin) + (1/3600*LatSec)
        decLon = LonDeg + (1/60*LonMin) + (1/3600*LonSec)

        return cls(decLat, decLon)

    @classmethod
    def fromDMS_unit(cls, pLat, pLon):
        """
        Initializes the class with a tuple in the form of
        ((LatDeg, LatMin, LatSec, LatUnit), (LonDeg, LonMin, LonSec, LonUnit))
        """
        (LatDeg, LatMin, LatSec, LatUnit) = pLat
        (LonDeg, LonMin, LonSec, LonUnit) = pLon

        (LatDeg, LonDeg) = cls.get_with_unit((LatDeg, LatUnit),
                                             (LonDeg, LonUnit))

        return cls.fromDMS((LatDeg, LatMin, LatSec), (LonDeg, LonMin, LonSec))

    def toD(self):
        """
        Returns lat and lon converted to degrees
        """
        lat = math.degrees(self.lat)
        lon = math.degrees(self.lon)

        return (lat, lon)
            
    def toD_unit(self):
        """
        Returns a tuple ((LatDeg, LatUnit), (LonDeg, LonUnit))
        """
        (LatDeg, LonDeg) = self.toD()
        ((LatDeg, LatUnit), (LonDeg, LonUnit)) = self._getunit(LatDeg, LonDeg)

        return ((LatDeg, LatUnit), (LonDeg, LonUnit))

    def toDM(self):
        """
        Returns a tuple ((LatDeg, LatMin), (LonDeg, LonMin))
        """
        (lat, lon) = self.toD()

        LatDeg = int(lat)
        LatMin = (lat - LatDeg) * 60

        LonDeg = int(lon)
        LonMin = (lon - LonDeg) * 60

        return ((LatDeg, LatMin), (LonDeg, LonMin))

    def toDM_unit(self):
        """
        Returns a tuple ((LatDeg, LatMin, LatUnit), (LonDeg, LonMin, LonUnit))
        """
        ((LatDeg, LatMin), (LonDeg, LonMin)) = self.toDM()
        ((LatDeg, LatUnit), (LonDeg, LonUnit)) = self._getunit(LatDeg, LonDeg)

        return ((LatDeg, LatMin, LatUnit), (LonDeg, LonMin, LonUnit))
        
    def toDMS(self):
        """
        Returns a tuple ((LatDeg, LatMin, LatSec), (LonDeg, LonMin, LonSec))
        """
        (lat, lon) = self.toD()
        LatDeg = int(lat)
        LatDecMin = (lat - LatDeg) * 60
        LatMin = int(LatDecMin)
        LatSec = (LatDecMin - LatMin) * 60

        LonDeg = int(lon)
        LonDecMin = (lon - LonDeg) * 60
        LonMin = int(LonDecMin)
        LonSec = (LonDecMin - LonMin) * 60

        return ((LatDeg, LatMin, LatSec), (LonDeg, LonMin, LonSec))

    def toDMS_unit(self):
        """
        Returns a tuple
        ((LatDeg, LatMin, LatSec, LatUnit), (LonDeg, LonMin, LonSec, LonUnit))
        """
        ((LatDeg, LatMin, LatSec), (LonDeg, LonMin, LonSec)) = self.toDMS()
        ((LatDeg, LatUnit), (LonDeg, LonUnit)) = self._getunit(LatDeg, LonDeg)

        return  ((LatDeg, LatMin, LatSec, LatUnit),
                 (LonDeg, LonMin, LonSec, LonUnit))

    def _getunit(self, LatDeg, LonDeg):
        """
        Takes a Latitude/Longitude in positive/negative form
        and returns their absolute and their unit (N/S or E/W)
        """
        if LatDeg >= 0:
            LatUnit = 'N'
        else:
            LatUnit = 'S'
            LatDeg = abs(LatDeg)

        if LonDeg >= 0:
            LonUnit = 'E'
        else:
            LonUnit = 'W'
            LonDeg = abs(LonDeg)

        return ((LatDeg, LatUnit), (LonDeg, LonUnit))


def distance(coord1, coord2):
    """
    Takes two Coord-Objects and returns the great circle distance in meters
    between them.
    """
    R = 6371009 # Something like that. Earth radius in meters

    dLat = coord2.lat - coord1.lat
    dLon = coord2.lon - coord1.lon

    a = (math.sin(dLat / 2)**2 + math.cos(coord1.lat) * math.cos(coord2.lat) *
         math.sin(dLon / 2)**2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return R * c

def bearing(coord1, coord2):
    """
    Takes two Coord-Objects and returns the bearing between them
    """
    # FIXME I don't want to write this myself...
    return None

def speed(coord1, coord2, t):
    """
    Takes two Coord-Objects and a time and retuns the speed
    """
    s = distance(coord, coord2)
    v = s / t
    
    return v