-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.html
422 lines (363 loc) · 14 KB
/
map.html
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>USA trip october 2018</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<!--
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.default.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.3/jquery.sumoselect.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.3/sumoselect.min.css">
<script src="https://joker-x.github.io/Leaflet.geoCSV/leaflet.geocsv-src.js"></script>
<script src="http://rawgit.com/turban/Leaflet.Photo/gh-pages/Leaflet.Photo.js"></script>
<link rel="stylesheet" href="http://rawgit.com/turban/Leaflet.Photo/gh-pages/Leaflet.Photo.css"/>
<link rel="stylesheet" href="https://rawgit.com/turban/Leaflet.Photo/gh-pages/examples/css/map.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet-src.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster-src.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css"/>
-->
<script src="lib/jquery-1.9.1.js"></script>
<script src="lib/leaflet-src.js?v=0.7.7"></script>
<link rel="stylesheet" href="lib/leaflet.css?v=0.7.7"/>
<script src="lib/leaflet.markercluster-src.js?v=0.4.0"></script>
<link rel="stylesheet" href="lib/MarkerCluster.css?v=0.4.0"/>
<script src="lib/Leaflet.Photo.js"></script>
<link rel="stylesheet" href="lib/Leaflet.Photo.css"/>
<link rel="stylesheet" href="lib/map.css"/>
<script src="lib/leaflet.geocsv-src.js"></script>
<script src="lib/jquery.sumoselect.js"></script>
<link rel="stylesheet" href="lib/sumoselect.css">
<style>
html,
body,
#map {
width: 97%;
height: 97%;
padding: 0px;
margin: 0px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #333;
}
.infopop h4 {
font: 14px/16px Arial, Helvetica, sans-serif;
font-weight: bold;
margin: 0 0 0px;
color: #777;
}
th.tg-0lax {
width: 18%;
}
#btnShowPrevious {
margin-left: 50pt;
}
</style>
</head>
<body>
<table class="tg">
<tr>
<th class="tg-0lax">
<select class="form-control" id="selDate"></select>
</th>
<th class="tg-0lax">
<select class="form-control" id="selState"></select>
</th>
<th class="tg-0lax">
<select class="form-control" id="selTown"></select>
</th>
<th class="tg-0lax">
<input type="button" value="Show photos & videos" id="btnShowPhotos" />
</th>
<th class="tg-0lax">
<input type="button" value="Show videos only" id="btnShowVideos" />
</th>
</tr>
</table>
<div id="map"></div>
<script>
const IMAGES_CSV = "map_markers_data.csv.dos";
// show button wrapper
function enableShowPhotosButton(state) {
const btn = $('#btnShowPhotos')[0];
btn.disabled = !state;
return btn;
}
function enableShowVideosButton(state) {
const btn = $('#btnShowVideos')[0];
btn.disabled = !state;
return btn;
}
enableShowPhotosButton(false);
enableShowVideosButton(false);
// photos filter UI
const filterAll = 'all';
const filterOptions = {placeholder: filterAll};
const filterSelectors = [
$('#selDate').SumoSelect(filterOptions),
$('#selState').SumoSelect(filterOptions),
$('#selTown').SumoSelect(filterOptions)
];
// global container for all images
var photos = [{
"lat": 34.04555452,
"lng": -118.3001509,
"name": "C.H. Johnston, 1951",
"ts": "2018:10:11 12:23:15.33",
"url": "http://41.media.tumblr.com/8dd3cc5931650a6a772db77f6fad797b/tumblr_n5c7uw79Yn1tq6tdto1_400.jpg",
"thumbnail": "http://41.media.tumblr.com/8dd3cc5931650a6a772db77f6fad797b/tumblr_n5c7uw79Yn1tq6tdto1_400.jpg"
}];
// collback after csv loaded and parsed;
// load data to filter + show UI panel
function setupFilter() {
console.log("setupFilter ...");
// get filter list items from array of photos (dates, states, towns)
const sets = getItemsSets(photos);
function setupSelect(sel, items, choice) {
// add items to select lists
updateSelect(sel, items, choice);
// setup select change callback
sel.sumo.E.on('change', null, sel.id, evt => {
console.log(`on change, sel.id: '${evt.data}', sel.placeholder: '${evt.target.sumo.placeholder}'`);
if(typeof $.updating === 'undefined' || $.updating == false) {
$.updating = true;
updateFilter(getFilter(filterSelectors));
$.updating = false;
}
});
}
[0, 1, 2].forEach(idx => setupSelect(
filterSelectors[idx],
sets[idx],
filterAll)
);
// setup show buttons
enableShowPhotosButton(true).onclick =
() => renderMarkersLayer(filterPhotos);
enableShowVideosButton(true).onclick =
() => renderMarkersLayer(filterVideos);
console.log("setupFilter done.");
}
// make 3 sorted sets from photos date,state,town
function getItemsSets(photos) {
const sets = [new Set(), new Set(), new Set()];
photos.forEach(function(photo) {
const parts = photo['name'].split(';');
[0, 1, 2].forEach(idx => sets[idx].add(parts[idx].trim()));
});
return sets.map(s => Array.from(s).sort())
}
// recreate select options
function updateSelect(sel, items, choice) {
// modified sel.sumo.removeAll();
function removeAll() {
const obj = sel.sumo;
const options = obj.E.find('option');
for (var x = (options.length - 1); x >= 0; x--) {
obj.remove(x);
}
}
// clear sel list
sel.sumo.unSelectAll();
removeAll();
// add items to select lists
sel.sumo.add(filterAll);
items.forEach(itm => sel.sumo.add(itm));
sel.sumo.selectItem(choice);
}
// recreate filter UI
function updateFilter(selected) {
console.log('updateFilter', selected);
const choices = [selected.date, selected.state, selected.town].map(itm => {
if(itm == '') return filterAll; else return itm;
});
console.log('updateFilter', choices);
const filteredPhotos = filterPhotos(photos, selected);
const allitems = getItemsSets(filteredPhotos);
[0, 1, 2].forEach(idx => {
const sel = filterSelectors[idx];
const items = allitems[idx];
const choice = choices[idx];
updateSelect(sel, items, choice);
});
}
// filter photos array using transformed UI input
function filterPhotos(arr, selected) {
const dst = [selected['date'], selected['state'], selected['town']];
function partOK(flt, itm) { return (flt == '' || flt == itm); }
return arr.filter(photo => {
const parts = photo['name'].split(';');
return [0, 1, 2].every(idx => partOK(dst[idx], parts[idx].trim()));
});
}
// filter only videos
function filterVideos(arr, selected) {
return filterPhotos(arr, selected)
.filter(photo => photo['url'].endsWith('.mp4'));
}
// transform UI data to filter data
function getFilter(sels) {
const fs = sels.map(s => s.sumo.placeholder.trim())
.map(s => {if(s == filterAll) return ''; else return s;});
return {
'date' : fs[0],
'state': fs[1],
'town' : fs[2]
}
}
// create photo object from parsed csv record
function newPhoto(feature) {
const coords = feature.geometry.coordinates;
const props = feature.properties;
const fname = props['sourcefile'].slice(37);
const photo = {
'lat': coords[1],
'lng': coords[0],
'name': props['compositesubsecdatetimeoriginal'].slice(0, 10) + ';' +
props['state'] + ';' +
props['town'],
'ts': props['compositesubsecdatetimeoriginal'],
'url': fname,
'thumbnail': 'thumb/thumb-' + fname.split('/')[1].split('.')[0] + '.jpg'
// usa-vid/VID_20181005_152619.mp4 => thumb/thumb-VID_20181005_152619.jpg
};
return photo;
}
// const currIdx = findPhoto(popupLayer.photo);
function findPhoto(photo) {
const idx = photos.findIndex(p => p['ts'] == photo['ts']);
if (idx < 0) return 0;
return idx;
}
// callback after csv loaded to window
var csvlayer = '';
function processCSV(csvtext) {
csvlayer = L.geoCsv(csvtext, {
firstLineTitles: true,
fieldSeparator: ',',
latitudeTitle: 'Composite:GPSLatitude',
longitudeTitle: 'Composite:GPSLongitude',
lineSeparator: '\r\n',
quoteSymbol: '"'
});
console.assert(Object.keys(csvlayer._layers).length == 2691, 'unexpected num of parsed csv lines');
// create global array of photos
photos = Object.keys(csvlayer._layers).map(k =>
newPhoto(csvlayer._layers[k].feature)
).sort(function(a, b){
if(a['ts'] < b['ts']) return -1;
if(a['ts'] > b['ts']) return 1;
return 0;
});
}
// prev/next functionality
var popupLayer = '';
var popupPhoto = '';
function photoLayerOnClick(evt) {
popupLayer = evt.layer;
popupPhoto = popupLayer.photo;
_photoLayerOnClick(popupLayer, popupPhoto);
}
function _photoLayerOnClick(layer, photo) {
const prevnext = '<input type="button" value="prev" id="btnShowPrevious" onclick="showPrevNext(-1)" />' +
'<input type="button" value="next" id="btnShowNext" onclick="showPrevNext(1)" />';
var template = '<a href="{url}"><img src="{url}"/></a>' +
'<p>{name}' + prevnext + '</p>';
if (photo.url.endsWith('.mp4') && (!!document.createElement('video').canPlayType(
'video/mp4; codecs=avc1.42E01E,mp4a.40.2'))) {
template = '<video autoplay loop controls poster="{thumbnail}" ' +
' width="600" height="600"> ' +
' <source src="{url}" type="video/mp4"/></video> ' +
' <p>{name} <a href="{url}"> mp4 </a>' + prevnext + '</p>';
}
layer.bindPopup(L.Util.template(template, photo), {
className: 'leaflet-popup-photo',
minWidth: 600
}).openPopup();
}
function showPrevNext(incr) {
const idx = Math.abs(findPhoto(popupPhoto) + incr) % photos.length;
popupPhoto = photos[idx];
_photoLayerOnClick(popupLayer, popupPhoto);
}
// add filtered photos to map
function renderMarkersLayer(filterFunc) {
photoLayer.clear();
const filter = getFilter(filterSelectors);
const filtered = filterFunc.call(this, photos, filter);
console.log("filtered: ", filtered.length);
photoLayer.add(filtered);
map.fitBounds(photoLayer.getBounds());
}
// map setup
// set center coordinates
const center = [40, -34];
// set default zoom level
const zoomLevel = 3;
// initialize map
const map = L.map('map').setView(center, zoomLevel);
// set source for map tiles
const cdbAttr = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'© <a href="http://cartodb.com/attributions">CartoDB</a>';
const cdbUrl = 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
const mbAttr = '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>';
const mbUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
// create base layers
const grayscaleCDB = L.tileLayer(cdbUrl, {attribution: cdbAttr});
const grayscaleMBX = L.tileLayer(mbUrl, {id: 'mapbox.light', attribution: mbAttr});
const streetsMBX = L.tileLayer(mbUrl, {id: 'mapbox.streets', attribution: mbAttr});
const osmCLR = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
const osmBW = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
const otmCLR = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
maxZoom: 17,
attribution: 'Map data: © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
});
const thunderforestOTD = L.tileLayer('https://{s}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey={apikey}', {
attribution: '© <a href="http://www.thunderforest.com/">Thunderforest</a>, © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
apikey: 'db5ae1f5778a448ca662554581f283c5',
maxZoom: 22
});
// more on https://leaflet-extras.github.io/leaflet-providers/preview/
// create photo layer, clustered thumbnails
const photoLayer = L.photo.cluster({
featureGroup: L.photo,
maxClusterRadius: 100,
showCoverageOnHover: false,
spiderfyDistanceMultiplier: 3,
icon: {iconSize: [100, 100]}});
photoLayer.on('click', photoLayerOnClick);
// add layers to map
map.addLayer(grayscaleCDB);
map.addLayer(photoLayer);
// add layers control
const baseLayers = {
"Grayscale cdb": grayscaleCDB,
"Grayscale mbx": grayscaleMBX,
"Streets": streetsMBX,
"OSM": osmCLR,
"OSM black/white": osmBW,
"OTM": otmCLR,
"Thunderforest outdoors": thunderforestOTD };
const overlays = { "Photos": photoLayer };
L.control.layers(baseLayers, overlays).addTo(map);
// load photos
$.ajax({
type: "GET",
url : IMAGES_CSV,
dataType: "text",
success : function(txt) { processCSV(txt); setupFilter(); }
});
</script>
</body>
</html>