{ "metadata": { "name": "", "signature": "sha256:7c011ce3c1df815d663b03a13ea2e0263f9ded07fdd55c462758207ce4b87564" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Map Your Google Location History\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "## Step 1: Download your Google Location History\n", "\n", "Google makes this process very easy. Go here to [download your location history data](https://www.google.com/settings/takeout) and unzip it." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Run this script" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Preliminaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Import pandas\n", "import pandas as pd\n", "\n", "# Import matplotlib and Basemap\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.basemap import Basemap\n", "\n", "# Import decimal\n", "import decimal\n", "\n", "# Set iPython to display visualization inline\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read in the location history json\n", "\n", "Simply change the string to point to where you unzipped your location history json file" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a dataframe from the json file in the filepath\n", "raw = pd.io.json.read_json('/Users/chrisralbon/Downloads/Location History/LocationHistory.json')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's take a look at some of the data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# View the last five rows of the dataframe\n", "raw.tail()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
locationssomePointsTruncated
369608 {'longitudeE7': -770434474, 'timestampMs': '13... True
369609 {'longitudeE7': -770433938, 'timestampMs': '13... True
369610 {'longitudeE7': -770432626, 'timestampMs': '13... True
369611 {'longitudeE7': -770432626, 'timestampMs': '13... True
369612 {'longitudeE7': -769333633, 'timestampMs': '13... True
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ " locations somePointsTruncated\n", "369608 {'longitudeE7': -770434474, 'timestampMs': '13... True\n", "369609 {'longitudeE7': -770433938, 'timestampMs': '13... True\n", "369610 {'longitudeE7': -770432626, 'timestampMs': '13... True\n", "369611 {'longitudeE7': -770432626, 'timestampMs': '13... True\n", "369612 {'longitudeE7': -769333633, 'timestampMs': '13... True" ] } ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Expand the locations object into it's own dataframe" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Expand the locations column into a dataframe\n", "# This lets us move down one level in the json structure\n", "df = raw['locations'].apply(pd.Series)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Take a peak at the data again" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# View the last five rows of the dataframe\n", "df.tail()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wrangle the data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a list from the latitude column\n", "lats = df['latitudeE7']\n", "\n", "# Create a list from the longitude column\n", "lons = df['longitudeE7']\n", "\n", "# Multiply both lists by -E7 to move the decimal point\n", "# to the correct location\n", "lats = lats * 0.0000001\n", "lons = lons * 0.0000001" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# View the last five list elements\n", "list(lons[-5:])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Set the precison of getcontext\n", "decimal.getcontext().prec = 6\n", "\n", "# Convert the float into a decimal string, then put it back into float\n", "lats = [float(decimal.Decimal(\"%.6f\" % e)) for e in lats]\n", "lons = [float(decimal.Decimal(\"%.6f\" % e)) for e in lons]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# View the last five list elements\n", "# to check the decimal function worked\n", "list(lons[-5:])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Map the data using basemap" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a figure of size (i.e. pretty big)\n", "fig = plt.figure(figsize=(20,10))\n", "\n", "# Create a map, using the Gall\u2013Peters projection, \n", "map = Basemap(projection='gall', \n", " # with low resolution,\n", " resolution = 'h', \n", " # And threshold 100000\n", " area_thresh = 100000.0,\n", " llcrnrlon=-0.186768,\n", " llcrnrlat=51.454007,\n", " urcrnrlon=-0.061798,\n", " urcrnrlat=51.530960)\n", "\n", "# Draw the coastlines on the map\n", "map.drawcoastlines()\n", "\n", "# Draw country borders on the map\n", "map.drawcountries()\n", "\n", "# Fill the land with grey\n", "map.fillcontinents(color = '#888888')\n", "\n", "# Draw the map boundaries\n", "map.drawmapboundary(fill_color='#f4f4f4')\n", "\n", "# Define our longitude and latitude points\n", "x,y = map(lons, lats)\n", "\n", "# Plot them using round markers of size 6\n", "map.plot(x, y, 'ro', markersize=6)\n", " \n", "# Show the map\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }