source: src/mlx/util.py@ 94:dc8fbc53a90c

Last change on this file since 94:dc8fbc53a90c was 89:ef4711a984fe, checked in by István Váradi <ivaradi@…>, 13 years ago

Added the collection of some further statistics and the finish page

File size: 4.4 KB
Line 
1# Various utilities
2
3import math
4
5#------------------------------------------------------------------------------
6
7# The average of the radius at the poles and a the equator, in metres
8#EARTH_RADIUS=6367467.4
9EARTH_RADIUS=6371000
10#EARTH_RADIUS=6378137
11
12#------------------------------------------------------------------------------
13
14def getDegMinSec(degrees):
15 """Break up the given floating point degrees value into a tuple.
16
17 The tuple contains 4 items:
18 - the degrees as an integer
19 - the minutes as an integer
20 - the seconds as an integer
21 - 1.0 if the value was non-negative, -1.0 if it was negative."""
22
23 if degrees<0:
24 degrees = -degrees
25 mul = -1.0
26 else:
27 mul = 1.0
28
29 deg = int(degrees)
30 min = int((degrees*60.0)%60.0)
31 sec = int((degrees*3600.0)%60.0)
32
33 return (deg, min, sec, mul)
34
35#------------------------------------------------------------------------------
36
37def getCoordinateString((latitude, longitude)):
38 """Get the string representation of the given coordinate pair."""
39
40 latitude_str = getLatitudeString(latitude)
41 longitude_str = getLongitudeString(longitude)
42
43 return latitude_str + " " + longitude_str
44
45#------------------------------------------------------------------------------
46
47def getLatitudeString(latitude):
48 """Get a string representation of the given latitude."""
49 return getDegreeString(latitude, ["N", "S"])
50
51#------------------------------------------------------------------------------
52
53def getLongitudeString(longitude):
54 """Get a string representation of the given longitude."""
55
56 return getDegreeString(longitude, ["E", "W"])
57
58#------------------------------------------------------------------------------
59
60def getDegreeString(degree, prefixes):
61 """Get a string representation of the given degree.
62
63 If the sign is positive, prefixes[0], otherwise prefixes[1] will be
64 prepended to the string."""
65
66 if degree<0:
67 prefix = prefixes[1]
68 else:
69 prefix = prefixes[0]
70
71 (deg, min, sec, _sign) = getDegMinSec(degree)
72
73 return u"%s%d\u00b0%02d\u2032%02d\u2033" % (prefix, deg, min, sec)
74
75#------------------------------------------------------------------------------
76
77def getTimeIntervalString(seconds):
78 """Get a more human-friendly representation of the given time interval
79 expressed in seconds."""
80 hours = int(seconds / 3600)
81 minutes = int((seconds / 60) % 60)
82 seconds = int(seconds % 60)
83 return "%d:%02d:%02d" % (hours, minutes, seconds)
84
85#------------------------------------------------------------------------------
86
87def km2nm(km):
88 """Convert the given kilometres into nautical miles."""
89 return km/1.852
90
91#------------------------------------------------------------------------------
92
93def nm2km(nm):
94 """Convert the given nautical miles into kilometres."""
95 return nm*1.852
96
97#------------------------------------------------------------------------------
98
99def radians2km(radians):
100 """Convert the given radians into kilometres"""
101 return radians * EARTH_RADIUS / 1000.0
102
103#------------------------------------------------------------------------------
104
105def radians2nm(radians):
106 """Convert the given radians into nautical miles."""
107 return km2nm(radians2km(radians))
108
109#------------------------------------------------------------------------------
110
111def getDistCourse(latitude1, longitude1, latitude2, longitude2):
112 """Get the distance and course between the two geographical coordinates.
113
114 This function calculates the rhumb distance."""
115
116 latitude1 = math.radians(latitude1)
117 longitude1 = math.radians(longitude1)
118
119 latitude2 = math.radians(latitude2)
120 longitude2 = math.radians(longitude2)
121
122 dlon_W = (longitude1 - longitude2) % (math.pi*2)
123 dlon_E = (longitude2 - longitude1) % (math.pi*2)
124
125 dphi = math.log(math.tan(latitude2/2 + math.pi/4)/
126 math.tan(latitude1/2 + math.pi/4))
127
128 if abs(latitude1-latitude2) < math.sqrt(1e-15):
129 q = math.cos(latitude1)
130 else:
131 q = (latitude1-latitude2)/dphi
132
133 if dlon_W < dlon_E:
134 tc = math.atan2(-dlon_W, dphi) % (math.pi*2)
135 d = math.sqrt(math.pow(q*dlon_W, 2) +
136 math.pow(latitude1-latitude2, 2))
137 else:
138 tc = math.atan2(dlon_E, dphi) % (math.pi*2)
139 d = math.sqrt(math.pow(q*dlon_E, 2) +
140 math.pow(latitude1-latitude2, 2))
141
142 return (radians2nm(d), math.degrees(tc))
143
Note: See TracBrowser for help on using the repository browser.