1
2
3
4
5
6 import time, calendar, math
7
8
9 METERS_TO_FEET = 3.2808399
10 METERS_TO_MILES = 0.00062137119
11 KNOTS_TO_MPH = 1.1507794
12 KNOTS_TO_KPH = 1.852
13 KNOTS_TO_MPS = 0.51444444
14 MPS_TO_KPH = 3.6
15 MPS_TO_MPH = 2.2369363
16 MPS_TO_KNOTS = 1.9438445
17
18
19
21 "Degrees to radians."
22 return x * (math.pi/180)
23
25 "Radians to degress."
26 return x * (180/math.pi)
27
29 "Radius of curvature in meters at specified latitude."
30 a = 6378.137
31 e2 = 0.081082 * 0.081082
32
33
34
35
36
37
38
39
40
41
42
43
44 sc = math.sin(Deg2Rad(lat))
45 x = a * (1.0 - e2)
46 z = 1.0 - e2 * sc * sc
47 y = pow(z, 1.5)
48 r = x / y
49
50 r = r * 1000.0
51 return r
52
54 "Distance in meters between two points specified in degrees."
55 x1 = CalcRad(lat1) * math.cos(Deg2Rad(lon1)) * math.sin(Deg2Rad(90-lat1))
56 x2 = CalcRad(lat2) * math.cos(Deg2Rad(lon2)) * math.sin(Deg2Rad(90-lat2))
57 y1 = CalcRad(lat1) * math.sin(Deg2Rad(lon1)) * math.sin(Deg2Rad(90-lat1))
58 y2 = CalcRad(lat2) * math.sin(Deg2Rad(lon2)) * math.sin(Deg2Rad(90-lat2))
59 z1 = CalcRad(lat1) * math.cos(Deg2Rad(90-lat1))
60 z2 = CalcRad(lat2) * math.cos(Deg2Rad(90-lat2))
61 a = (x1*x2 + y1*y2 + z1*z2)/pow(CalcRad((lat1+lat2)/2), 2)
62
63
64
65
66 if abs(a) > 1: a = 1
67 elif a < -1: a = -1
68 return CalcRad((lat1+lat2) / 2) * math.acos(a)
69
71 "Return offset in meters of second arg from first."
72 dx = EarthDistance((lat1, lon1), (lat1, lon2))
73 dy = EarthDistance((lat1, lon1), (lat2, lon1))
74 if lat1 < lat2: dy *= -1
75 if lon1 < lon2: dx *= -1
76 return (dx, dy)
77
79 "Convert timestamps in ISO8661 format to and from Unix time."
80 if type(s) == type(1):
81 return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(s))
82 elif type(s) == type(1.0):
83 date = int(s)
84 msec = s - date
85 date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(s))
86 return date + "." + `msec`[2:]
87 elif type(s) == type(""):
88 if s[-1] == "Z":
89 s = s[:-1]
90 if "." in s:
91 (date, msec) = s.split(".")
92 else:
93 date = s
94 msec = "0"
95
96 return calendar.timegm(time.strptime(date, "%Y-%m-%dT%H:%M:%S")) + float("0." + msec)
97 else:
98 raise TypeError
99
100
101