-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathmap.js
394 lines (325 loc) · 12.3 KB
/
map.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import leaflet from 'leaflet';
import leafletImage from 'leaflet-image';
import 'leaflet-providers';
import 'leaflet-easybutton';
import * as ui from './ui';
// Los Angeles is the center of the universe
const INIT_COORDS = [34.0522, -118.243];
const DEFAULT_OPTIONS = {
theme: 'CartoDB.DarkMatter',
lineOptions: {
color: '#0CB1E8',
weight: 1,
opacity: 0.5,
smoothFactor: 1,
overrideExisting: true,
detectColors: true,
},
markerOptions: {
color: '#00FF00',
weight: 3,
radius: 5,
opacity: 0.5
}
};
export default class GpxMap {
constructor(options) {
this.options = options || DEFAULT_OPTIONS;
this.tracks = [];
this.filters = {
minDate: null,
maxDate: null,
};
this.imageMarkers = [];
this.map = leaflet.map('background-map', {
center: INIT_COORDS,
zoom: 10,
preferCanvas: true,
});
leaflet.easyButton({
type: 'animate',
states: [{
icon: 'fa-camera fa-lg',
stateName: 'default',
title: 'Export as png',
onClick: () => {
let modal = ui.showModal('exportImage')
.afterClose(() => modal.destroy());
document.getElementById('render-export').onclick = (e) => {
e.preventDefault();
let output = document.getElementById('export-output');
output.innerHTML = 'Rendering <i class="fa fa-cog fa-spin"></i>';
let form = document.getElementById('export-settings').elements;
this.screenshot(form.format.value, output);
};
}
}]
}).addTo(this.map);
leaflet.easyButton({
type: 'animate',
states: [{
icon: 'fa-sliders fa-lg',
stateName: 'default',
title: 'Open settings dialog',
onClick: () => {
ui.buildSettingsModal(this.tracks, this.options, (opts) => {
this.updateOptions(opts);
this.saveOptions(opts);
}).show();
},
}],
}).addTo(this.map);
leaflet.easyButton({
type: 'animate',
states: [{
icon: 'fa-filter fa-lg',
stateName: 'default',
title: 'Filter displayed tracks',
onClick: () => {
ui.buildFilterModal(this.tracks, this.filters, (f) => {
this.filters = f;
this.applyFilters();
}).show();
}
}]
}).addTo(this.map);
this.viewAll = leaflet.easyButton({
type: 'animate',
states: [{
icon: 'fa-map fa-lg',
stateName: 'default',
title: 'Zoom to all tracks',
onClick: () => {
this.center();
},
}],
}).addTo(this.map);
this.markScrolled = () => {
this.map.removeEventListener('movestart', this.markScrolled);
this.scrolled = true;
};
this.clearScroll();
this.viewAll.disable();
this.switchTheme(this.options.theme);
this.requestBrowserLocation();
}
clearScroll() {
this.scrolled = false;
this.map.addEventListener('movestart', this.markScrolled);
}
switchTheme(themeName) {
if (this.mapTiles) {
this.mapTiles.removeFrom(this.map);
}
if (themeName !== 'No map') {
this.mapTiles = leaflet.tileLayer.provider(themeName);
this.mapTiles.addTo(this.map, {detectRetina: true});
}
}
saveOptions(opts) {
window.localStorage.setItem('options', JSON.stringify(opts));
}
restoreSavedOptions() {
if (window.localStorage.getItem('options') === null) {
return;
}
let opts = window.localStorage.getItem('options');
opts = JSON.parse(opts);
if (typeof opts === 'object') {
this.updateOptions(opts);
}
}
updateOptions(opts) {
if (opts.theme !== this.options.theme) {
this.switchTheme(opts.theme);
}
if (opts.lineOptions.overrideExisting) {
this.tracks.forEach(({line}) => {
line.setStyle({
color: opts.lineOptions.color,
weight: opts.lineOptions.weight,
opacity: opts.lineOptions.opacity,
});
line.redraw();
});
let markerOptions = opts.markerOptions;
this.imageMarkers.forEach(i => {
i.setStyle({
color: markerOptions.color,
weight: markerOptions.weight,
opacity: markerOptions.opacity,
radius: markerOptions.radius
});
i.redraw();
});
}
this.options = opts;
}
applyFilters() {
const dateBounds = {
min: new Date(this.filters.minDate || '1900/01/01'),
max: new Date(this.filters.maxDate || '2500/01/01'),
};
// NOTE: Tracks that don't have an associated timestamp will never be
// excluded.
const filters = [
(t) => t.timestamp && dateBounds.min > t.timestamp,
(t) => t.timestamp && dateBounds.max < t.timestamp,
];
for (let track of this.tracks) {
let hideTrack = filters.some(f => f(track));
if (hideTrack && track.visible) {
track.line.remove();
} else if (!hideTrack && !track.visible){
track.line.addTo(this.map);
}
track.visible = !hideTrack;
}
}
// Try to pull geo location from browser and center the map
requestBrowserLocation() {
navigator.geolocation.getCurrentPosition(pos => {
if (!this.scrolled && this.tracks.length === 0) {
this.map.panTo([pos.coords.latitude, pos.coords.longitude], {
noMoveStart: true,
animate: false,
});
// noMoveStart doesn't seem to have an effect, see Leaflet
// issue: https://github.com/Leaflet/Leaflet/issues/5396
this.clearScroll();
}
});
}
addTrack(track) {
this.viewAll.enable();
let lineOptions = Object.assign({}, this.options.lineOptions);
if (lineOptions.detectColors) {
if (/-(Hike|Walk)\.gpx/.test(track.filename)) {
lineOptions.color = '#ffc0cb';
} else if (/-Run\.gpx/.test(track.filename)) {
lineOptions.color = '#ff0000';
} else if (/-Ride\.gpx/.test(track.filename)) {
lineOptions.color = '#00ffff';
}
}
let line = leaflet.polyline(track.points, lineOptions);
line.addTo(this.map);
this.tracks.push(Object.assign({line, visible: true}, track));
}
async markerClick(image) {
const latitude = await image.latitude();
const longitude = await image.longitude();
const imageData = await image.getImageData();
let latlng = leaflet.latLng(latitude, longitude);
leaflet.popup({minWidth: 512})
.setLatLng(latlng)
.setContent(`<img src="${imageData}" width="512" height="100%">`)
.addTo(this.map);
}
async addImage(image) {
const lat = await image.latitude();
const lng = await image.longitude();
let latlng = leaflet.latLng(lat, lng);
let markerOptions = Object.assign({}, this.options.markerOptions);
let marker = leaflet.circleMarker(latlng, markerOptions)
.on('click', () => {
this.markerClick(image);
})
.addTo(this.map);
this.imageMarkers.push(marker);
}
// Center the map if the user has not yet manually panned the map
recenter() {
if (!this.scrolled) {
this.center();
}
}
center() {
// If there are no tracks, then don't try to get the bounds, as there
// would be an error
if (this.tracks.length === 0 && this.imageMarkers.length === 0) {
return;
}
let tracksAndImages = this.tracks.map(t => t.line)
.concat(this.imageMarkers);
this.map.fitBounds((new leaflet.featureGroup(tracksAndImages)).getBounds(), {
noMoveStart: true,
animate: false,
padding: [50, 20],
});
if (!this.scrolled) {
this.clearScroll();
}
}
screenshot(format, domNode) {
leafletImage(this.map, (err, canvas) => {
if (err) {
return window.alert(err);
}
let link = document.createElement('a');
if (format === 'png') {
link.download = 'derive-export.png';
link.innerText = 'Download as PNG';
canvas.toBlob(blob => {
link.href = URL.createObjectURL(blob);
domNode.innerText = '';
domNode.appendChild(link);
});
} else if (format === 'svg') {
link.innerText = 'Download as SVG';
const scale = 2;
const bounds = this.map.getPixelBounds();
bounds.min = bounds.min.multiplyBy(scale);
bounds.max = bounds.max.multiplyBy(scale);
const left = bounds.min.x;
const top = bounds.min.y;
const width = bounds.getSize().x;
const height = bounds.getSize().y;
let svg = leaflet.SVG.create('svg');
let root = leaflet.SVG.create('g');
svg.setAttribute('viewBox', `${left} ${top} ${width} ${height}`);
this.tracks.forEach(track => {
// Project each point from LatLng, scale it up, round to
// nearest 1/10 (by multiplying by 10, rounding and
// dividing), and reducing by removing duplicates (when two
// consecutive points have rounded to the same value)
let pts = track.points.map(ll =>
this.map.project(ll)
.multiplyBy(scale*10)
.round()
.divideBy(10)
).reduce((acc,next) => {
if (acc.length === 0 ||
acc[acc.length-1].x !== next.x ||
acc[acc.length-1].y !== next.y) {
acc.push(next);
}
return acc;
}, []);
// If none of the points on the track are on the screen,
// don't export the track
if (!pts.some(pt => bounds.contains(pt))) {
return;
}
let path = leaflet.SVG.pointsToPath([pts], false);
let el = leaflet.SVG.create('path');
el.setAttribute('stroke', track.line.options.color);
el.setAttribute('stroke-opacity', track.line.options.opacity);
el.setAttribute('stroke-width', scale * track.line.options.weight);
el.setAttribute('stroke-linecap', 'round');
el.setAttribute('stroke-linejoin', 'round');
el.setAttribute('fill', 'none');
el.setAttribute('d', path);
root.appendChild(el);
});
svg.appendChild(root);
let xml = (new XMLSerializer()).serializeToString(svg);
link.download = 'derive-export.svg';
let blob = new Blob([xml], {type: 'application/octet-stream'});
link.href = URL.createObjectURL(blob);
domNode.innerText = '';
domNode.appendChild(link);
}
});
}
}