-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.py
240 lines (192 loc) · 6.71 KB
/
geo.py
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
240
# -*- coding: utf-8 -*-
import datetime
import math
WGS84_a = 6378137.0
WGS84_b = 6356752.314245
def ecef_from_lla(lat, lon, alt):
'''
Compute ECEF XYZ from latitude, longitude and altitude.
All using the WGS94 model.
Altitude is the distance to the WGS94 ellipsoid.
Check results here http://www.oc.nps.edu/oc2902w/coord/llhxyz.htm
'''
a2 = WGS84_a ** 2
b2 = WGS84_b ** 2
lat = math.radians(lat)
lon = math.radians(lon)
L = 1.0 / math.sqrt(a2 * math.cos(lat) ** 2 + b2 * math.sin(lat) ** 2)
x = (a2 * L + alt) * math.cos(lat) * math.cos(lon)
y = (a2 * L + alt) * math.cos(lat) * math.sin(lon)
z = (b2 * L + alt) * math.sin(lat)
return x, y, z
def gps_distance(latlon_1, latlon_2):
'''
Distance between two (lat,lon) pairs.
>>> p1 = (42.1, -11.1)
>>> p2 = (42.2, -11.3)
>>> 19000 < gps_distance(p1, p2) < 20000
True
'''
x1, y1, z1 = ecef_from_lla(latlon_1[0], latlon_1[1], 0.)
x2, y2, z2 = ecef_from_lla(latlon_2[0], latlon_2[1], 0.)
dis = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2)
return dis
def dms_to_decimal(degrees, minutes, seconds, hemisphere):
'''
Convert from degrees, minutes, seconds to decimal degrees.
@author: mprins
'''
dms = float(degrees) + float(minutes) / 60 + float(seconds) / 3600
if hemisphere in "WwSs":
dms = -1 * dms
return dms
def decimal_to_dms(value, precision):
'''
Convert decimal position to degrees, minutes, seconds in a fromat supported by EXIF
'''
deg = math.floor(value)
min = math.floor((value - deg) * 60)
sec = math.floor((value - deg - min / 60) * 3600 * precision)
return ((deg, 1), (min, 1), (sec, precision))
def gpgga_to_dms(gpgga):
'''
Convert GPS coordinate in GPGGA format to degree/minute/second
Reference: http://us.cactii.net/~bb/gps.py
'''
deg_min, dmin = gpgga.split('.')
degrees = int(deg_min[:-2])
minutes = float('%s.%s' % (deg_min[-2:], dmin))
decimal = degrees + (minutes / 60)
return decimal
def utc_to_localtime(utc_time):
utc_offset_timedelta = datetime.datetime.utcnow() - datetime.datetime.now()
return utc_time - utc_offset_timedelta
def compute_bearing(start_lat, start_lon, end_lat, end_lon):
'''
Get the compass bearing from start to end.
Formula from
http://www.movable-type.co.uk/scripts/latlong.html
'''
# make sure everything is in radians
start_lat = math.radians(start_lat)
start_lon = math.radians(start_lon)
end_lat = math.radians(end_lat)
end_lon = math.radians(end_lon)
dLong = end_lon - start_lon
dPhi = math.log(math.tan(end_lat / 2.0 + math.pi / 4.0) /
math.tan(start_lat / 2.0 + math.pi / 4.0))
if abs(dLong) > math.pi:
if dLong > 0.0:
dLong = -(2.0 * math.pi - dLong)
else:
dLong = (2.0 * math.pi + dLong)
y = math.sin(dLong) * math.cos(end_lat)
x = math.cos(start_lat) * math.sin(end_lat) - \
math.sin(start_lat) * math.cos(end_lat) * math.cos(dLong)
bearing = (math.degrees(math.atan2(y, x)) + 360.0) % 360.0
return bearing
def diff_bearing(b1, b2):
'''
Compute difference between two bearings
'''
d = abs(b2 - b1)
d = 360 - d if d > 180 else d
return d
def offset_bearing(bearing, offset):
'''
Add offset to bearing
'''
bearing = (bearing + offset) % 360
return bearing
def normalize_bearing(bearing, check_hex=False):
'''
Normalize bearing and convert from hex if
'''
if bearing > 360 and check_hex:
# fix negative value wrongly parsed in exifread
# -360 degree -> 4294966935 when converting from hex
bearing = bin(int(bearing))[2:]
bearing = ''.join([str(int(int(a) == 0)) for a in bearing])
bearing = -float(int(bearing, 2))
bearing %= 360
return bearing
def interpolate_lat_lon(points, t, max_dt=1):
'''
Return interpolated lat, lon and compass bearing for time t.
Points is a list of tuples (time, lat, lon, elevation), t a datetime object.
'''
# find the enclosing points in sorted list
if (t <= points[0][0]) or (t >= points[-1][0]):
if t <= points[0][0]:
dt = abs((points[0][0] - t).total_seconds())
else:
dt = (t - points[-1][0]).total_seconds()
if dt > max_dt:
raise ValueError(
"time t not in scope of gpx file by {} seconds".format(dt))
else:
print(
"time t not in scope of gpx file by {} seconds, extrapolating...".format(dt))
if t < points[0][0]:
before = points[0]
after = points[1]
else:
before = points[-2]
after = points[-1]
bearing = compute_bearing(before[1], before[2], after[1], after[2])
if t == points[0][0]:
x = points[0]
return (x[1], x[2], bearing, x[3])
if t == points[-1][0]:
x = points[-1]
return (x[1], x[2], bearing, x[3])
else:
for i, point in enumerate(points):
if t < point[0]:
if i > 0:
before = points[i - 1]
else:
before = points[i]
after = points[i]
break
# weight based on time
weight = (t - before[0]).total_seconds() / \
(after[0] - before[0]).total_seconds()
# simple linear interpolation in case points are not the same
if before[1] == after[1]:
lat = before[1]
else:
lat = before[1] - weight * before[1] + weight * after[1]
if before[2] == after[2]:
lon = before[2]
else:
lon = before[2] - weight * before[2] + weight * after[2]
# camera angle
bearing = compute_bearing(before[1], before[2], after[1], after[2])
# altitude
if before[3] is not None:
ele = before[3] - weight * before[3] + weight * after[3]
else:
ele = None
return lat, lon, bearing, ele
def write_gpx(filename, gps_trace):
time_format = "%Y-%m-%dT%H:%M:%S.%f"
gpx = "<gpx>" + "\n"
gpx += "<trk>" + "\n"
gpx += "<name>Mapillary GPX</name>" + "\n"
gpx += "<trkseg>" + "\n"
for point in gps_trace:
lat = point[1]
lon = point[2]
time = datetime.datetime.strftime(point[0], time_format)[:-3]
elevation = point[3]
gpx += "<trkpt lat=\"" + \
str(lat) + "\" lon=\"" + str(lon) + "\">" + "\n"
gpx += "<ele>" + str(elevation) + "</ele>" + "\n"
gpx += "<time>" + time + "</time>" + "\n"
gpx += "</trkpt>" + "\n"
gpx += "</trkseg>" + "\n"
gpx += "</trk>" + "\n"
gpx += "</gpx>" + "\n"
with open(filename, "w") as fout:
fout.write(gpx)