Details for google-location-history-to-gpx.ipynb

Published by gedankenstuecke

Description

Convert your Google Location History data (which is in JSON format) to GPX format, allowing you to use the data e.g. in Google Earth!

1

Tags & Data Sources

GPS location data location movement maps mapping Google Location History Upload

Comments

[url=https://hotelexpress53.com]bonus new member[/url]

Please log in to comment.

Notebook
Last updated 5 years, 3 months ago

Turn Google Location History JSON data into GPX Tracks

With the Google Location History integration into Open Humans you can collect the GPS records that you export from Google Takeout. By default Open Humans uses the JSON representation for storage.

But while JSON is great for many web-based tools to read and use the data, other software that you might want to run locally (e.g. Google Earth) want to get the data as GPX. This notebook will take your Google Location History data set and turn it into GPX files.

The points are transformed into Tracks, so that you can more easily visualize your movement. Your files will be stored in a folder called gpx/ on your notebook server.

Converting the files

First, we define some functions which takes the JSON file, reads it, and then transforms it. Then we store it in the gpx/ folder.

Running the cell below can take some time, depending on how much data you have collected through Google Location History:

And that's already all of it. If we now look into the gpx folder we should see a couple of newly converted GPX files:

gpx/Location History.gpx
gpx/overland-data-2018-10.gpx
gpx/overland-data-2018-11.gpx
gpx/overland-data-2018-12.gpx

Indeed, there is the GPX file for the Google Location History file. You can now download this from the file list by navigating to the gpx folder, selecting the file and choosing Download. You can re-run this notebook at any point to update your files and get the latest data points as GPX.

Enjoy exploring your location data.

Notebook
Last updated 5 years, 3 months ago

Turn Google Location History JSON data into GPX Tracks

With the Google Location History integration into Open Humans you can collect the GPS records that you export from Google Takeout. By default Open Humans uses the JSON representation for storage.

But while JSON is great for many web-based tools to read and use the data, other software that you might want to run locally (e.g. Google Earth) want to get the data as GPX. This notebook will take your Google Location History data set and turn it into GPX files.

The points are transformed into Tracks, so that you can more easily visualize your movement. Your files will be stored in a folder called gpx/ on your notebook server.

Converting the files

First, we define some functions which takes the JSON file, reads it, and then transforms it. Then we store it in the gpx/ folder.

Running the cell below can take some time, depending on how much data you have collected through Google Location History:

In [1]:
import os
from ohapi import api
import json
import requests
import glob 
import datetime

# create path for gpx files if non-existent 
if not os.path.exists('gpx/'):
    os.mkdir('gpx/')


def read_geojson(f_object):
    '''
    read geojson from overland based on OH API output
    '''
    geo_json = requests.get(f_object['download_url']).json()
    return geo_json

def write_gpx(geo_json, fname):
    # open file
    gpx = open('gpx/{}.gpx'.format(fname), 'w')
    #write header
    gpx.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    gpx.write('<gpx version="1.0">\n')
    track_name = ''
    for entry in geo_json['locations']:
        i = {}
        dt = datetime.datetime.fromtimestamp(int(entry['timestampMs']) / 1000)
        i['lat'] = entry['latitudeE7'] / 1e7
        i['lon'] = entry['longitudeE7'] / 1e7
        i['timestamp'] = datetime.datetime.strftime(dt, "%Y-%m-%dT%H:%M:%SZ")
        # if new day, new track
        if track_name != i['timestamp'][:10]:
            # if this is not the first track ever: finish current track
            if track_name:
                gpx.write('</trkseg></trk>\n')
            track_name = i['timestamp'][:10]
            gpx.write('<trk><name>{}</name><number>1</number><trkseg>'.format(track_name))
        gpx.write('<trkpt lat="{}" lon="{}">\n'.format(
                i['lat'],
                i['lon']))
        gpx.write('<time>{}</time>\n'.format(i['timestamp']))
        gpx.write("</trkpt>\n")
    gpx.write('</trkseg></trk>\n')
    gpx.write('</gpx>\n')
    gpx.close()
    

member_data = api.exchange_oauth2_member(os.getenv('OH_ACCESS_TOKEN'))
for data in member_data['data']:
    if data['source'] == "direct-sharing-182":
        geo_json = read_geojson(data)
        write_gpx(geo_json, data['basename'][:-5])

And that's already all of it. If we now look into the gpx folder we should see a couple of newly converted GPX files:

In [2]:
for i in glob.glob('gpx/*'):
    print(i)
gpx/Location History.gpx
gpx/overland-data-2018-10.gpx
gpx/overland-data-2018-11.gpx
gpx/overland-data-2018-12.gpx

Indeed, there is the GPX file for the Google Location History file. You can now download this from the file list by navigating to the gpx folder, selecting the file and choosing Download. You can re-run this notebook at any point to update your files and get the latest data points as GPX.

Enjoy exploring your location data.