-
Notifications
You must be signed in to change notification settings - Fork 7
/
mbgl-photomap.js
553 lines (484 loc) · 18.7 KB
/
mbgl-photomap.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// map.addControl(new PhotoMapControl({
// layer: "buildings",
// outlineLayer: "buildings_outline",
// editorUrl: "https://re.city/e",
// noterUrl: "https://re.city/nf",
// noterApiUrl: "https://re.city/nb",
// getYear: <function returning current time slider year>
// }));
class PhotoMapControl {
constructor(options) {
this.layer = options.layer;
this.outlineLayer = options.outlineLayer;
this.editorUrl = options.editorUrl;
this.noterUrl = options.noterUrl; // https://re.city/nf/?query=312454650
this.noterApiUrl = options.noterApiUrl;
this.getYear = options.getYear;
this.savedPhotoStyle = null;
this.mapInfoZoomMessage = document.getElementById('map-info-zoom-message');
this.mapOnClick = (e) => {
if (!this.isActive()) {
return;
}
this.handleMapClickEnterPhoto(e);
this.handleMapSelectPolygon(e);
};
this.mapOnMoveend = () => {
if (!this.isActive()) {
this.removePhotoStyle();
return;
}
this.loadPhotoData();
}
}
onAdd(map) {
this._map = map;
this._container = document.createElement('div');
this._container.className = "mapboxgl-ctrl button-photo";
this._container.id = "button-photo";
this._container.textContent = 'i';
this.sideBarContainer = document.getElementById("map-wrapper");
this.searchResults = document.getElementById("search-results");
this.searchResultsList = document.getElementById("search-results-list");
this.searchResultsLoading = document.getElementById("search-results-loading");
this.anno_ids = []; // array of ids suitable for mapping
this.annotations = []; // structure from API containing
this.browseBounds; //current bounds of the map
// Note on "enabled" vs "active" terminology used in this file:
//
// "enabled" means the user has enabled the photo/info display feature, i.e. the 'i' button
// is in its clicked/down state (grey). The user toggles "enabled" explicitly by clicking
// the 'i' button, or via the 'i' key shortcut.
//
// "active" means the current zoom level is >= 17 so we can display photo data.
//
// Photo data is displayed, and buildings can be clicked on, only when both "enabled" and "active" are true.
//
// "enabled" is indicated/controlled by an instance variable.
//
// "active" is indicated by the isActive method.
this.enabled = false;
// When button is clicked:
this._container.addEventListener('click', () => this.toggleEnabled());
return this._container;
}
isActive() {
return this._map.getZoom() > 17 /*this._map.getLayer(this.layer).minzoom*/;
}
toggleEnabled() {
if (this.enabled) {
this.disable();
} else{
if (!this.isActive()) {
this.mapInfoZoomMessage.classList.remove("kartta-hidden");
setTimeout(() => {
this.mapInfoZoomMessage.classList.add("kartta-hidden");
}, 1500);
return;
}
this.enable();
}
}
enable() {
this.enabled = true;
this._map.on('click', this.layer, this.mapOnClick);
this._map.on('moveend', this.layer, this.mapOnMoveend);
this._container.className = this._container.className + " button-photo-enabled";
this.loadPhotoData();
}
disable() {
this.enabled = false;
this.cancelPhotomap();
this.removePhotoStyle();
}
handleMapClickEnterPhoto(e) {
// console.log('Click ',e.features[0].id, e.features[0], e.features[0].properties);
this.searchResultsList.textContent = "";
this.searchResultsList.appendChild(this.buildingItem(e.features[0]));
const footprintId = this._map.getFeatureState({
source: 'antique',
sourceLayer: this.layer,
id: e.features[0].id
}).footprint;
// check to see if the building has a photo associated with it
if (this.anno_ids.indexOf(footprintId) != -1 && this.getPhotos(footprintId).length > 0) {
this.searchResultsList.appendChild(this.photoItem(footprintId));
}else {
const helpDiv = document.createElement("div");
helpDiv.classList.add("photodiv")
const helpHeader = document.createElement("h3");
helpHeader.textContent = "No Photos found."
helpDiv.appendChild(helpHeader);
const helpP = document.createElement("p")
helpP.textContent = "There are currently no photos associated with this building."
helpDiv.appendChild(helpP);
this.searchResultsList.appendChild(helpDiv);
}
// append the please help text
const commonDiv = document.createElement("div");
commonDiv.classList.add("helpdiv")
const commonHeader = document.createElement("h3");
commonHeader.textContent = "Can you help?"
commonDiv.appendChild(commonHeader);
//editor link
const commonP = document.createElement("p")
commonP.textContent = "Do you have a photo for this feature? Want to make a correction to the shape and/or data associated with it? You can upload photos and enter/edit the feature's data in the editor.";
commonP.appendChild(document.createElement("br"));
const a = document.createElement("a");
a.setAttribute("href", "#");
a.textContent = "Open Editor for this feature";
a.addEventListener('click', (e) => {
e.preventDefault();
const year = parseInt(this.getYear());
location.replace(this.editorUrl+"/edit?disable_features=date_range&start_date="+year+"&end_date="+(year+1)+"&way="+footprintId);
});
commonP.appendChild(a);
commonDiv.appendChild(commonP);
////noter link
//const commonP2 = document.createElement("p")
//commonP2.textContent = "You can edit the annotation and facade directly in the Noter."
//commonP2.appendChild(document.createElement("br"));
//const url2 = this.noterUrl+"/?query="+footprintId;
//const a2 = document.createElement("a");
//a2.setAttribute("href", url2);
//a2.setAttribute("target", "new");
//a2.textContent = "Open Noter for this feature";
//commonP2.appendChild(a2);
//commonDiv.appendChild(commonP2);
this.searchResultsList.appendChild(commonDiv);
// append the common links
this.searchResultsLoading.classList.add("kartta-hidden");
this.searchResults.classList.remove('kartta-hidden');
this.sideBarContainer.classList.add("sidebar-open")
}
getFeatureName(feature, wayId) {
if (feature.properties.name) {
return feature.properties.name;
}
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['name'];
}
getFeatureType(feature, wayId) {
// Ignore type/building values of "yes"
if (feature.properties.type) {
return feature.properties.type != "yes" ? feature.properties.type : null;
}
if (!(wayId in this.elements['way'])) {
return null;
}
const building = this.elements['way'][wayId]['tags']['building'];
return building != "yes" ? building : null;
}
getFeatureStartDate(feature, wayId) {
if (feature.properties.start_date) {
return feature.properties.start_date;
}
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['start_date'];
}
getFeatureEndDate(feature, wayId) {
if (feature.properties.end_date) {
return feature.properties.end_date;
}
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['end_date'];
}
getFeatureHouseNumber(feature, wayId) {
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['addr:housenumber'];
}
getFeaturePostCode(feature, wayId) {
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['addr:postcode'];
}
getFeatureStreet(feature, wayId) {
if (!(wayId in this.elements['way'])) {
return null;
}
return this.elements['way'][wayId]['tags']['addr:street'];
}
maybeAddBuildingInfoTableRow(table, name, value) {
if (!value) { return; }
const tr = document.createElement("tr");
const td0 = document.createElement("td");
td0.textContent = name;
tr.appendChild(td0);
const td1 = document.createElement("td");
td1.textContent = value;
tr.appendChild(td1);
table.appendChild(tr);
}
buildingItem(feature) {
const buildingDiv = document.createElement("div");
buildingDiv.classList.add("photodiv");
const header = document.createElement("h3");
const footprintId = this._map.getFeatureState({
source: 'antique',
sourceLayer: this.layer,
id: feature.id
}).footprint;
const table = document.createElement("table");
table.classList.add("building-info");
this.maybeAddBuildingInfoTableRow(table, "name", this.getFeatureName(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "addr:housenumber", this.getFeatureHouseNumber(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "addr:street", this.getFeatureStreet(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "addr:postcode", this.getFeaturePostCode(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "start_date", this.getFeatureStartDate(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "end_date", this.getFeatureEndDate(feature, footprintId));
this.maybeAddBuildingInfoTableRow(table, "type", this.getFeatureType(feature, footprintId));
buildingDiv.appendChild(table);
return buildingDiv;
}
photoItem(id) {
const photoDiv = document.createElement("div");
const header = document.createElement("h3");
header.textContent = "Photos: "
photoDiv.appendChild(header);
const photos = this.getPhotos(id);
const ul = document.createElement("ul");
ul.classList.add('unstyled');
let imageLi;
let imagea;
for (var i=0;i< photos.length;i++){
imageLi = document.createElement("li")
imagea = document.createElement("img")
imagea.setAttribute("src", photos[i].url );
imagea.setAttribute("alt", "You may need to login first to be able to view this image.");
imagea.setAttribute("width", "300px" );
imageLi.appendChild(imagea);
ul.appendChild(imageLi);
}
photoDiv.appendChild(ul);
return photoDiv;
}
getPhotos(id) {
let photos = [];
this.annotations.forEach(anno => {
if (id == anno.buildingId){
photos.push( {"url": this.noterApiUrl + "/download/"+ anno.noterImageId +"/"})
}
})
return photos;
}
loadPhotoData() {
const bounds = this._map.getBounds();
//dont load data if the map is over the same areas
if (this.browseBounds && (this.browseBounds.contains(bounds._sw) && this.browseBounds.contains(bounds._ne) )){
return;
}
if (!this.isActive()) {
return;
}
var mapbounds = [bounds._sw.lng, bounds._sw.lat, bounds._ne.lng, bounds._ne.lat].join(",")
const url = this.editorUrl+ "/api/0.6/map?bbox=" + mapbounds;
fetch(url, {
headers: {
Accept: 'application/json'
}
}).then(response => {
return response.json();
}).then(result => {
this.annotations = this.photoAnnotations(result);
this.anno_ids = this.idsFromAnnotations(this.annotations)
this.updateFeatureState(this.getMultipolygons(result));
this.showPhotoStyle();
this.browseBounds = bounds;
}).catch(error => {
console.log('Error:', error);
})
}
removeHighlight() {
if (typeof this._map.getLayer('highlight') !== "undefined" ){
this._map.removeLayer('highlight');
}
}
handleMapSelectPolygon(e) {
const features = this._map.queryRenderedFeatures(e.point, { layers: [this.layer] });
if (!features.length) {
return;
}
this.removeHighlight();
let filter = ["==", '$id', features[0].id];
if (features[0].properties.name){
filter = ["all",
["==", '$id', features[0].id],
["==", "name", features[0].properties.name]
]
}
this._map.addLayer(
{
'id': 'highlight',
'type': 'line',
'source': 'antique',
'source-layer': 'buildings',
"paint": {"line-color": "yellow", "line-width": 5 },
'filter': filter
}
);
}
showPhotoStyle() {
// highlight the building lines
if (this.anno_ids.length > 0){
this.savedPhotoStyle = {
'line-color': this._map.getPaintProperty(this.outlineLayer, 'line-color'),
'line-width': this._map.getPaintProperty(this.outlineLayer, 'line-width'),
'line-opacity': this._map.getPaintProperty(this.outlineLayer, 'line-opacity')
};
this._map.setPaintProperty(this.outlineLayer, 'line-color', ['match', ['feature-state','footprint'], [...this.anno_ids], '#de683d', '#aaaaaa' ]);
this._map.setPaintProperty(this.outlineLayer, 'line-width', ['match', ['feature-state','footprint'], [...this.anno_ids], 4.5 , 1.5] );
this._map.setPaintProperty(this.outlineLayer, 'line-opacity', ['match', ['feature-state','footprint'], [...this.anno_ids], 1 , 0.5 ]);
}
}
removePhotoStyle() {
if (this.savedPhotoStyle != null) {
this._map.setPaintProperty(this.outlineLayer, 'line-color', this.savedPhotoStyle['line-color']);
this._map.setPaintProperty(this.outlineLayer, 'line-width', this.savedPhotoStyle['line-width']);
this._map.setPaintProperty(this.outlineLayer, 'line-opacity', this.savedPhotoStyle['line-opacity']);
this.savedPhotoStyle = null;
}
}
cancelPhotomap() {
this._map.off('click', this.layer, this.mapOnClick);
this._map.off('moveend', this.layer, this.mapOnMoveend);
this.removeHighlight();
this.browseBounds = null;
this._container.className = "mapboxgl-ctrl button-photo";
this.sideBarContainer.classList.remove("sidebar-open");
this.searchResults.classList.add('kartta-hidden');
};
// Extract photo annotatioin data from editor api db data.
// Takes a json object containing editor api data (from the endpoint '/e/api/0.6/map?bbox=...').
// Returns an array containing one json object for each photo annotation; each object looks like:
// {
// buildingId: < the building id (way id) of a building >
// noterAnnotationId: < id for the annotation in noter >
// noterImageId: < id for the image in noter >
// facadeLine: < coordinates of the facade line for the photo >
// }
// Has the side effect of saving a map of the loaded data in this.elements.
photoAnnotations(json) {
const elements = {
'node': {},
'way': {},
'relation': {}
};
json.elements.forEach(element => {
elements[element.type][element.id] = element;
});
const noterAnnotationRelationIds = Object.keys(elements['relation']).filter(id => 'noter_annotation_id' in elements.relation[id].tags);
const annotations = noterAnnotationRelationIds.map(id => {
const relation = elements.relation[id];
const buildingId = relation.members.filter(member => member.role == 'footprint')[0].ref;
// facade might be useful later
// const facadeLineId = relation.members.filter(member => member.role == 'facadeline')[0].ref;
// let facadeLine = [];
// if (elements.way[facadeLineId]) {
// const facadeLineNodeIds = elements.way[facadeLineId].nodes;
// const facadeLineNodes = facadeLineNodeIds.map(id => elements['node'][id]);
// facadeLine = facadeLineNodes.map(node => [node.lon, node.lat]);
// }
return {
buildingId: buildingId,
noterAnnotationId: relation.tags.noter_annotation_id,
noterImageId: relation.tags.noter_image_id //,
// facadeLine: facadeLine
};
});
this.elements = elements;
return annotations;
};
getMultipolygons(json) {
const elements = {
'node': {},
'way': {},
'relation': {}
};
json.elements.forEach(element => {
elements[element.type][element.id] = element;
});
const multi = {};
json.elements.forEach(element => {
if (element.type == 'relation' && 'building' in element.tags ){
if (element.tags.type && element.members && element.tags.type == 'multipolygon'){
const outer = element.members.filter(member => member.role == 'outer')[0];
multi[element.id] = elements['way'][outer.ref];
}
}
});
return {'elements':elements, 'multi': multi};
}
// updates the feature state of all buildings with their id, an updates those buildings which are multi polygons with the outer relation id
updateFeatureState(elements_and_multi) {
const elements = elements_and_multi.elements
const multi = elements_and_multi.multi
//add footprint feature state to all buildings
const allfeatures = this._map.querySourceFeatures('antique', {sourceLayer: this.layer });
allfeatures.forEach(feat => {
this._map.setFeatureState({
source: 'antique',
sourceLayer: this.layer,
id: feat.id
}, {"footprint": feat.id}
);
})
const node_keys = Object.keys(elements.node)
for (let [key, outer] of Object.entries(multi)) {
//console.log("outer",outer)
let way_nodes = [];
for (let a = 0; a<outer.nodes.length; a++){
if (node_keys.indexOf(outer.nodes[a].toString()) > -1){
const matchingid = outer.nodes[a];
way_nodes.push(elements.node[matchingid]);
}
}
// Query the map for the building features, and set the footprint id as the outer way id.
const centerPoint = this.calcCenter(way_nodes)
const center = this._map.project(centerPoint)
const intersects = this._map.queryRenderedFeatures(center, { layers: [this.layer] });
if (intersects.length> 0) {
intersects.forEach(feat => {
this._map.setFeatureState({
source: 'antique',
sourceLayer: this.layer,
id: feat.id
}, {"footprint": outer.id}
);
})
}
}
}
// helper function to simply calcuate the center of a way
calcCenter(way_nodes) {
var minX, maxX, minY, maxY;
for (var i = 0; i < way_nodes.length; i++) {
minX = (way_nodes[i].lon < minX || minX == null) ? way_nodes[i].lon : minX;
maxX = (way_nodes[i].lon > maxX || maxX == null) ? way_nodes[i].lon : maxX;
minY = (way_nodes[i].lat < minY || minY == null) ? way_nodes[i].lat : minY;
maxY = (way_nodes[i].lat > maxY || maxY == null) ? way_nodes[i].lat : maxY;
}
return [(minX + maxX) / 2, (minY + maxY) / 2];
}
idsFromAnnotations(annotations) {
const ids = [];
annotations.forEach(annotation => {
if(ids.indexOf(annotation.buildingId) == -1){
ids.push(annotation.buildingId);
}
})
return ids;
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
}
}