This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_GPS_EXIF1.py
254 lines (232 loc) · 15.2 KB
/
load_GPS_EXIF1.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
##############################################################################################
# This file is deprecated because Python 2.x is deprecated #
# A Python 3.x version of this file can be found at: #
# #
# https://github.com/Guymer/PyGuymer3/blob/master/load_GPS_EXIF1.py #
##############################################################################################
def load_GPS_EXIF1(fname):
# NOTE: This function uses the Python module "exifread".
# NOTE: Earth's mean radius is 6,371,009 m.
# NOTE: The following web pages were helpful:
# * https://gist.github.com/snakeye/fdc372dbf11370fe29eb
# * https://sno.phy.queensu.ca/~phil/exiftool/TagNames/GPS.html
# Import modules ...
import datetime
import exifread
import math
import pytz
# Create default dictionary answer ...
ans = {}
# Open RAW file read-only ...
with open(fname, "rb") as fobj:
# Load EXIF tags ...
tags = exifread.process_file(fobj, details = False)
# Check that there are EXIF tags ...
gps = False
for key in tags.iterkeys():
if key.startswith("GPS "):
gps = True
break
# Check that there are EXIF tags ...
if gps:
# Check that the required tags are preset ...
if "GPS GPSLongitude" in tags and "GPS GPSLongitudeRef" in tags:
# Extract longitude ...
d = float(tags["GPS GPSLongitude"].values[0].num) / float(tags["GPS GPSLongitude"].values[0].den) # [deg]
m = float(tags["GPS GPSLongitude"].values[1].num) / float(tags["GPS GPSLongitude"].values[1].den) # [min]
s = float(tags["GPS GPSLongitude"].values[2].num) / float(tags["GPS GPSLongitude"].values[2].den) # [sec]
ans["lon"] = d + (m / 60.0) + (s / 3600.0) # [deg]
if tags["GPS GPSLongitudeRef"].values[0] == "W":
ans["lon"] = 0.0 - ans["lon"] # [deg]
elif tags["GPS GPSLongitudeRef"].values[0] != "E":
raise Exception("the longitude reference is unexpected", tags["GPS GPSLongitudeRef"].values)
# Deduce longitude precision ...
ans["lon_prec"] = 0.0 # [deg]
if tags["GPS GPSLongitude"].values[0].den != 1:
ans["lon_prec"] += 1.0 / float(tags["GPS GPSLongitude"].values[0].den) # [deg]
if tags["GPS GPSLongitude"].values[1].den != 1:
ans["lon_prec"] += 1.0 / float(tags["GPS GPSLongitude"].values[1].den) / 60.0 # [deg]
if tags["GPS GPSLongitude"].values[2].den != 1:
ans["lon_prec"] += 1.0 / float(tags["GPS GPSLongitude"].values[2].den) / 3600.0 # [deg]
# Check that the required tags are preset ...
if "GPS GPSLatitude" in tags and "GPS GPSLatitudeRef" in tags:
# Extract latitude ...
d = float(tags["GPS GPSLatitude"].values[0].num) / float(tags["GPS GPSLatitude"].values[0].den) # [deg]
m = float(tags["GPS GPSLatitude"].values[1].num) / float(tags["GPS GPSLatitude"].values[1].den) # [min]
s = float(tags["GPS GPSLatitude"].values[2].num) / float(tags["GPS GPSLatitude"].values[2].den) # [sec]
ans["lat"] = d + (m / 60.0) + (s / 3600.0) # [deg]
if tags["GPS GPSLatitudeRef"].values[0] == "S":
ans["lat"] = 0.0 - ans["lat"] # [deg]
elif tags["GPS GPSLatitudeRef"].values[0] != "N":
raise Exception("the latitude reference is unexpected", tags["GPS GPSLatitudeRef"].values)
# Deduce latitude precision ...
ans["lat_prec"] = 0.0 # [deg]
if tags["GPS GPSLatitude"].values[0].den != 1:
ans["lat_prec"] += 1.0 / float(tags["GPS GPSLatitude"].values[0].den) # [deg]
if tags["GPS GPSLatitude"].values[1].den != 1:
ans["lat_prec"] += 1.0 / float(tags["GPS GPSLatitude"].values[1].den) / 60.0 # [deg]
if tags["GPS GPSLatitude"].values[2].den != 1:
ans["lat_prec"] += 1.0 / float(tags["GPS GPSLatitude"].values[2].den) / 3600.0 # [deg]
# Check that the required tags are preset ...
if "GPS GPSAltitude" in tags and "GPS GPSAltitudeRef" in tags:
# Extract altitude ...
ans["alt"] = float(tags["GPS GPSAltitude"].values[0].num) / float(tags["GPS GPSAltitude"].values[0].den) # [m]
if tags["GPS GPSAltitudeRef"].values[0] == 1:
ans["alt"] = 0.0 - ans["alt"] # [m]
elif tags["GPS GPSAltitudeRef"].values[0] != 0:
raise Exception("the altitude reference is unexpected", tags["GPS GPSAltitudeRef"].values)
# Deduce altitude precision ...
ans["alt_prec"] = 0.0 # [m]
if tags["GPS GPSAltitude"].values[0].den != 1:
ans["alt_prec"] += 1.0 / float(tags["GPS GPSAltitude"].values[0].den) # [m]
# Check that the required tags are preset ...
if "GPS GPSDate" in tags and "GPS GPSTimeStamp" in tags:
# Extract date/time and merge into one (TZ-aware) object (
# correcting mistakes that shouldn't exist) ...
tmp1 = tags["GPS GPSDate"].values.split(":")
ye = int(tmp1[0]) # [year]
mo = int(tmp1[1]) # [month]
da = int(tmp1[2]) # [day]
hr = int(tags["GPS GPSTimeStamp"].values[0].num) # [hour]
mi = int(tags["GPS GPSTimeStamp"].values[1].num) # [minute]
tmp2 = float(tags["GPS GPSTimeStamp"].values[2].num) / float(tags["GPS GPSTimeStamp"].values[2].den) # [s]
se = int(math.floor(tmp2)) # [s]
us = int(1.0e6 * (tmp2 - se)) # [us]
if hr > 23:
# HACK: This particular gem is due to my Motorola Moto G3
# smartphone.
hr = hr % 24 # [hour]
ans["datetime"] = datetime.datetime(
year = ye,
month = mo,
day = da,
hour = hr,
minute = mi,
second = se,
microsecond = us,
tzinfo = pytz.timezone("UTC")
)
# Deduce time precision ...
foo = 0.0 # [s]
if tags["GPS GPSTimeStamp"].values[0].den != 1:
foo += 3600.0 / float(tags["GPS GPSTimeStamp"].values[0].den) # [s]
if tags["GPS GPSTimeStamp"].values[1].den != 1:
foo += 60.0 / float(tags["GPS GPSTimeStamp"].values[1].den) # [s]
if tags["GPS GPSTimeStamp"].values[2].den != 1:
foo += 1.0 / float(tags["GPS GPSTimeStamp"].values[2].den) # [s]
ans["time_prec"] = datetime.timedelta(seconds = foo)
# Check that the required tags are preset ...
if "GPS GPSMapDatum" in tags:
# Extract map datum ...
ans["datum"] = tags["GPS GPSMapDatum"].values
# Check that the required tags are preset ...
if "GPS GPSMeasureMode" in tags:
# Extract measure mode ...
if tags["GPS GPSMeasureMode"].values[0] == "2":
ans["mode"] = u"2D"
elif tags["GPS GPSMeasureMode"].values[0] == "3":
ans["mode"] = u"3D"
else:
raise Exception("the mode is unexpected", tags["GPS GPSMeasureMode"].values)
# Check that the required tags are preset ...
if "GPS GPSDOP" in tags:
# Extract dilution of precision ...
ans["dop"] = float(tags["GPS GPSDOP"].values[0].num) / float(tags["GPS GPSDOP"].values[0].den) # [ratio]
# Estimate the location error ...
# NOTE: The longitude and latitude precisions are added in
# quadrature and then multiplied by the dilution of
# precision to give an estimate of the error, in degrees.
# The error is then divided by 360 to convert it to a
# fraction around a circle. Assuming the Earth is a
# perfect sphere then the fraction can be converted to a
# distance around its circumference.
ans["loc_err"] = ans["dop"] * math.hypot(ans["lon_prec"], ans["lat_prec"]) # [deg]
ans["loc_err"] /= 360.0 # [frac]
ans["loc_err"] *= 2.0 * math.pi * 6371009.0 # [m]
# Estimate the time error ...
# NOTE: The time precision is multiplied by the dilution of
# precision to give an estimate of the error
ans["time_err"] = datetime.timedelta(seconds = ans["dop"] * ans["time_prec"].total_seconds())
# Check that there is location information ...
if "lon" in ans and "lat" in ans:
# Check that there is date/time information ...
if "datetime" in ans:
# Check that there is altitude information ...
if "alt" in ans:
# Check that there is error information ...
if "loc_err" in ans and "time_err" in ans:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°, {2:.1f}m ASL) ± {3:.1f}m at \"{4:s}\" ± {5:.3f}s.".format(
ans["lon"],
ans["lat"],
ans["alt"],
ans["loc_err"],
ans["datetime"].isoformat(" "),
ans["time_err"].total_seconds()
)
else:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°, {2:.1f}m ASL) at \"{3:s}\".".format(
ans["lon"],
ans["lat"],
ans["alt"],
ans["datetime"].isoformat(" "),
)
else:
# Check that there is error information ...
if "loc_err" in ans and "time_err" in ans:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°) ± {2:.1f}m at \"{3:s}\" ± {4:.3f}s.".format(
ans["lon"],
ans["lat"],
ans["loc_err"],
ans["datetime"].isoformat(" "),
ans["time_err"].total_seconds()
)
else:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°) at \"{2:s}\".".format(
ans["lon"],
ans["lat"],
ans["datetime"].isoformat(" "),
)
else:
# Check that there is altitude information ...
if "alt" in ans:
# Check that there is error information ...
if "loc_err" in ans:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°, {2:.1f}m ASL) ± {3:.1f}m.".format(
ans["lon"],
ans["lat"],
ans["alt"],
ans["loc_err"]
)
else:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°, {2:.1f}m ASL).".format(
ans["lon"],
ans["lat"],
ans["alt"]
)
else:
# Check that there is error information ...
if "loc_err" in ans:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°) ± {2:.1f}m.".format(
ans["lon"],
ans["lat"],
ans["loc_err"]
)
else:
# Make a pretty string ...
ans["pretty"] = u"GPS fix returned ({0:.6f}°, {1:.6f}°).".format(
ans["lon"],
ans["lat"]
)
# Return answer ...
if ans == {}:
return False
else:
return ans