forked from Scarygami/location-history-json-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation_history_json_converter.py
executable file
·224 lines (196 loc) · 10.5 KB
/
location_history_json_converter.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
#!/usr/bin/env python
# Copyright 2012-2017 Gerwin Sturm
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
import sys
import json
import math
from argparse import ArgumentParser, ArgumentTypeError
from datetime import datetime
def valid_date(s):
try:
return datetime.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise ArgumentTypeError(msg)
def dateCheck(timestampms, startdate, enddate):
dt = datetime.utcfromtimestamp(int(timestampms) / 1000)
if startdate and startdate > dt : return False
if enddate and enddate < dt : return False
return True
def main():
arg_parser = ArgumentParser()
arg_parser.add_argument("input", help="Input File (JSON)")
arg_parser.add_argument("-o", "--output", help="Output File (will be overwritten!)")
arg_parser.add_argument("-f", "--format", choices=["kml", "json", "csv", "js", "gpx", "gpxtracks"], default="kml", help="Format of the output")
arg_parser.add_argument("-v", "--variable", default="locationJsonData", help="Variable name to be used for js output")
arg_parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD (0h00)", type=valid_date)
arg_parser.add_argument('-e', "--enddate", help="The End Date - format YYYY-MM-DD (0h00)", type=valid_date)
arg_parser.add_argument('-c', "--chronological", help="Sort items in chronological order", action="store_true")
args = arg_parser.parse_args()
if not args.output: #if the output file is not specified, set to input filename with a diffrent extension
args.output = '.'.join(args.input.split('.')[:-1]) + '.' + args.format
if args.input == args.output:
arg_parser.error("Input and output have to be different files")
return
try:
json_data = open(args.input).read()
except:
print("Error opening input file")
return
try:
data = json.loads(json_data)
except:
print("Error decoding json")
return
if "locations" in data and len(data["locations"]) > 0:
try:
f_out = open(args.output, "w")
except:
print("Error creating output file for writing")
return
items = data["locations"]
if args.startdate or args.enddate:
items = [ item for item in items if dateCheck(item["timestampMs"], args.startdate, args.enddate) ]
if args.chronological:
items = sorted(items, key=lambda item: item["timestampMs"])
if args.format == "json" or args.format == "js":
if args.format == "js":
f_out.write("window.%s = " % args.variable)
f_out.write("{\"locations\":[")
first = True
for item in items:
if first:
first = False
else:
f_out.write(",")
f_out.write("{")
f_out.write("\"timestampMs\":%s," % item["timestampMs"])
f_out.write("\"latitudeE7\":%s," % item["latitudeE7"])
f_out.write("\"longitudeE7\":%s" % item["longitudeE7"])
f_out.write("}")
f_out.write("]}")
if args.format == "js":
f_out.write(";")
if args.format == "csv":
f_out.write("Time,Latitude,Longitude\n")
for item in items:
f_out.write(datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%d %H:%M:%S"))
f_out.write(",")
f_out.write("%s,%s\n" % (item["latitudeE7"] / 10000000, item["longitudeE7"] / 10000000))
if args.format == "kml":
f_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
f_out.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n")
f_out.write(" <Document>\n")
f_out.write(" <name>Location History</name>\n")
for item in items:
f_out.write(" <Placemark>\n")
# Order of these tags is important to make valid KML: TimeStamp, ExtendedData, then Point
f_out.write(" <TimeStamp><when>")
f_out.write(datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%dT%H:%M:%SZ"))
f_out.write("</when></TimeStamp>\n")
if "accuracy" in item or "speed" in item or "altitude" in item:
f_out.write(" <ExtendedData>\n")
if "accuracy" in item:
f_out.write(" <Data name=\"accuracy\">\n")
f_out.write(" <value>%d</value>\n" % item["accuracy"])
f_out.write(" </Data>\n")
if "speed" in item:
f_out.write(" <Data name=\"speed\">\n")
f_out.write(" <value>%d</value>\n" % item["speed"])
f_out.write(" </Data>\n")
if "altitude" in item:
f_out.write(" <Data name=\"altitude\">\n")
f_out.write(" <value>%d</value>\n" % item["altitude"])
f_out.write(" </Data>\n")
f_out.write(" </ExtendedData>\n")
f_out.write(" <Point><coordinates>%s,%s</coordinates></Point>\n" % (item["longitudeE7"] / 10000000, item["latitudeE7"] / 10000000))
f_out.write(" </Placemark>\n")
f_out.write(" </Document>\n</kml>\n")
if args.format == "gpx" or args.format == "gpxtracks":
f_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
f_out.write("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" version=\"1.1\" creator=\"Google Latitude JSON Converter\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n")
f_out.write(" <metadata>\n")
f_out.write(" <name>Location History</name>\n")
f_out.write(" </metadata>\n")
if args.format == "gpx":
for item in items:
f_out.write(" <wpt lat=\"%s\" lon=\"%s\">\n" % (item["latitudeE7"] / 10000000, item["longitudeE7"] / 10000000))
if "altitude" in item:
f_out.write(" <ele>%d</ele>\n" % item["altitude"])
f_out.write(" <time>%s</time>\n" % str(datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%dT%H:%M:%SZ")))
f_out.write(" <desc>%s" % datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%d %H:%M:%S"))
if "accuracy" in item or "speed" in item:
f_out.write(" (")
if "accuracy" in item:
f_out.write("Accuracy: %d" % item["accuracy"])
if "accuracy" in item and "speed" in item:
f_out.write(", ")
if "speed" in item:
f_out.write("Speed:%d" % item["speed"])
f_out.write(")")
f_out.write("</desc>\n")
f_out.write(" </wpt>\n")
if args.format == "gpxtracks":
f_out.write(" <trk>\n")
f_out.write(" <trkseg>\n")
lastloc = None
# The deltas below assume input is in chronological or reverse chronological order.
# If it's not, use the '--chronological' option or uncomment this:
# items = sorted(data["data"]["items"], key=lambda x: x['timestampMs'], reverse=True)
for item in items:
if lastloc:
timedelta = abs((int(item['timestampMs']) - int(lastloc['timestampMs'])) / 1000 / 60)
distancedelta = getDistanceFromLatLonInKm(item['latitudeE7'] / 10000000, item['longitudeE7'] / 10000000, lastloc['latitudeE7'] / 10000000, lastloc['longitudeE7'] / 10000000)
if timedelta > 10 or distancedelta > 40:
# No points for 10 minutes or 40km in under 10m? Start a new track.
f_out.write(" </trkseg>\n")
f_out.write(" </trk>\n")
f_out.write(" <trk>\n")
f_out.write(" <trkseg>\n")
f_out.write(" <trkpt lat=\"%s\" lon=\"%s\">\n" % (item["latitudeE7"] / 10000000, item["longitudeE7"] / 10000000))
if "altitude" in item:
f_out.write(" <ele>%d</ele>\n" % item["altitude"])
f_out.write(" <time>%s</time>\n" % str(datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%dT%H:%M:%SZ")))
if "accuracy" in item or "speed" in item:
f_out.write(" <desc>\n")
if "accuracy" in item:
f_out.write(" Accuracy: %d\n" % item["accuracy"])
if "speed" in item:
f_out.write(" Speed:%d\n" % item["speed"])
f_out.write(" </desc>\n")
f_out.write(" </trkpt>\n")
lastloc = item
f_out.write(" </trkseg>\n")
f_out.write(" </trk>\n")
f_out.write("</gpx>\n")
f_out.close()
else:
print("No data found in json")
return
# Haversine formula
def getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2):
R = 6371 # Radius of the earth in km
dlat = deg2rad(lat2-lat1)
dlon = deg2rad(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + \
math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * \
math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = R * c # Distance in km
return d
def deg2rad(deg):
return deg * (math.pi/180)
if __name__ == "__main__":
sys.exit(main())