-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpxDraw.py
executable file
·69 lines (59 loc) · 3.08 KB
/
GpxDraw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
from math import floor
import gpxpy
import click
import os
import html
import datetime
@click.command()
@click.option("--output", default="map", help="Specify the name of the output file. Defaults to `map`")
@click.option("--input", default="gpx", help="Specify an input folder. Defaults to `gpx`")
def main(output, input):
load_points_and_polygon(input)
def format_time(time_s: float) -> str:
if not time_s:
return 'n/a'
else:
minutes = floor(time_s / 60.)
hours = floor(minutes / 60.)
return '%s:%s:%s' % (str(int(hours)).zfill(2), str(int(minutes % 60)).zfill(2), str(int(time_s % 60)).zfill(2))
def load_points_and_polygon(folder):
"""Loads all gpx files into a list of points"""
print ("Loading files...")
with open('polylines.js', 'w') as outputfile:
outputfile.write('function addLines(map) {')
with click.progressbar(os.listdir(folder)) as bar:
for filename in bar:
if filename.endswith(".gpx"):
gpx_file = open(f'{folder}/' + filename)
try:
color='#3388ff'
gpx = gpxpy.parse(gpx_file)
if gpx.time < datetime.datetime(2019, 8, 1, 0, 0, 0, 0, datetime.timezone.utc):
gpx_file.close()
os.rename(f'{folder}/' + filename, f'{folder}/archive/' + filename)
else:
tooltip = filename + ' - ' + gpx.time.strftime('%x')
polygon="L.polyline(["
for track in gpx.tracks:
if track.name:
tooltip += ' - ' + track.name #+ ' (' + track.type + ')'
try:
moving_data = track.get_moving_data()
if moving_data:
tooltip += ' - duration:' + format_time(moving_data.moving_time)
except :
pass
if track.type == 'Ride' or track.type == '1':
color='#ff22ff'
for segment in track.segments:
for point in segment.points:
polygon += "["+str(point.latitude)+","+str(point.longitude)+"],"
polygon = polygon[:-1] + "],{color:'" + color + "',weight:2}).bindTooltip('" + html.escape(tooltip) + "').on('mouseover',function(e){e.target.setStyle({color:'red'});}).on('mouseout',function(e){e.target.setStyle({color:'" + color + "'});}).addTo(map);"
outputfile.write(polygon)
except :
print (f"Failed to load {filename} : ", sys.exc_info()[0])
outputfile.write('}')
print ("Done...")
if __name__ == '__main__':
main()