Created
September 8, 2014 13:46
-
-
Save chrisalbon/0a55b7b5ef6584e672cd to your computer and use it in GitHub Desktop.
map your google data
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
# coding: utf-8 | |
# # Map Your Google Location History | |
# | |
# ## Step 1: Download your Google Location History | |
# | |
# Google makes this process very easy. Go here to [download your location history data](https://www.google.com/settings/takeout) and unzip it. | |
# ## Step 2: Run this script | |
# ### Preliminaries | |
# In[11]: | |
# Import pandas | |
import pandas as pd | |
# Import matplotlib and Basemap | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.basemap import Basemap | |
# Set iPython to display visualization inline | |
get_ipython().magic('matplotlib inline') | |
# ### Read in the location history json | |
# | |
# Simply change the string to point to where you unzipped your location history json file | |
# In[2]: | |
# Create a dataframe from the json file in the filepath | |
raw = pd.io.json.read_json('/Users/chrisralbon/Downloads/Location History/LocationHistory.json') | |
# ### Let's take a look at some of the data | |
# In[3]: | |
# View the last five rows of the dataframe | |
raw.tail() | |
# ### Expand the locations object into it's own dataframe | |
# In[4]: | |
# Expand the locations column into a dataframe | |
# This lets us move down one level in the json structure | |
df = raw['locations'].apply(pd.Series) | |
# ### Take a peak at the data again | |
# In[5]: | |
# View the last five rows of the dataframe | |
df.tail() | |
# ### Wrangle the data | |
# In[6]: | |
# Create a list from the latitude column, multiplied by -E7 | |
df['latitude'] = df['latitudeE7'] * 0.0000001 | |
# Create a list from the longitude column, multiplied by -E7 | |
df['longitude'] = df['longitudeE7'] * 0.0000001 | |
# ### Map the data using basemap | |
# In[8]: | |
# Create a figure of size (i.e. pretty big) | |
fig = plt.figure(figsize=(20,10)) | |
# Create a map, using the Gall–Peters projection, | |
map = Basemap(projection='gall', | |
# with low resolution, | |
resolution = 'l', | |
# And threshold 100000 | |
area_thresh = 100000.0, | |
# Centered at 0,0 (i.e null island) | |
lat_0=0, lon_0=0) | |
# Draw the coastlines on the map | |
map.drawcoastlines() | |
# Draw country borders on the map | |
map.drawcountries() | |
# Fill the land with grey | |
map.fillcontinents(color = '#888888') | |
# Draw the map boundaries | |
map.drawmapboundary(fill_color='#f4f4f4') | |
# Define our longitude and latitude points | |
x,y = map(df['longitude'].values, df['latitude'].values) | |
# Plot them using round markers of size 6 | |
map.plot(x, y, 'ro', markersize=6) | |
# Show the map | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool, this code beautiful!! i meke this sample use ArcGIS