-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
575 lines (545 loc) · 26.3 KB
/
index.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>RAÄ Bebygelse registret</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="En temporär visningstjänst för RAÄs bebyggelseregister" />
<meta name="keywords" content="Riksantikvarieämbetet, Bebyggelseregistret">
<meta name="author" content="Andrew Mercer">
<!-- <link rel="stylesheet" type="text/css" href="style.css" /> -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<!-- Read in geojson data stored as variables in external js files.
Cannot easily export list ordered by län from QGIS which means either exporting individual files and then copyimng them into one file or just read them in individually-->
<!--<script type="text/javascript" src="./resources/BebrData.js"></script>-->
<script type="text/javascript" src="./resources/BebrP1.js"></script>
<script type="text/javascript" src="./resources/BebrP2.js"></script>
<style>
:root{
--raaOrange: #C94A18;
--raaYellow: #ffb219;
--raaBlue: #00567E;
--raaGreen: #008578;
}
html, body {
height: 90vh;
}
#ident {
display: flex;
}
#logo{
display: inline-block;
height: 50px;
}
#headline{
display: inline-block;
font-size: 30px;
padding-top: 5px;
height: 50px;
line-height: 50px;
margin-left: auto;
}
#maindisplay {
height: 100%;
border: 2px solid var(--raaOrange);
}
#map {
height: 100%;
width: 50%;
float: left;
display: inline-block;
}
#searchpanel {
height: 20%;
width: 48%;
padding: 5px;
float: right;
}
#infopanel {
height: 75%;
width: 48%;
padding: 5px;
float: right;
display: inline-block;
overflow-y: auto;
}
</style>
</head>
<body>
<!-- Set up basic page elements-->
<div id="ident"><img id="logo" src="./resources/RAA_logo_farg_rgb-950px.jpg" alt="RAÄ logo"> <span id="headline">Bebyggelseregistret</span></div>
<div id="maindisplay">
<div id="map"></div>
<div id="searchpanel">
<form id="idsearch" onsubmit="searchID(event)">
<label for="searchIDInput">Sök efter UUID:</label>
<input type="text" id="searchIDInput" placeholder="ID nummer" required>
<button type="submit">Sök</button>
</form><br>
<form id="namesearch" onsubmit="searchNamnAll(event)">
<label for="searchNmInput">Sök efter namn:</label>
<input type="text" id="searchNmInput" placeholder="namn" required>
<button type="submit">Sök</button>
</form><br>
<form id="byggbetsearch" onsubmit="searchByggBet(event)">
<label for="searchBBInput">Sök efter byggnadsbeteckning:</label>
<input type="text" id="searchBBInput" placeholder="byggnadsbetckning" required>
<button type="submit">Sök</button>
</form>
<br>
<form id="formreset">
<input type="button" onclick="resetFormFunction()" value="Tömma sökfälten">
</form>
</div>
<div id="infopanel"></div>
</div>
<!--Main script for creating map and displaying data-->
<script>
// Create empty map surface
startPos = [63, 19]; // Coordinates in lat,lon for map centre
startZoom = 5; // Initial zoom level where lower values means more zoomed out
var map = L.map('map').setView(startPos, startZoom); //Create Leaflet map object
// Add OSM map to map object
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {maxZoom: 19, attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'}).addTo(map);
// Add geojson data
// Set up RAÄ colours
let raaOrange = "#C94A18";
let raaYellow = "#ffb219";
let raaBlue = "#00567E";
let raaGreen = "#008578";
// Create point marker object
var geojsonMarkerOptions = {
radius: 4,
fillColor: raaOrange,
color: raaYellow,
weight: 1,
opacity: 1,
fillOpacity: 1
};
// Create polygon marker object
var geojsonPolygonOptions = {
color: raaOrange,
weight: 1,
opacity: 1,
fillColor: raaBlue,
fillOpacity: 0.8,
}
// JS object constructor for data objects. To be used for each län
function dataStore(name, points, polygons) {
this.name = name;
this.points = points;
this.polygons = polygons
}
// Creates data object from GeoJson formatted variable read in by script(s) in html head
function setUpLayers(data_store, add = false) {
data_store.layerGroup = L.layerGroup(); // Create Leaflet layer group for each län
// Add point data (byggnader) för current län
data_store.pointLayer = L.geoJSON(data_store.points, {
renderer: L.svg(),
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
},
onEachFeature: onEachFeature
}).addTo(data_store.layerGroup);
// Add polygon data (byggnadsminne) for current län
data_store.polygonLayer = L.geoJSON(data_store.polygons, {
renderer: L.canvas(),
style: geojsonPolygonOptions,
onEachFeature: onEachFeature
}).addTo(data_store.layerGroup);
// This will activate the current group when the page loads. Default is false
if (add == true) {data_store.layerGroup.addTo(map);}
}
let stores = []; // Create list of data objects. Used to loop through later
// Explicitly set up a data store for each län using imported GeoJson data structure
// Three lines repeat for each län:
// - Create new data store using the previously constructed object
// - Pass object to setUpLayers function - "Needs" to be done after the creation of the object
// - Add the data store to the list/array of stores
const Blekinge = new dataStore("Blekinge", pointBlekinge, polygonBlekinge);
setUpLayers(Blekinge);
stores.push(Blekinge);
const Dalarna = new dataStore("Dalarna", pointDalarna, polygonDalarna);
setUpLayers(Dalarna);
stores.push(Dalarna);
const Gotland = new dataStore("Gotland", pointGotland, polygonGotland);
setUpLayers(Gotland);
stores.push(Gotland);
const Gavleborg = new dataStore("Gävleborg", pointGavleborg, polygonGavleborg);
setUpLayers(Gavleborg);
stores.push(Gavleborg);
const Halland = new dataStore("Halland", pointHalland, polygonHalland);
setUpLayers(Halland);
stores.push(Halland);
const Jamtland = new dataStore("Jämtland", pointJamtland, polygonJamtland);
setUpLayers(Jamtland);
stores.push(Jamtland);
const Jonkoping = new dataStore("Jönköping", pointJonkoping, polygonJonkoping);
setUpLayers(Jonkoping);
stores.push(Jonkoping);
const Kalmar = new dataStore("Kalmar", pointKalmar, polygonKalmar);
setUpLayers(Kalmar);
stores.push(Kalmar);
const Kronoberg = new dataStore("Kronoberg", pointKronoberg, polygonKronoberg);
setUpLayers(Kronoberg);
stores.push(Kronoberg);
const Norrbotten = new dataStore("Norrbotten", pointNorrbotten, polygonNorrbotten);
setUpLayers(Norrbotten);
stores.push(Norrbotten);
const Skane = new dataStore("Skåne", pointSkane, polygonSkane);
setUpLayers(Skane);
stores.push(Skane);
const Stockholm = new dataStore("Stockholm", pointStockholm, polygonStockholm);
setUpLayers(Stockholm);
stores.push(Stockholm);
const Sodermanland = new dataStore("Södermanland", pointSodermanland, polygonSodermanland);
setUpLayers(Sodermanland);
stores.push(Sodermanland);
const Uppsala = new dataStore("Uppsala", pointUppsala, polygonUppsala);
setUpLayers(Uppsala);
stores.push(Uppsala);
const Varmland = new dataStore("Värmland", pointVarmland, polygonVarmland);
setUpLayers(Varmland);
stores.push(Varmland);
const Vasterbotten = new dataStore("Västerbotten", pointVasterbotten, polygonVasterbotten);
setUpLayers(Vasterbotten);
stores.push(Vasterbotten);
const Vasternorrland = new dataStore("Västernorrland", pointVasternorrland, polygonVasternorrland);
setUpLayers(Vasternorrland);
stores.push(Vasternorrland);
const Vastmanland = new dataStore("Västmanland", pointVastmanland, polygonVastmanland);
setUpLayers(Vastmanland);
stores.push(Vastmanland);
const VastraGotaland = new dataStore("Västra Götaland", pointVastraGotaland, polygonVastraGotaland);
setUpLayers(VastraGotaland);
stores.push(VastraGotaland);
const Orebro = new dataStore("Örebro", pointOrebro, polygonOrebro);
setUpLayers(Orebro);
stores.push(Orebro);
const Ostergotland = new dataStore("Östergötland", pointOstergotland, polygonOstergotland);
setUpLayers(Ostergotland);
stores.push(Ostergotland);
// Add Leaflet layer control/table of contents
var layerControl = L.control.layers({},
{
"Blekinge": Blekinge.layerGroup,
"Dalarna": Dalarna.layerGroup,
"Gotland": Gotland.layerGroup,
"Gävleborg": Gavleborg.layerGroup,
"Halland": Halland.layerGroup,
"Jämtland": Jamtland.layerGroup,
"Jönköping": Jonkoping.layerGroup,
"Kalmar": Kalmar.layerGroup,
"Kronoberg": Kronoberg.layerGroup,
"Norrbotten": Norrbotten.layerGroup,
"Skåne": Skane.layerGroup,
"Stockholm": Stockholm.layerGroup,
"Södermanland": Sodermanland.layerGroup,
"Uppsala": Uppsala.layerGroup,
"Värmland": Varmland.layerGroup,
"Västerbotten": Vasterbotten.layerGroup,
"Västernorrland": Vasternorrland.layerGroup,
"Västmanland": Vastmanland.layerGroup,
"Västra Götaland": VastraGotaland.layerGroup,
"Örebro": Orebro.layerGroup,
"Östergötland": Ostergotland.layerGroup},
{collapsed: false}
).addTo(map);
layerControl.expand(); // Stops leaflet collapsing the layer control
var scaleControl = L.control.scale({imperial: false}).addTo(map); // Adds a scale bar to the map
// Reset the map view to the inital zoom and centre. Appears as text below the zoom controls +-
(function() {
var control = new L.Control({position:'topleft'});
control.onAdd = function(map) {
var azoom = L.DomUtil.create('a','resetzoom');
azoom.textContent = "RESET ZOOM";
L.DomEvent
.disableClickPropagation(azoom)
.addListener(azoom, 'click', function() {
map.setView(startPos, startZoom);
},azoom);
return azoom;
};
return control;
}()).addTo(map);
// Functions used to display geojson data
// On mouse click on point or polygon a pop-up is displayed, the map zooms to the feature and attributes are displayed
function onEachFeature(feature, layer) {
if (feature.properties && feature.properties.namn) {
layer.bindPopup("Namn: " + feature.properties.namn);
} else if (feature.properties && feature.properties.id) {
layer.bindPopup("ID: " + feature.properties.id);
}
layer.on({
click: zoomToEvent
});
}
// For sanitizing text to be written to DOM below
function escapeHTML(str) {
str = String(str);
return str.replace(/[&<>"'/]/g, function (char) {
switch (char) {
case '&': return '&';
case '<': return '<';
case '>': return '>';
case '"': return '"';
case "'": return ''';
case "/": return '/';
}
});
}
// takes a string and normalises it to lower case,
// removes non-name characters and removes leading and trailing whitespace.
// Aids comparing manually entered search string (namn) with text stored in field "namn"
function normalizeString(str) {
if (str == null) {return '';}
return str
.normalize("NFD") // Normalize for diacritics
.toLowerCase() // Lowercase the string
.replace(/[^a-zA-Z0-9\s]/g, '') // Remove non-alphanumeric characters
.replace(/\s+/g, ' ') // Normalize spaces (e.g., multiple spaces to one)
.trim(); // Trim any leading or trailing whitespace
}
/* Extracts http url component after ?*/
function getQueryParam(param) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}
//Function to accept and populate the idsearch form if uuid is passed in url
// According to Johan Nordinge the uuids were created by Ksamsök and are used across all RAÄ data
function acceptParam() {
// Get uuid as "searchText"
const searchText = getQueryParam('uuid');
if (searchText) {
// Sanitize the input from the URL
const sanitizedText = escapeHTML(searchText);
// Fill the form with the sanitized text
document.getElementById('searchIDInput').value = sanitizedText;
}
}
// Trigger acceptParam when the page loads
window.onload = acceptParam;
// SEARCH FUNCTIONS
// Search data store objects for uuid stored in field "id"
function searchID(event) {
// Search box
event.preventDefault(); // Prevent form submission (page refresh)
// console.log(event) // Debug only
// Get the value from the input field
const searchValue = escapeHTML(document.getElementById('searchIDInput').value).trim();
// console.log("searchValue: ", searchValue) // Debug only
let found = false;
// Check active layers first, beginning with polygon layer
// as these tend to be considerably smaller. Intended to speed up performance.
for (let data_store of stores) {
if (map.hasLayer(data_store.polygonLayer)) {
for (let feature of data_store.polygons.features) {
if (feature.properties.id && feature.properties.id.includes(searchValue)) {
highlightFeature(feature);
found = true;
break;
}
}
};
if (map.hasLayer(data_store.pointLayer) && found == false) {
for (let feature of data_store.points.features) {
if (feature.properties.id && feature.properties.id.includes(searchValue)) {
highlightFeature(feature);
found = true;
break;
}
}
};
}
// Check inactive layers
// Used primarily when uuid provided in url as there is no default active layer
if (found == false) {
for (let data_store of stores) {
for (let feature of data_store.polygons.features) {
if (feature.properties.id && feature.properties.id.includes(searchValue)) {
highlightFeature(feature);
found = true;
data_store.layerGroup.addTo(map);
break;
}
}
}
}
if (found == false) {
for (let data_store of stores) {
for (let feature of data_store.points.features) {
if (feature.properties.id && feature.properties.id.includes(searchValue)) {
highlightFeature(feature);
found = true;
data_store.layerGroup.addTo(map);
break;
}
}
}
}
// Finally, if the search term cannot be matched to an existing uuid give feedback to user
if (found == false) {
document.getElementById("infopanel").textContent = searchValue + " not found.";
};
}
// Searches through active layers of map looking for match in field "namn"
// This search does not include inactive layers as many objects have similar or even matching names.
// This limitation is intended to reduce the number of potential matches as only the first match is reported.
function searchNamnAll(event){
// Search box
event.preventDefault(); // Prevent form submission (page refresh)
const searchValue = normalizeString(document.getElementById('searchNmInput').value); // Exact match of string not needed
// console.log("searchValue: ", searchValue) // Debug only
foundFeatureList = []; // Empty list will be populated wit features matching search criteria
for (let data_store of stores){ // Loop through all stored data
if (map.hasLayer(data_store.polygonLayer)) { // Select only those polygon data sets active in the map
for (let feature of data_store.polygons.features) {
const featureName = normalizeString(feature.properties.namn);
if (feature.properties.namn && featureName.includes(searchValue)) {
foundFeatureList.push(feature);
}
}
};
if (map.hasLayer(data_store.pointLayer) ) { // Select only those point layers active in th emap
for (let feature of data_store.points.features) {
const featureName = normalizeString(feature.properties.namn);
if (feature.properties.namn && featureName.includes(searchValue)) {
foundFeatureList.push(feature);
}
}
};
}
if (!foundFeatureList.length) {
document.getElementById("infopanel").textContent = searchValue + " not found." ;
};
// Add basic info and a button for each matching feature
let infoPanel = document.getElementById("infopanel");
while (infoPanel.firstChild) {
infoPanel.removeChild(infoPanel.firstChild);
}
for (let feature of foundFeatureList) {
attData = [
{namn:feature.properties.namn}, {id:feature.properties.id},
{kommun:feature.properties.kommun}, {lan:feature.properties.lan},
{huvudgrupp:feature.properties.huvudgrupp}]; // Gets the attributes of the feature
let mapLink = document.createElement("button") // Creates a button in html
mapLink.data = '';
mapLink.innerHTML = 'zoom till:';
mapLink.addEventListener('click', () => zoomFeature(feature)) // Adds zoom function to button
infoPanel.appendChild(mapLink);
let brElem = document.createElement('br'); // Create line break html element
infoPanel.appendChild(brElem);
for (let x in attData) {
let label = escapeHTML(Object.getOwnPropertyNames(attData[x]));
let value = attData[x][Object.getOwnPropertyNames(attData[x])];
let boldElem = document.createElement('b');
boldElem.textContent = label + ": ";
let textNode = document.createTextNode(value + " ");
let brElem = document.createElement('br');
infoPanel.appendChild(boldElem);
infoPanel.appendChild(textNode);
infoPanel.appendChild(brElem);
}
brElem = document.createElement('br');
infoPanel.appendChild(brElem); // Add a line after each feature in list
}
}
// Searches the field "byggnadsbeteckning" in the point(byggnad) layer.
// Uncertain of the value of this search function. Could be removed?
function searchByggBet(event) {
// Search box
event.preventDefault(); // Prevent form submission (page refresh)
const searchValue = escapeHTML(document.getElementById('searchBBInput').value).trim(); // Get the value from the input field
// console.log("searchValue: ", searchValue) // Debug only
let found = false;
for (let data_store of stores){
if (map.hasLayer(data_store.pointLayer) && found == false) {
for (let feature of data_store.points.features) {
if (feature.properties.byggnadsbeteckning && feature.properties.byggnadsbeteckning.includes(searchValue)) {
highlightFeature(feature);
found = true;
break;
}
}
};
}
if (found == false) {
document.getElementById("infopanel").textContent = searchValue + " not found.";
};
}
// Clear the search windows of their current content
function resetFormFunction() {
document.getElementById("idsearch").reset();
document.getElementById("namesearch").reset();
document.getElementById("byggbetsearch").reset();
}
// Convert event into feature and pass to highlightFeature()
// Used by mouse click on map object
function zoomToEvent(e) {
highlightFeature(e.target.feature)
}
// Displays selected feature's attributes in infopanel but first calls the zoomFeature() function
function highlightFeature(foundFeat) {
// console.log("foundFeat: ", foundFeat)
zoomFeature(foundFeat)
attData = foundFeat.properties
let infoPanel = document.getElementById("infopanel");
while (infoPanel.firstChild) {
infoPanel.removeChild(infoPanel.firstChild);
}
for (let x in attData) {
if (x == 'fid') { continue; }
// if (x.includes("url")) { continue; }
let label = escapeHTML(x);
let value = escapeHTML(attData[x]);
let boldElem = document.createElement('b');
boldElem.textContent = label + ": ";
infoPanel.appendChild(boldElem);
if (x.includes("url")) { // URL attributes are lists of semi-colon separeted urls
if (!!attData[x]) { // Some are null and split can't handle null
createMultiUrl(x, label, infoPanel);
}
}
else {
let textNode = document.createTextNode(value + " ");
infoPanel.appendChild(textNode);
}
let brElem = document.createElement('br');
infoPanel.appendChild(brElem);
}
}
// Loops through text list of semi-colon separated urls and creates links for each
function createMultiUrl(x, label, infoPanel) {
let xArray = attData[x].split(";");
// console.log("xArray: ", xArray)
for (let xText of xArray) {
let a = document.createElement('a');
let link = document.createTextNode(label);
a.appendChild(link);
a.title = label;
a.href = xText;
a.setAttribute('target', '_blank');
infoPanel.appendChild(a);
let textNode = document.createTextNode(", ");
infoPanel.appendChild(textNode);
}
}
// Zooms the map to the active feature
function zoomFeature(foundFeat) {
if (foundFeat.geometry.type == "Polygon"){ // Built-in for zooming to boundingbox of line or polygon
var tempLayer = L.geoJSON(foundFeat);
map.fitBounds(tempLayer.getBounds());
} else { // Points have no bounding box and so an arbitrary box is defined
degMetre = 0.0018;
pointLon = foundFeat.geometry.coordinates[0]
pointLat = foundFeat.geometry.coordinates[1]
bbll = [pointLat - degMetre , pointLon - degMetre]
bbur = [pointLat + degMetre , pointLon + degMetre]
map.fitBounds([bbll, bbur])
}
}
</script>
</body>
</html>