In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
#%matplotlib inline
plt.rcParams['text.usetex'] = True
plt.rcParams['figure.figsize'] = [10, 8]
plt.rcParams['font.size'] = 16
In [ ]:
# Create a dataframe from the json file in the filepath
raw = pd.io.json.read_json('LocationHistory.json')
df = raw['locations'].apply(pd.Series)
In [ ]:
df['latitude'] = df['latitudeE7'] * 1e-7
df['longitude'] = df['longitudeE7'] * 1e-7
In [24]:
df.columns
Out[24]:
In [28]:
df.drop(['activitys', 'altitude', 'heading', 'latitudeE7', 'longitudeE7', 'velocity'] , axis=1, inplace=True)
In [2]:
def Haversine(theta):
return np.sin(theta/2.0)**2
def DistanceFromGreenwhich(lat, lon):
R = 6.371e6 # m
latG, lonG = 51.48, 0.00 # Grenwhich lat and long
latG = np.radians(latG)
lonG = np.radians(lonG)
lat = np.radians(lat)
lon = np.radians(lon)
arg = Haversine(lat - latG) + np.cos(latG)*np.cos(lat)*Haversine(lon - lonG)
return 2 * R * np.arcsin(np.sqrt(arg))
df['DistanceFromGreenwhich'] = DistanceFromGreenwhich(df.latitude, df.longitude)
df_home = df[df.DistanceFromGreenwhich < 300e3]
In [10]:
def PaddingFunction(xL, xR, frac=0.1):
""" Return xL and xR with an added padding factor of frac either side """
xRange = xR - xL
xL_new = xL - frac*xRange
xR_new = xR + frac*xRange
return xL_new, xR_new
def GeneratePlot(data, fig=None, ignore_first=False, *args, **kwargs):
""" Helper function to plot points on a map
Parameters
----------
ignore_first : bool,
If true the data in the first df in data is ignored and used only to set
up the map
"""
if type(data) == pd.core.frame.DataFrame:
# Single df
df = data
df_list = [df]
elif type(data) == list:
df_list = data
df = data[0]
if not fig:
fig = plt.figure()
# Calculate some parameters which will be resused]
lat_0 = df.latitude.mean()
lon_0 = df.longitude.mean()
llcrnrlon, urcrnrlon = PaddingFunction(df.longitude.min(), df.longitude.max(), frac=0.3)
llcrnrlat, urcrnrlat = PaddingFunction(df.latitude.min(), df.latitude.max())
# Create a map, using the Gall–Peters projection,
m = Basemap(projection='gall',
resolution = 'h',
area_thresh = 10000.0,
lat_0=lat_0, lon_0=lon_0,
llcrnrlon=llcrnrlon,
urcrnrlon=urcrnrlon,
llcrnrlat=llcrnrlat,
urcrnrlat=urcrnrlat,
ax=fig.gca()
)
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color = '#996633')
m.drawmapboundary(fill_color='#0099FF')
if ignore_first:
df_list = df_list[1:]
for df in df_list:
# Define our longitude and latitude points
x, y = m(df['longitude'].values, df['latitude'].values)
# Plot them using round markers of size 6
m.plot(x, y, "o", zorder=100, *args, **kwargs)
return fig
In [7]:
fig = GeneratePlot(df_home, color="r")
plt.show()
In [12]:
fig = GeneratePlot(df_home[df_home.accuracy < 50], color="r")
plt.show()
In [35]:
df_home['Deltat'] = np.concatenate(([0], np.diff(df_home.timestampMs.values.astype(np.float64))))
df_home
Out[35]:
In [40]:
plt.hist(df_home.Deltat[df_home.Deltat > -1e5], bins=50, log=True)
plt.show()
In [31]:
df[df.Deltat < -1e10]
Out[31]: