Created
June 2, 2017 22:26
-
-
Save dokeeffe/2301bab529d920d7f4d9f66f00f1d3c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Plot your Google location history with matplotlib" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd, numpy as np, matplotlib.pyplot as plt\n", | |
"from datetime import datetime as dt\n", | |
"from mpl_toolkits.basemap import Basemap\n", | |
"import warnings; warnings.simplefilter('ignore')\n", | |
"%matplotlib inline\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Load your history data\n", | |
"Download your entire google location history as json using takeout https://takeout.google.com/settings/takeout \n", | |
"\n", | |
"For me it was just over 1 million points" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"df_gps = pd.read_json('/home/dokeeffe/Downloads/Location History/Location History.json')\n", | |
"print('There are {:,} rows in the location history dataset'.format(len(df_gps)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"df_gps.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Tidy up the data and remove what we dont need" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# parse lat, lon, and timestamp from the dict inside the locations column\n", | |
"df_gps['lat'] = df_gps['locations'].map(lambda x: x['latitudeE7'])\n", | |
"df_gps['lon'] = df_gps['locations'].map(lambda x: x['longitudeE7'])\n", | |
"df_gps['timestamp_ms'] = df_gps['locations'].map(lambda x: x['timestampMs'])\n", | |
"\n", | |
"# convert lat/lon to decimalized degrees and the timestamp to date-time\n", | |
"df_gps['lat'] = df_gps['lat'] / 10.**7\n", | |
"df_gps['lon'] = df_gps['lon'] / 10.**7\n", | |
"df_gps['timestamp_ms'] = df_gps['timestamp_ms'].astype(float) / 1000\n", | |
"df_gps['datetime'] = df_gps['timestamp_ms'].map(lambda x: dt.fromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S'))\n", | |
"date_range = '{}-{}'.format(df_gps['datetime'].min()[:4], df_gps['datetime'].max()[:4])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# drop columns we don't need, then show a slice of the dataframe\n", | |
"df_gps = df_gps.drop(labels=['locations', 'timestamp_ms'], axis=1, inplace=False)\n", | |
"df_gps[1000:1005]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# define map colors\n", | |
"land_color = '#f5f5f3'\n", | |
"water_color = '#cdd2d4'\n", | |
"coastline_color = '#f5f5f3'\n", | |
"border_color = '#bbbbbb'\n", | |
"meridian_color = '#f5f5f3'\n", | |
"marker_fill_color = '#cc3300'\n", | |
"marker_edge_color = 'None'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Plot the entire dataset on a world map" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# create the plot\n", | |
"fig = plt.figure(figsize=(20, 10))\n", | |
"ax = fig.add_subplot(111, axisbg='#ffffff', frame_on=False)\n", | |
"ax.set_title('Google Location History, {}'.format(date_range), fontsize=24, color='#333333')\n", | |
"\n", | |
"# draw the basemap and its features\n", | |
"m = Basemap(projection='kav7', lon_0=0, resolution='c', area_thresh=10000)\n", | |
"m.drawmapboundary(color=border_color, fill_color=water_color)\n", | |
"m.drawcoastlines(color=coastline_color)\n", | |
"m.drawcountries(color=border_color)\n", | |
"m.fillcontinents(color=land_color, lake_color=water_color)\n", | |
"m.drawparallels(np.arange(-90., 120., 30.), color=meridian_color)\n", | |
"m.drawmeridians(np.arange(0., 420., 60.), color=meridian_color)\n", | |
"\n", | |
"# project the location history points then scatter plot them\n", | |
"x, y = m(df_gps['lon'].values, df_gps['lat'].values)\n", | |
"m.scatter(x, y, s=9, color=marker_fill_color, edgecolor=marker_edge_color, alpha=1, zorder=3)\n", | |
"\n", | |
"# show the map\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Map Ireland only" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# first define a transverse mercator projection for ireland\n", | |
"map_width_m = 300 * 1000\n", | |
"map_height_m = 360 * 1000\n", | |
"target_crs = {'datum':'WGS84',\n", | |
" 'ellps':'WGS84',\n", | |
" 'proj':'tmerc',\n", | |
" 'lon_0':-8.2,\n", | |
" 'lat_0':52.2}\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# plot the map\n", | |
"fig_width = 15\n", | |
"fig = plt.figure(figsize=[fig_width, fig_width * map_height_m / float(map_width_m)])\n", | |
"ax = fig.add_subplot(111, axisbg='#ffffff', frame_on=False)\n", | |
"ax.set_title('Ireland Location History, {}'.format(date_range), fontsize=16, color='#333333')\n", | |
"\n", | |
"# resolution of boundary database to use. Can be c (crude), l (low), i (intermediate), h (high), f (full) \n", | |
"res = 'i'\n", | |
"m = Basemap(ellps=target_crs['ellps'],\n", | |
" projection=target_crs['proj'],\n", | |
" lon_0=target_crs['lon_0'], \n", | |
" lat_0=target_crs['lat_0'],\n", | |
" width=map_width_m, \n", | |
" height=map_height_m,\n", | |
" resolution=res,\n", | |
" area_thresh=10000)\n", | |
"\n", | |
"m.drawcoastlines(color=coastline_color)\n", | |
"m.drawcountries(color=border_color)\n", | |
"m.fillcontinents(color=land_color, lake_color=water_color)\n", | |
"m.drawstates(color=border_color)\n", | |
"m.drawmapboundary(fill_color=water_color)\n", | |
"\n", | |
"x, y = m(df_gps['lon'].values, df_gps['lat'].values)\n", | |
"m.scatter(x, y, s=5, color=marker_fill_color, edgecolor=marker_edge_color, alpha=0.2, zorder=3)\n", | |
"plt.show();" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Cork City Only" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# first define a transverse mercator projection for cork\n", | |
"map_width_m = 6 * 1000\n", | |
"map_height_m = 6 * 1000\n", | |
"target_crs = {'datum':'WGS84',\n", | |
" 'ellps':'WGS84',\n", | |
" 'proj':'tmerc',\n", | |
" 'lon_0':-8.470706,\n", | |
" 'lat_0':51.899081}\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# plot the map\n", | |
"fig_width = 15\n", | |
"fig = plt.figure(figsize=[fig_width, fig_width * map_height_m / float(map_width_m)])\n", | |
"ax = fig.add_subplot(111, axisbg='#ffffff', frame_on=False)\n", | |
"ax.set_title('Cork city Location History, {}'.format(date_range), fontsize=16, color='#333333')\n", | |
"\n", | |
"# resolution of boundary database to use. Can be c (crude), l (low), i (intermediate), h (high), f (full) \n", | |
"res = 'f'\n", | |
"m = Basemap(ellps=target_crs['ellps'],\n", | |
" projection=target_crs['proj'],\n", | |
" lon_0=target_crs['lon_0'], \n", | |
" lat_0=target_crs['lat_0'],\n", | |
" width=map_width_m, \n", | |
" height=map_height_m,\n", | |
" resolution=res,\n", | |
" area_thresh=10000)\n", | |
"\n", | |
"m.drawcoastlines(color=coastline_color)\n", | |
"m.drawcountries(color=border_color)\n", | |
"m.fillcontinents(color=land_color, lake_color=water_color)\n", | |
"m.drawstates(color=border_color)\n", | |
"m.drawmapboundary(fill_color=water_color)\n", | |
"\n", | |
"x, y = m(df_gps['lon'].values, df_gps['lat'].values)\n", | |
"m.scatter(x, y, s=5, color=marker_fill_color, edgecolor=marker_edge_color, alpha=0.3, zorder=3)\n", | |
"\n", | |
"# plt.savefig('images/google_location_history_cal_map.png', dpi=300, bbox_inches='tight', pad_inches=0.2)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment