# Speed Test -- Trajectory Plotting
import numpy as np
import pandas as pd
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import movingpandas as mpd
from holoviews import opts, dim
gdf = read_file('../data/geolife_small.gpkg')
runtimes={}
%%time
t0 = datetime.now()
gdf.plot()
runtime = datetime.now()-t0
runtimes['GeoDataFrame.plot'] = runtime
print(runtime)
traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
%%time
t0 = datetime.now()
traj_collection.plot()
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.plot'] = runtime
print(runtime)
%%time
t0 = datetime.now()
traj_collection.hvplot(line_width=7)
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.hvplot'] = runtime
print(runtime)
%%time
t0 = datetime.now()
traj_collection.hvplot(line_width=7, frame_width=300, frame_height=300)
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.hvplot (smaller)'] = runtime
print(runtime)
%%time
generalized_traj = mpd.DouglasPeuckerGeneralizer(traj_collection).generalize(tolerance=0.01)
t0 = datetime.now()
generalized_traj.hvplot(line_width=7)
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.hvplot (generalized)'] = runtime
print(runtime)
%%time
t0 = datetime.now()
gdf.hvplot(geo=True, tiles='OSM')
runtime = datetime.now()-t0
runtimes['GeoDataFrame.hvplot'] = runtime
print(runtime)
%%time
line_gdf = traj_collection.to_line_gdf()
t0 = datetime.now()
line_gdf.hvplot(geo=True, tiles='OSM', line_width=7)
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.to_line_gdf.hvplot'] = runtime
print(runtime)
%%time
line_gdf = traj_collection.to_line_gdf()
t0 = datetime.now()
line_gdf.hvplot()
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.to_line_gdf.hvplot (no basemap)'] = runtime
print(runtime)
%%time
traj_gdf = traj_collection.to_traj_gdf()
t0 = datetime.now()
traj_gdf.hvplot(geo=True, tiles='OSM', line_width=7)
runtime = datetime.now()-t0
runtimes['TrajectoryCollection.to_traj_gdf.hvplot'] = runtime
print(runtime)
for key, value in sorted(runtimes.items()):
print(f'{key}: {value}')
result = pd.DataFrame.from_dict(runtimes, orient='index', columns=['runtime'])
result['seconds'] = result.runtime.dt.total_seconds()
result
result.sort_values('seconds').hvplot.barh(y='seconds', title='Runtimes in seconds')
import geopandas
print(f'GeoPandas {geopandas.__version__}')
import geoviews
print(f'Geoviews {geoviews.__version__}')
import cartopy
print(f'Cartopy {cartopy.__version__}')