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
/
load_GPS_EXIF2.py
114 lines (106 loc) · 4.45 KB
/
load_GPS_EXIF2.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
# -*- 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_EXIF2.py #
##############################################################################################
def load_GPS_EXIF2(fname):
# NOTE: This function uses the binary "exiftool".
# Import modules ...
import datetime
import json
import math
import pytz
import subprocess
# Create default dictionary answer ...
ans = {}
# Run "exiftool" and load it as JSON ...
out = subprocess.check_output(
[
"exiftool",
"-json",
"-dateFormat", "%Y:%m:%d %H:%M:%S.%f",
"--printConv",
"-GPSDateTime",
"-GPSAltitude",
"-GPSLongitude",
"-GPSLatitude",
"-GPSHPositioningError",
fname
]
)
dat = json.loads(out)[0]
# Populate dictionary ...
if "GPSLongitude" in dat:
ans["lon"] = dat["GPSLongitude"] # [deg]
if "GPSLatitude" in dat:
ans["lat"] = dat["GPSLatitude"] # [deg]
if "GPSAltitude" in dat:
ans["alt"] = dat["GPSAltitude"] # [m]
if "GPSHPositioningError" in dat:
ans["loc_err"] = dat["GPSHPositioningError"] # [m]
if "GPSDateTime" in dat:
date, time = dat["GPSDateTime"][:-1].split(" ")
tmp1 = date.split(":")
tmp2 = time.split(":")
ye = int(tmp1[0]) # [year]
mo = int(tmp1[1]) # [month]
da = int(tmp1[2]) # [day]
hr = int(tmp2[0]) # [hour]
mi = int(tmp2[1]) # [minute]
se = int(math.floor(float(tmp2[2]))) # [s]
us = int(1.0e6 * (float(tmp2[2]) - 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")
)
# 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:
# 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:
# 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:
# 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:
# 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