2018-07-26 07:53:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import datetime
|
2018-09-05 21:39:54 +00:00
|
|
|
import glob
|
2018-07-26 07:53:44 +00:00
|
|
|
import sys
|
|
|
|
import gpx_parser
|
|
|
|
import matplotlib
|
|
|
|
matplotlib.use('Agg')
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy
|
|
|
|
import os
|
|
|
|
import pandas as pd
|
|
|
|
import collections
|
2018-09-05 21:39:54 +00:00
|
|
|
from gpx_parser import Tracks
|
2019-08-08 12:25:19 +00:00
|
|
|
import pytz
|
2018-09-05 21:39:54 +00:00
|
|
|
|
|
|
|
MONTH_LABELS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
|
|
|
|
|
2018-07-26 07:53:44 +00:00
|
|
|
|
|
|
|
def plot_bar_chart(labels, ticklabels, values, title, xlabel, ylabel, filename, xtick_rotation=0):
|
|
|
|
fig = plt.figure()
|
|
|
|
ax1 = fig.add_subplot(111)
|
|
|
|
ax1.grid(zorder=0)
|
|
|
|
ax1.spines["top"].set_visible(False)
|
|
|
|
ax1.spines["bottom"].set_visible(False)
|
|
|
|
ax1.spines["left"].set_visible(False)
|
|
|
|
ax1.spines["right"].set_visible(False)
|
|
|
|
|
|
|
|
plt.title(title)
|
|
|
|
plt.xlabel(xlabel)
|
|
|
|
plt.ylabel(ylabel)
|
|
|
|
|
|
|
|
width = 1.0 / len(values) - 0.03
|
|
|
|
x_base = numpy.arange(len(ticklabels))
|
|
|
|
x_pos = list()
|
|
|
|
|
|
|
|
for i in range(len(values)):
|
|
|
|
x_pos.append([x + (width / 2) + i * width for x in range(len(x_base))])
|
|
|
|
plt.bar(x_pos[i], values[i], width=width, label=labels[i], zorder=2)
|
|
|
|
|
|
|
|
plt.xticks(x_base, ticklabels, rotation=xtick_rotation)
|
|
|
|
|
|
|
|
# Tweak spacing to prevent clipping of tick-labels
|
|
|
|
plt.subplots_adjust(bottom=0.2)
|
|
|
|
|
|
|
|
plt.legend()
|
|
|
|
plt.savefig(filename)
|
2019-02-28 08:21:18 +00:00
|
|
|
plt.close('all')
|
2018-07-26 07:53:44 +00:00
|
|
|
|
2019-06-16 06:34:37 +00:00
|
|
|
|
|
|
|
def plot_line_chart(values, ticklabels, title, xlabel, ylabel, filename, xtick_rotation=0):
|
|
|
|
'''Plot a line chart.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
values (dict): key: line name
|
|
|
|
value (list): line values
|
2019-08-08 12:25:19 +00:00
|
|
|
ticklabels (list): Names for the tick labels.
|
2019-06-16 06:34:37 +00:00
|
|
|
title (str): Title of the chart.
|
|
|
|
'''
|
|
|
|
fig = plt.figure()
|
|
|
|
ax1 = fig.add_subplot(111)
|
|
|
|
ax1.grid(zorder=0)
|
|
|
|
ax1.spines["top"].set_visible(False)
|
|
|
|
ax1.spines["bottom"].set_visible(False)
|
|
|
|
ax1.spines["left"].set_visible(False)
|
|
|
|
ax1.spines["right"].set_visible(False)
|
|
|
|
|
2019-08-08 12:25:19 +00:00
|
|
|
step = len(values[min(values.keys())]) / len(ticklabels)
|
|
|
|
ax1.set_xticks(numpy.arange(1, len(ticklabels) * step, step))
|
|
|
|
ax1.set_xticklabels(ticklabels, rotation=xtick_rotation)
|
|
|
|
|
2019-06-16 06:34:37 +00:00
|
|
|
plt.title(title)
|
|
|
|
plt.xlabel(xlabel)
|
|
|
|
plt.ylabel(ylabel)
|
|
|
|
|
|
|
|
for key in values.keys():
|
2019-08-08 12:25:19 +00:00
|
|
|
plt.plot(values[key], label=key)
|
2019-06-16 06:34:37 +00:00
|
|
|
|
|
|
|
plt.legend()
|
|
|
|
plt.savefig(filename)
|
|
|
|
plt.close('all')
|
|
|
|
|
|
|
|
|
2019-08-08 12:25:19 +00:00
|
|
|
def generate_date_list(start_date, end_date):
|
|
|
|
r = (end_date + datetime.timedelta(days=1) - start_date).days
|
|
|
|
return[start_date + datetime.timedelta(days=i) for i in range(r)]
|
|
|
|
|
|
|
|
|
2018-07-26 07:53:44 +00:00
|
|
|
class Gpx2Html(object):
|
2018-09-05 21:39:54 +00:00
|
|
|
def __init__(self, infolder, outfolder, logger):
|
|
|
|
self.logger = logger
|
2018-07-26 07:53:44 +00:00
|
|
|
self.infolder = infolder
|
2018-07-26 12:20:15 +00:00
|
|
|
self.outfolder = os.path.abspath(outfolder)
|
|
|
|
|
2019-02-28 08:21:18 +00:00
|
|
|
if not os.path.exists(self.outfolder):
|
|
|
|
os.makedirs(self.outfolder)
|
|
|
|
|
2018-09-05 21:39:54 +00:00
|
|
|
self.tracks = Tracks(logger)
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
infiles = glob.glob(os.path.join(self.infolder, '*.gpx'))
|
|
|
|
for filename in infiles:
|
|
|
|
self.tracks.add(filename)
|
|
|
|
|
|
|
|
self.logger.info("Begin update of png's/html...")
|
|
|
|
distances = list()
|
|
|
|
avg_speeds = list()
|
2019-06-16 06:34:37 +00:00
|
|
|
distances_dict = dict()
|
2018-09-05 21:39:54 +00:00
|
|
|
for year in self.tracks.years():
|
|
|
|
distances.append(self.tracks.distances(year))
|
2019-06-16 06:34:37 +00:00
|
|
|
distances_dict[year] = self.tracks.distances(year)
|
2018-09-05 21:39:54 +00:00
|
|
|
avg_speeds.append(self.tracks.avg_speeds(year))
|
|
|
|
|
|
|
|
plot_bar_chart(self.tracks.years(), MONTH_LABELS, distances,
|
|
|
|
'Distance', 'Month', 'km',
|
|
|
|
os.path.join(self.outfolder, 'distance.png'))
|
|
|
|
|
|
|
|
plot_bar_chart(self.tracks.years(), MONTH_LABELS, avg_speeds,
|
|
|
|
'Average Speed', 'Month', 'km/h',
|
|
|
|
os.path.join(self.outfolder, 'avg_spd.png'))
|
|
|
|
|
2019-06-16 06:34:37 +00:00
|
|
|
# Accumulated distance:
|
2019-08-08 12:25:19 +00:00
|
|
|
accumulated_distance = dict()
|
|
|
|
for year in self.tracks.years():
|
|
|
|
acc_year_dist = list()
|
|
|
|
start_date = datetime.datetime(year, 1, 1)
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
end_date = datetime.datetime(year, 12, 31)
|
|
|
|
if year == now.year:
|
|
|
|
end_date = now
|
|
|
|
tracks = self.tracks.tracks(start_date, end_date)
|
|
|
|
for date in generate_date_list(start_date, end_date):
|
|
|
|
for track in tracks:
|
|
|
|
if track.start_time.month == date.month and track.start_time.day == date.day:
|
|
|
|
if len(acc_year_dist) < date.timetuple().tm_yday:
|
|
|
|
acc_year_dist.append(track.distance / 1000)
|
|
|
|
else:
|
|
|
|
acc_year_dist.append(track.distance / 1000 + acc_year_dist[-1])
|
|
|
|
elif len(acc_year_dist) < date.timetuple().tm_yday:
|
|
|
|
if len(acc_year_dist) > 0:
|
|
|
|
acc_year_dist.append(acc_year_dist[-1])
|
|
|
|
else:
|
|
|
|
acc_year_dist.append(0)
|
|
|
|
if len(acc_year_dist) == 365:
|
|
|
|
acc_year_dist.append(acc_year_dist[-1])
|
|
|
|
accumulated_distance[year] = acc_year_dist
|
|
|
|
plot_line_chart(accumulated_distance, MONTH_LABELS,
|
|
|
|
"Accumulated Distance", 'Month', 'km',
|
2019-06-16 06:34:37 +00:00
|
|
|
os.path.join(self.outfolder, 'acc_dist.png'))
|
|
|
|
|
2019-08-08 12:25:19 +00:00
|
|
|
|
|
|
|
|
2018-09-05 21:39:54 +00:00
|
|
|
end_date = datetime.datetime.today()
|
|
|
|
start_date = end_date - datetime.timedelta(days=14)
|
|
|
|
last_n_tracks = self.tracks.tracks(start_date, end_date)
|
|
|
|
last_n_distances = dict()
|
|
|
|
last_n_durations = dict()
|
|
|
|
dates = pd.date_range(start_date.date(), end_date.date())
|
|
|
|
for date in dates:
|
|
|
|
for track in last_n_tracks:
|
|
|
|
if date.date() == track.start_time.date():
|
|
|
|
get = 0
|
|
|
|
try:
|
|
|
|
get = last_n_distances[date.date()]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if get == 0:
|
|
|
|
last_n_distances[date.date()] = track.distance / 1000
|
|
|
|
else:
|
|
|
|
last_n_distances[date.date()] += track.distance / 1000
|
|
|
|
try:
|
|
|
|
get = last_n_durations[date.date()]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if get == 0:
|
|
|
|
last_n_durations[date.date()] = track.duration.total_seconds()
|
|
|
|
else:
|
|
|
|
last_n_durations[date.date()] += track.duration.total_seconds()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
get = last_n_distances[date.date()]
|
|
|
|
except KeyError:
|
|
|
|
last_n_distances[date.date()] = 0
|
|
|
|
try:
|
|
|
|
get = last_n_durations[date.date()]
|
|
|
|
except KeyError:
|
|
|
|
last_n_durations[date.date()] = 0
|
|
|
|
last_n_dist = list()
|
|
|
|
last_n_dur = list()
|
|
|
|
last_n_avg = list()
|
|
|
|
last_n_dates = list()
|
|
|
|
for date in dates:
|
|
|
|
try:
|
|
|
|
last_n_dist.append(last_n_distances[date.date()])
|
|
|
|
except KeyError:
|
|
|
|
last_n_dist.append(0)
|
|
|
|
try:
|
|
|
|
last_n_dur.append(last_n_durations[date.date()])
|
|
|
|
except KeyError:
|
|
|
|
last_n_dur.append(0)
|
|
|
|
date_str = "{0:04d}-{1:02d}-{2:02d}".format(date.year, date.month, date.day)
|
|
|
|
last_n_dates.append(date_str)
|
|
|
|
try:
|
|
|
|
if last_n_durations[date.date()] == 0:
|
|
|
|
last_n_avg.append(0)
|
|
|
|
else:
|
|
|
|
last_n_avg.append(last_n_distances[date.date()] /
|
|
|
|
(last_n_durations[date.date()] / 3600))
|
|
|
|
except KeyError:
|
|
|
|
last_n_avg.append(0)
|
2018-07-26 12:20:15 +00:00
|
|
|
|
2018-09-05 21:39:54 +00:00
|
|
|
plot_bar_chart(["Distance", "Average speed"], last_n_dates,
|
|
|
|
[last_n_dist, last_n_avg],
|
|
|
|
'Last 14 days', 'Date', 'km, km/h',
|
|
|
|
os.path.join(self.outfolder, 'last_14_days.png'), 90)
|
|
|
|
self.__write_html_file()
|
|
|
|
self.logger.info("End update of png's/html...")
|
2018-07-26 12:20:15 +00:00
|
|
|
|
2018-09-05 21:39:54 +00:00
|
|
|
def __write_html_file(self):
|
|
|
|
with open(os.path.join(self.outfolder, 'index.html'), 'w') as handle:
|
2018-07-26 12:20:15 +00:00
|
|
|
handle.write('<!DOCTYPE html>\n')
|
|
|
|
handle.write('<html>\n')
|
|
|
|
handle.write('<head>\n')
|
|
|
|
handle.write('<style>\n')
|
|
|
|
handle.write('table {\n')
|
|
|
|
handle.write(' border-collapse: separate;\n')
|
|
|
|
handle.write(' border-spacing: 20px 0;\n')
|
|
|
|
handle.write('}\n')
|
|
|
|
handle.write('th {\n')
|
|
|
|
handle.write(' text-align: left;\n')
|
|
|
|
handle.write('}\n')
|
|
|
|
handle.write('</style>\n')
|
|
|
|
handle.write('<title> Bicycle </title>\n')
|
|
|
|
handle.write('</head>\n')
|
|
|
|
handle.write('<body>\n')
|
|
|
|
handle.write('<center>\n')
|
|
|
|
handle.write('<h1> Bicycle </h1>\n')
|
|
|
|
handle.write('<p>\n')
|
|
|
|
|
|
|
|
handle.write('<table>\n')
|
|
|
|
handle.write('<tr>\n')
|
2018-09-05 21:39:54 +00:00
|
|
|
for year in self.tracks.years():
|
2018-07-26 12:20:15 +00:00
|
|
|
handle.write('<th>{}</th>\n'.format(year))
|
|
|
|
handle.write('</tr>\n')
|
|
|
|
|
|
|
|
handle.write('<tr>\n')
|
2018-09-05 21:39:54 +00:00
|
|
|
for year in self.tracks.years():
|
|
|
|
handle.write('<td>{} km</td>\n'.format(round(sum(self.tracks.distances(year)), 1)))
|
2018-07-26 12:20:15 +00:00
|
|
|
handle.write('</tr>\n')
|
|
|
|
handle.write('</table>\n')
|
|
|
|
|
|
|
|
handle.write('</p>\n')
|
|
|
|
|
|
|
|
handle.write('<p>\n')
|
2018-09-05 21:39:54 +00:00
|
|
|
handle.write('<IMG SRC="{}" ALT="Distance">\n'.format('distance.png'))
|
2019-06-16 06:34:37 +00:00
|
|
|
handle.write('<IMG SRC="{}" ALT="Distance">\n'.format('acc_dist.png'))
|
2018-09-05 21:39:54 +00:00
|
|
|
handle.write('<IMG SRC="{}" ALT="Distance">\n'.format('avg_spd.png'))
|
|
|
|
handle.write('<IMG SRC="{}" ALT="Distance">\n'.format('last_14_days.png'))
|
2018-07-26 12:20:15 +00:00
|
|
|
handle.write('</p>\n')
|
|
|
|
|
|
|
|
handle.write('</body>\n')
|
|
|
|
handle.write('<center>\n')
|
|
|
|
handle.write('</html>\n')
|