from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
from datetime import datetime
import numpy as np
import pandas as pd
import holoviews as hv
import hvplot.pandas # noqa
from datashader.utils import lnglat_to_meters
from holoviews.element import tiles
from holoviews.util.transform import dim
from holoviews.selection import link_selections
from holoviews.operation import gridmatrix
from holoviews.operation.element import histogram
from holoviews import opts
hv.__version__
opts.defaults(opts.Overlay(active_tools=['wheel_zoom']))
csv_file = 'E:/Geodata/AISDK/raw_ais/aisdk_20170701.csv'
df = pd.read_csv(csv_file, usecols=['Latitude', 'Longitude', 'Ship type', 'SOG', 'MMSI', '# Timestamp'])
df.rename(columns={'# Timestamp':'Timestamp'}, inplace=True)
len(df)
df['Timestamp'] = pd.to_datetime(df['Timestamp'], format='%d/%m/%Y %H:%M:%S')
df.set_index('Timestamp', inplace=True)
df.head()
df.loc[:, 'x'], df.loc[:, 'y'] = lnglat_to_meters(df.Longitude, df.Latitude)
map_plot = (df.hvplot.scatter(x='x', y='y', datashade=True, height=200, width=400) )
map_plot
hist_plot = df.where((df.SOG>0) & (df.SOG<50)).hvplot.hist("SOG", bins=20, width=400, height=200) # .where((df.SOG>0) & (df.SOG<50))
hist_plot
link_selections(map_plot + hist_plot)
from holoviews.element import tiles
from holoviews.operation.datashader import datashade
BG_TILES = tiles.CartoLight()
grouped = [dfx[['x','y']] for name, dfx in
df.groupby(['MMSI'])]
path = hv.Path(grouped, kdims=['x','y'])
plot = datashade(path).opts(frame_height=300, frame_width=300)
BG_TILES * plot
TIME_SAMPLE = '15min'
df['MMSI'].dropna().resample(TIME_SAMPLE).count() \
.hvplot(title=f'Records per {TIME_SAMPLE}', frame_width=300, frame_height=100)
df.head()
map_plot = (tiles.CartoLight() * df.hvplot.scatter(x='x', y='y', datashade=True, height=300, width=400) )
map_plot
linked_plots = link_selections(map_plot + hist_plot)
linked_plots.cols(1)
It would be nice to add a bar plot with counts per ship type but bar plots are currently not supported, see http://holoviews.org/user_guide/Linked_Brushing.html
bar_plot = df.groupby('Ship type').agg({'SOG':'count'}).rename(columns={'SOG':'count'}).hvplot.barh(width=400, height=400)
bar_plot