By Sukhbinder and inspired from this
Google makes this process very easy. Go here to download your location history data and unzip it.
fname=r"/Users/Sukhbinder/Downloads/Takeout/Location History/LocationHistory.json"
# Import pandas
import pandas as pd
import numpy as np
# Import matplotlib and Basemap
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap
# Set iPython to display visualization inline
%matplotlib inline
Simply change the string to point to where you unzipped your location history json file
raw = pd.read_json(fname) #pd.io.json.read_json(fname)
raw.tail(5)
locations | somePointsTruncated | |
---|---|---|
86241 | {u'latitudeE7': 129896780, u'accuracy': 100, u... | True |
86242 | {u'latitudeE7': 129896780, u'accuracy': 100, u... | True |
86243 | {u'latitudeE7': 129896780, u'accuracy': 100, u... | True |
86244 | {u'latitudeE7': 129896780, u'accuracy': 100, u... | True |
86245 | {u'latitudeE7': 129947642, u'accuracy': 798, u... | True |
5 rows × 2 columns
df = raw['locations'].apply(pd.Series)
df.tail(5)
accuracy | activitys | altitude | heading | latitudeE7 | longitudeE7 | timestampMs | velocity | |
---|---|---|---|---|---|---|---|---|
86241 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096571529 | NaN |
86242 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096556086 | NaN |
86243 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096541775 | NaN |
86244 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096526596 | NaN |
86245 | 798 | NaN | NaN | NaN | 129947642 | 776835705 | 1297274751020 | NaN |
5 rows × 8 columns
df['latitude'] = df['latitudeE7'] * 0.0000001
# Create a list from the longitude column, multiplied by -E7
df['longitude'] = df['longitudeE7'] * 0.0000001
df.tail(5)
accuracy | activitys | altitude | heading | latitudeE7 | longitudeE7 | timestampMs | velocity | latitude | longitude | |
---|---|---|---|---|---|---|---|---|---|---|
86241 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096571529 | NaN | 12.989678 | 77.688110 |
86242 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096556086 | NaN | 12.989678 | 77.688110 |
86243 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096541775 | NaN | 12.989678 | 77.688110 |
86244 | 100 | NaN | NaN | NaN | 129896780 | 776881100 | 1304096526596 | NaN | 12.989678 | 77.688110 |
86245 | 798 | NaN | NaN | NaN | 129947642 | 776835705 | 1297274751020 | NaN | 12.994764 | 77.683571 |
5 rows × 10 columns
df.head(5)
accuracy | activitys | altitude | heading | latitudeE7 | longitudeE7 | timestampMs | velocity | latitude | longitude | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 165 | NaN | 901 | -1 | 129919563 | 777150227 | 1404655403815 | -1 | 12.991956 | 77.715023 |
1 | 165 | NaN | 900 | -1 | 129919981 | 777145832 | 1404634564910 | -1 | 12.991998 | 77.714583 |
2 | 165 | NaN | 900 | -1 | 129919532 | 777150546 | 1404630560819 | -1 | 12.991953 | 77.715055 |
3 | 165 | NaN | 900 | -1 | 129920234 | 777143167 | 1404626864671 | -1 | 12.992023 | 77.714317 |
4 | 1414 | NaN | 900 | -1 | 129954840 | 776779268 | 1404626799099 | -1 | 12.995484 | 77.677927 |
5 rows × 10 columns
lat=df['longitude'].values*np.pi/180.0
long=df['latitude'].values*np.pi/180.0
x = np.cos(long)*np.cos(lat)
y = np.cos(long)*np.sin(lat)
z = np.sin(long)
import vtk
class VtkPointCloud:
def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e6):
self.maxNumPoints = maxNumPoints
self.vtkPolyData = vtk.vtkPolyData()
self.clearPoints()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(self.vtkPolyData)
mapper.SetScalarRange(zMin, zMax)
mapper.SetScalarVisibility(1)
self.vtkActor = vtk.vtkActor()
self.vtkActor.SetMapper(mapper)
self.vtkActor.GetProperty().SetPointSize(4)
self.vtkActor.GetProperty().SetColor(1,0,0)
def addPoint(self, point):
if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
pointId = self.vtkPoints.InsertNextPoint(point[:])
self.vtkDepth.InsertNextValue(point[2])
self.vtkCells.InsertNextCell(1)
self.vtkCells.InsertCellPoint(pointId)
self.vtkCells.Modified()
self.vtkPoints.Modified()
self.vtkDepth.Modified()
def clearPoints(self):
self.vtkPoints = vtk.vtkPoints()
self.vtkCells = vtk.vtkCellArray()
self.vtkDepth = vtk.vtkDoubleArray()
self.vtkDepth.SetName('DepthArray')
self.vtkPolyData.SetPoints(self.vtkPoints)
self.vtkPolyData.SetVerts(self.vtkCells)
self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
cldpnts= VtkPointCloud()
for i in range(len(x)):
cldpnts.addPoint([x[i],y[i],z[i]])
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
WIDTH=640
HEIGHT=480
renWin.SetSize(WIDTH,HEIGHT)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.SetBackground(0.48, 0.48, 0.48)
# create source
source = vtk.vtkEarthSource()
source.SetOnRatio = 2
# mapper for original earth
coneMapper1 = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
coneMapper1.SetInput(source.GetOutput())
else:
coneMapper1.SetInputConnection(source.GetOutputPort())
# actor for earth
actor1 = vtk.vtkActor()
actor1.SetMapper(coneMapper1)
actor1.GetProperty().SetColor(0, 0, 0) # (R,G,B)
actor1.GetProperty().SetSpecular = 0.45
actor1.GetProperty().SetSpecularPower = 5
actor1.SetBackfaceCulling = True
# add points actor on the earth
# assign actor to the renderer
ren.AddActor(cldpnts.vtkActor)
ren.AddActor(actor1)
# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
# 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()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-11-f566cc02e48c> in <module>() 3 4 # Create a map, using the Gall–Peters projection, ----> 5 map = Basemap(projection='gall', 6 # with low resolution, 7 resolution = 'l', NameError: name 'Basemap' is not defined
<matplotlib.figure.Figure at 0x12442a450>