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