/
gpxToLeaflet.py
184 lines (165 loc) · 6.9 KB
/
gpxToLeaflet.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
from dateutil import tz
from datetime import timedelta
import gpxpy
import geopy.distance
class DistanceMarker:
def __init__(self, latitude, longitude, label):
self.latitude = latitude
self.longitude = longitude
self.label = label
def toJsString(self):
return f"""
L.marker([{self.latitude},{self.longitude}], {{
icon: L.divIcon({{ html: '<span style="font-size: 20px; font-weight: bold">{self.label}</span>' }})
}}).addTo(myMap);"""
class Track:
def __init__(self, track, distance, startTime, endTime, distanceMarkers=[]):
self.track = track
self.distance = distance
self.startTime = startTime
self.endTime = endTime
self.distanceMarkers = distanceMarkers
def duration(self):
if self.startTime != "" and self.endTime != "":
return self.endTime - self.startTime
else:
return timedelta(0)
def durationToStr(self):
duration = self.duration()
hours, remainder = divmod(duration.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
def distanceMarkersToJsStr(self):
return "\n".join(map(lambda marker: marker.toJsString(), self.distanceMarkers))
def main(gpxFilename, htmlFilename) -> None:
track: Track = load_track(gpxFilename)
if(track != None and len(track.track) > 0):
generate_html(track, htmlFilename)
print("Done generating html page: ", htmlFilename)
def load_track(filename: str) -> Track:
if(os.path.exists(filename) == False):
print(f"File not found: {filename}")
return None
localtime = tz.tzlocal()
gpx_file = open(filename)
current_track = Track([], 0, "", "")
try:
gpx = gpxpy.parse(gpx_file)
prevPoint = (0, 0)
prevDistance = 0
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
current_track.track.append([float(point.latitude), float(point.longitude)])
if current_track.startTime == "":
current_track.startTime = point.time.astimezone(localtime)
current_track.endTime = point.time.astimezone(localtime)
if prevPoint != (0, 0):
pointDistance = geopy.distance.distance(prevPoint, (float(point.latitude), float(point.longitude))).km
current_track.distance = current_track.distance + pointDistance
if(int(current_track.distance) > prevDistance):
prevDistance = int(current_track.distance)
newDistanceMarker = DistanceMarker(point.latitude, point.longitude, f"{int(prevDistance)}km")
current_track.distanceMarkers.append(newDistanceMarker)
prevPoint = (float(point.latitude), float(point.longitude))
except Exception as error:
print(f"\nParsing file '{filename}' failed. Error: {error}")
current_track = None
gpx_file.close()
return(current_track)
def generate_html(track: Track, file_out: str) -> None:
"""Generates a new html file with points"""
template = """
<html><head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
<style type="text/css">
#mapId {
position: absolute;
top: 0px;
width: 800px;
left: 0px;
height: 800px;
border: 1px solid #000;
}
#info {
position: absolute;
top: 0px;
left: 805px;
border: 1px solid #000;
background-color: #ddd;
font-size: larger;
padding: 5px;
}
</style>
</head>
<body>
<div id="mapId"></div>
<div id="info">
<h1>Track info</h1>
<div id="duration"></div>
<div id="distance"></div>
</div>
<script>
var myMap = L.map('mapId').setView([55.641, 12.47], 13);
L.tileLayer(
'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
}).addTo(myMap);
var track = [];
var duration = '';
var distance = '';
const polyline = L.polyline(track, {color: 'blue'}).addTo(myMap);
var highlightMarker = undefined;
polyline.on('click', function(e) {
const closestIdx = findPointIdxOnPolyline(track, e.latlng);
const trackLength = lengthOfTrack(track, closestIdx);
if(highlightMarker != undefined) {
myMap.removeLayer(highlightMarker);
}
highlightMarker = L.marker(track[closestIdx], {icon: L.divIcon({ html: '<span style="font-size: 20px; font-weight: bold">' + Math.round(trackLength) + 'm</span>' })}).addTo(myMap);
});
function findPointIdxOnPolyline(track, latlng) {
let prevDiffLat = prevDiffLng = 99999;
let closestIdx = 0;
for(var i = 0; i < track.length; i++) {
var diffLat = Math.abs(track[i][0] - latlng.lat);
var diffLng = Math.abs(track[i][1] - latlng.lng);
if(diffLat + diffLng < prevDiffLat + prevDiffLng) {
prevDiffLat = diffLat;
prevDiffLng = diffLng;
closestIdx = i;
}
}
return closestIdx;
}
function lengthOfTrack(track, idx) {
var length = 0;
for(var i = 1; i < track.length && i < idx; i++) {
let from = L.latLng(track[i-1]);
let to = L.latLng(track[i]);
length += from.distanceTo(to);
}
return length;
}
<!--DISTANCEMARKERS-->
</script>
</body></html>
"""
track_points = ",".join([f"[{g_track_point[0]}, {g_track_point[1]}, 0.1]" for g_track_point in track.track])
track_points = f"var track=[{track_points}];"
template = template.replace("var track = [];", track_points)
template = template.replace('<div id="duration"></div>', '<div id="duration">Duration: ' + track.durationToStr() + '</div>')
template = template.replace('<div id="distance"></div>', '<div id="distance">Distance: ' + str(round(track.distance, 2)) + ' km</div>')
template = template.replace('<!--DISTANCEMARKERS-->', track.distanceMarkersToJsStr())
f = open(file_out, "w")
f.write(template)
f.close()
if __name__ == '__main__':
main("myTrack.gpx", "myTrack.html")