-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.js
332 lines (288 loc) · 8.13 KB
/
editor.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var SEGMENT_COLORS = [
'#e41a1c',
'#377eb8',
'#984ea3',
'#ff7f00',
'#a65628'
];
function updateSegmentAttributes(segment) {
segment.distance = 0;
segment.enabled = true;
segment.maxSpeed = 0;
var i = 1;
var len = segment.length;
for (; i < len; i += 1) {
var pt = segment[i];
segment.distance += pt.distance;
segment.maxSpeed = Math.max(segment.maxSpeed, pt.speed);
}
segment.duration = segment[len - 1].time - segment[0].time;
segment.avgSpeed = segment.distance / segment.duration;
// Those properties will be only present when splitting segments
delete segment[0].distance;
delete segment[0].speed;
}
function extractSegments(pts, maxDuration, maxDistance) {
var segments = [];
var segment = [pts[0]];
var i = 0;
var j = 1;
var len = pts.length;
for (; j < len; i += 1, j += 1) {
var pt1 = pts[i];
var pt2 = pts[j];
var duration = pt2.time - pt1.time;
var distance = calcDistance(pt1, pt2);
if (duration < maxDuration && distance < maxDistance) {
pt2.distance = distance;
pt2.speed = distance / duration;
segment.push(pt2);
} else {
if (segment.length > 3) {
segments.push(segment);
}
segment = [pt2];
}
}
if (segment.length > 3) {
segments.push(segment);
}
segments.forEach(updateSegmentAttributes);
return segments;
}
function fitMap(map, segments) {
var points = segments.reduce(function (points, segment) {
if (segment.enabled) {
return points.concat(segment);
}
return points;
}, []);
map.fitBounds([
[
Math.min.apply(Math, points.map(function (pt) {
return pt.lat;
})),
Math.min.apply(Math, points.map(function (pt) {
return pt.lon;
}))
],
[
Math.max.apply(Math, points.map(function (pt) {
return pt.lat;
})),
Math.max.apply(Math, points.map(function (pt) {
return pt.lon;
}))
]
]);
}
function convertToLatLon(pt) {
return [pt.lat, pt.lon];
}
function drawMap(elementId, segments) {
var $mapEl = $(document.getElementById(elementId));
//$mapEl.height($mapEl.height());
var map = L.map(elementId);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 18
}).addTo(map);
var currentColor = -1;
segments.forEach(function (segment, segmentIndex) {
currentColor += 1;
currentColor %= SEGMENT_COLORS.length;
if (segment.enabled) {
var line = L.polyline(segment.map(convertToLatLon), {
color: SEGMENT_COLORS[currentColor],
weight: 6
}).addTo(map);
line.on('click', function (ev) {
deselectSegment();
selectSegment(segmentIndex);
L.DomEvent.stopPropagation(ev);
});
}
});
fitMap(map, segments);
map.on('click', deselectSegment);
return map;
}
function getDisplayedSpeed(speed, format) {
var value = speed * 3.6;
return format ? value.toFixed(1) + ' km/h' : value;
}
function getDisplayedDuration(duration) {
return [Math.ceil(duration / 60), duration % 60].join(':');
}
function genChartData(segments) {
var len = segments.length;
var data = new google.visualization.DataTable();
data.addColumn('date', 'Time');
var rowId = 0;
segments.forEach(function (segment, segmentIndex) {
var colId = segmentIndex + 1;
data.addColumn('number', 'Segment #' + colId);
if (segment.enabled) {
for (var i = 1, segmentLen = segment.length; i < segmentLen; i += 1) {
data.addRows(1);
data.setCell(rowId, 0, new Date(segment[i].time * 1000));
data.setCell(rowId, colId, getDisplayedSpeed(segment[i].speed));
rowId += 1;
}
}
});
return data;
}
function getPosition(segments, row) {
row += 1;
var segmentId = 0;
var pointId;
while (true) {
var segment = segments[segmentId];
if (segment.enabled) {
if (row < segment.length) {
pointId = row;
break;
}
row -= segment.length - 1;
}
segmentId += 1;
}
var point = JSON.parse(JSON.stringify(segments[segmentId][pointId]));
point.segmentId = segmentId;
point.pointId = pointId;
return point;
}
function drawChart(elementId, segments, map) {
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawBasic);
function drawBasic() {
var data = genChartData(segments);
var options = {
colors: SEGMENT_COLORS,
crosshair: {
orientation: 'vertical',
trigger: 'focus'
},
curveType: 'function',
focusTarget: 'category',
legend: {
position: 'none'
},
vAxis: {
title: 'Speed (km/h)',
viewWindow: {
min: 0
}
}
};
var chart = new google.visualization.LineChart(document.getElementById(elementId));
function selectSplitPoint() {
deselectSegment();
var point = chart.getSelection()[0];
if (!point) {
return;
}
var position = getPosition(segments, point.row);
selectSegment(position.segmentId);
var $a = $('<a href="#split">Split segment here</a>');
$a.attr('data-segment-id', position.segmentId);
$a.attr('data-point-id', position.pointId);
L.popup().setLatLng([position.lat, position.lon])
.setContent($('<p></p>').html($a)[0].outerHTML)
.openOn(map);
}
chart.draw(data, options);
google.visualization.events.addListener(chart, 'onmouseover', function (point) {
if (!point) {
return;
}
var position = getPosition(segments, point.row);
L.popup().setLatLng([position.lat, position.lon])
.setContent('<p>Here</p>')
.openOn(map);
});
google.visualization.events.addListener(chart, 'select', selectSplitPoint);
$(document.getElementById(elementId)).on('mouseleave', selectSplitPoint);
$(window).on('resize', function () {
chart.draw(data, options);
});
}
}
function deselectSegment() {
$('#split-table tr').removeClass('is-selected');
}
function selectSegment(segmentIndex) {
$('#split-table tr').eq(segmentIndex + 1).addClass('is-selected');
}
function drawTable(elementId, segments) {
var $tbody = $('tbody', document.getElementById(elementId));
$tbody.html('');
var currentColor = 0;
segments.forEach(function (segment, index) {
var $marker = $('<td><span class="marker"></span></td>');
$marker.find('.marker').css('border-color', SEGMENT_COLORS[currentColor]);
$tbody.append($('<tr data-toggle-segment></tr>')
.attr('data-segment-id', index)
.toggleClass('is-enabled', segment.enabled)
.append($marker)
.append('<td>' + segment.distance.toFixed(0) + ' m</td>')
.append('<td>' + getDisplayedDuration(segment.duration) + '</td>')
.append('<td>' + getDisplayedSpeed(segment.maxSpeed, true) + '</td>')
.append('<td>' + getDisplayedSpeed(segment.avgSpeed, true) + '</td>')
);
currentColor += 1;
currentColor %= SEGMENT_COLORS.length;
});
}
function generateDataLink(data) {
return 'data:application/gpx+xml;charset=utf-8,' + encodeURIComponent(data);
}
function initUI(gpxInput) {
$('#input').hide();
$('#editor').show();
var points = parseGpx(gpxInput);
var segments = extractSegments(points, 120, 500);
var map;
function refreshUi() {
if (map) {
map.remove();
}
drawTable('split-table', segments);
map = drawMap('lf-map', segments);
drawChart('g-chart', segments, map);
$('#download-gpx').attr('href', generateDataLink(updateGpx(gpxInput, segments)));
}
$('body').on('click', '[data-toggle-segment]', function () {
var $this = $(this);
if ($('tr.is-enabled').length < 2 && $this.hasClass('is-enabled')) {
return;
}
var segmentId = $this.attr('data-segment-id');
$this.toggleClass('is-enabled');
segments[segmentId].enabled = $this.hasClass('is-enabled');
refreshUi();
});
$('body').on('click', '[href="#split"]', function (ev) {
ev.preventDefault();
var $this = $(this);
var segmentId = $this.attr('data-segment-id');
var pointId = $this.attr('data-point-id');
var segmentToSplit = segments[segmentId];
var splitSegmentLeft = segmentToSplit.slice(0, pointId);
var splitSegmentRight = segmentToSplit.slice(pointId);
updateSegmentAttributes(splitSegmentLeft);
updateSegmentAttributes(splitSegmentRight);
segments.splice(segmentId, 1, splitSegmentLeft, splitSegmentRight);
refreshUi();
});
refreshUi();
}
$('#input-gpx').on('change', function (ev) {
var file = ev.target.files[0];
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function () {
initUI(reader.result);
};
});