Python has massive capabilities in fitting well to data wrangling and manipulation tasks.
These abilities complemented with folium makes its easy for you to visualize this data
I’ll be running all commands from a Jupyter notebook.
To get stated,
Import numpy and pandas python packages for data manipulation
import pandas as pd
import geo
For purposes of demontrations, I use data from a class I took.
The data is captures in a csv file
df = pd.read_csv('track.csv',parse_dates=['time'])
df.head()
time | lat | lng | height | |
---|---|---|---|---|
0 | 2015-08-20 03:48:07.235 | 35.015021 | 32.519585 | 136.199997 |
1 | 2015-08-20 03:48:24.734 | 35.014954 | 32.519606 | 126.599998 |
2 | 2015-08-20 03:48:25.660 | 35.014871 | 32.519612 | 123.000000 |
3 | 2015-08-20 03:48:26.819 | 35.014824 | 32.519654 | 120.500000 |
4 | 2015-08-20 03:48:27.828 | 35.014776 | 32.519689 | 118.900002 |
Index the dataframe by the 'time' column
df.index = df.time
df.index[:10]
DatetimeIndex(['2015-08-20 03:48:07.235000', '2015-08-20 03:48:24.734000',
'2015-08-20 03:48:25.660000', '2015-08-20 03:48:26.819000',
'2015-08-20 03:48:27.828000', '2015-08-20 03:48:29.720000',
'2015-08-20 03:48:30.669000', '2015-08-20 03:48:33.793000',
'2015-08-20 03:48:34.869000', '2015-08-20 03:48:37.708000'],
dtype='datetime64[ns]', name='time', freq=None)
localize the time
(OPTIONAL)
Am in Nairobi currently so (+3:00 GMT)
import pytz
ts = df.index[0]
ts.tz_localize(pytz.utc)
Timestamp(‘2015-08-20 03:48:07.235000+0000’, tz=’UTC’)
ts.tz_localize(pytz.UTC).tz_convert(pytz.timezone('Africa/Nairobi'))
Timestamp(‘2015-08-20 06:48:07.235000+0300’, tz=’Africa/Nairobi’)
Plot a marker on the map
Using a circular marker, add a popup depicting time at a particular location
import folium
m = folium.Map(location=[df['lng'].mean(), df['lat'].mean()], zoom_start=15)
row = df.iloc[321]
marker = folium.CircleMarker([row['lng'], row['lat']], radius=5, color='red', popup=row['time'].strftime('%H:%M'))
marker.add_to(m)
m
Plot track followed
m = folium.Map(location=[df['lng'].mean(), df['lat'].mean()], zoom_start=15)
def add_marker(row):
marker = folium.CircleMarker([row['lng'], row['lat']], radius=5, color='red', popup=row['time'].strftime('%H:%M'))
marker.add_to(m)
df.apply(add_marker, axis=1)
m
Resample to clear overlaps
m = folium.Map(location=[df['lng'].mean(), df['lat'].mean()], zoom_start=15)
mdf = df.resample('T').mean()
def add_marker(row):
marker = folium.CircleMarker([row['lng'], row['lat']], radius=5, color='red', popup=row.name.strftime('%H:%M'))
marker.add_to(m)
mdf.apply(add_marker, axis=1)
m
A check on the resampled data
mdf.head()
lat | lng | height | |
---|---|---|---|
time | |||
2015-08-20 03:48:00 | 35.014571 | 32.519485 | 122.766666 |
2015-08-20 03:49:00 | 35.014316 | 32.518971 | 122.966667 |
2015-08-20 03:50:00 | 35.014055 | 32.517258 | 114.573913 |
2015-08-20 03:51:00 | 35.013864 | 32.516264 | 106.653334 |
2015-08-20 03:52:00 | 35.012744 | 32.515141 | 97.710000 |