Skip to content

Instantly share code, notes, and snippets.

@gati
Created September 7, 2014 16:48
Show Gist options
  • Save gati/400f462a6fff00448dad to your computer and use it in GitHub Desktop.
Save gati/400f462a6fff00448dad to your computer and use it in GitHub Desktop.
Format Google Location data
import os
import json
import datetime
"""
I wanted to map where I'd been in the past year using DataMaps, a jQuery + D3
library for visualizing geospatial data. https://github.com/markmarkoh/datamaps
First I downloaded my data from Google https://www.google.com/settings/takeout
(I let them track my Android phone), and then used the script below to
format the JSON the way DataMaps expects.
Obviously this isn't production code. It's super slow, but I only needed to
run it once, so...
Assumes the file you downloaded from Google is in a 'data' directory relative
to the current working directory, and that you only want locations from Jan 1
of this year.
"""
cur_dir = os.path.dirname(os.path.realpath(__file__))
json_file = open(cur_dir + '/data/LocationHistory.json')
json_data = json.load(json_file)
fixed = []
first_of_year = datetime.datetime(2014, 1, 1)
for loc in json_data['locations']:
# This is optional - I only wanted my results from after Jan 1 of this year
if datetime.datetime.fromtimestamp(int(loc['timestampMs'])/1000.0) > first_of_year:
fixed.append({
'latitude': loc['latitudeE7'] * 0.0000001,
'longitude': loc['longitudeE7'] * 0.0000001
})
with open(cur_dir + '/data/fixed_this_year.json', 'w') as outfile:
json.dump(fixed, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment