-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
413 lines (367 loc) · 13.7 KB
/
Copy pathindex.html
File metadata and controls
413 lines (367 loc) · 13.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Gieß den Kiez – Kennzahlen</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.js"></script>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.css"
rel="stylesheet"
/>
<script src="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.mapboxgl-popup {
width: inline-block;
font: 12px/20px "Helvetica Neue", Arial, Helvetica, sans-serif;
line-height: 100%;
}
.mapboxgl-popup p {
line-height: 90%;
}
.mapboxgl-popup-content {
padding: 0 1em 0.2em 1em;
box-shadow: 0 6px 6px rgba(0, 0, 0, 0.11);
}
.legend {
background-color: #fff;
border-radius: 5px;
top: 10px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
font: 14px "Helvetica Neue", Arial, Helvetica, sans-serif;
padding: 20px;
position: absolute;
left: 10px;
z-index: 1;
}
.legend h3,
h4 {
margin: 0 0 0;
}
.legend p {
margin: 10px 0 10px;
}
.legend div span {
border-radius: 50%;
display: inline;
height: 10px;
margin-right: 5px;
width: 10px;
}
#dynamic div span {
border-radius: 50%;
display: inline-block;
height: 10px;
margin-right: 5px;
width: 10px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="state-legend" class="legend">
<h3>Gieß den Kiez Kennzahlen</h3>
<p><em>Vegetationsperiode 2021 (März bis Oktober)</em></p>
<hr style="opacity: 0.4;" />
<div id="dynamic">
<p style="background-color: #35de89; opacity: 0.8; color: white; padding: 0.5em 1em 0.5em 1em; display: inline-flex; margin: 0.5em 0 0 0"><strong>Berlin</strong></p>
<p>∑ Gegossene Bäume: <strong><span id="sumBaumBerlin"></span></strong></p>
<p>∑ Gießungen: <strong><span id="sumGiessBerlin"></span></strong></p>
<p>∑ Wassermenge: <strong><span id="sumWasserBerlin"></span>Liter</strong></p>
</div>
<hr style="opacity: 0.4;" />
<p style="background-color: #35de89; opacity: 0.8; color: white; padding: 0.5em 1em 0.5em 1em; display: inline-flex; margin: 0.5em 0 0 0">
<strong><span id="kiezname">Bezirk nicht ausgewählt</span></strong>
<p>∑ Gegossene Bäume: <strong><span id="sumBaumBezirk">0</span></strong></p>
<p>∑ Gießungen: <strong><span id="sumGiessBezirk">0</span></strong></p></p>
<p>∑ Wassermenge: <strong><span id="sumWasserBezirk">0</span> Liter</strong></p>
</p>
</div>
<script>
mapboxgl.accessToken =
"pk.eyJ1IjoianVsaWF6ZXQiLCJhIjoiY2trdmhkdmE0MXNidjJ1cGZhaXVrZDE1NSJ9.I0eyCt352bKnMo50n4cDIQ";
var map;
map = new mapboxgl.Map({
container: "map", // container id
style: "mapbox://styles/mapbox/light-v9",
center: [13.430168, 52.489868], // starting position ([lng, lat] close to Hermannplatz
zoom: 15, // starting zoom
pitch: 45,
maxZoom: 18,
minZoom: 10,
});
var bezirkID = null;
var scale = new mapboxgl.ScaleControl({
maxWidth: 160,
unit: "metric",
});
map.addControl(scale, "bottom-right");
map.addControl(new mapboxgl.NavigationControl());
map.addControl(new mapboxgl.FullscreenControl());
// Add geolocate control to the map.
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true,
},
trackUserLocation: true,
})
);
// helper function
function numberFormat(num) {
return (
num
.toFixed(0) // always two decimal digits
.replace('.', ',') // replace decimal point character with ,
.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
) // use . as a separator
}
let kiezname;
let trees;
let sumB = 0;
let sumG = 0;
let sumW = 0
// load data with vanllia JS
fetch("./data/wateredTreesSum_MidSep.geojson").then(data => data.json()).then(geojson => {
trees = Object.values(geojson) // object
trees = trees[1] // array
// +++ calculate numbers for Berlin
sumB = trees.length;
trees.forEach(function(feature){
sumG = sumG + feature.properties.anzahlG;
sumW = sumW + feature.properties.wassersumme;
})
const sumBaumBerlin = document.getElementById("sumBaumBerlin");
sumBaumBerlin.innerHTML = numberFormat(sumB);
const sumGiessBerlin = document.getElementById("sumGiessBerlin");
sumGiessBerlin.innerHTML = numberFormat(sumG);
const sumWasserBerlin = document.getElementById("sumWasserBerlin");
sumWasserBerlin.innerHTML = numberFormat(sumW);
})
// // Berlin
// let gemeindeTrees = allTrees.filter((tree) => (tree.Gemeinde_name === kiezname));
// // Gemeinde
// let wassermenge = 0;
// gemeindeTrees.forEach((trees) =>
// wassermenge = wassermenge + trees.
// )
// add data sources right from mapbox tilesets
map.on("load", function () {
map.addSource("bezirke", {
type: "geojson",
generateId: true,
data: "./data/bezirksgrenzen.geojson",
});
map.addSource("watered-trees", {
type: "vector",
url: "mapbox://juliazet.azwgfjip",
});
map.addSource("watered-trees-geojson", {
type: "geojson",
data: "./data/treesExtrusion.geojson",
});
map.addLayer({
id: "bezirk-fill",
type: "fill",
source: "bezirke",
layout: {},
paint: {
"fill-color": "#35DE89",
"fill-opacity": [
"case",
["boolean", ["feature-state", "hover"], false],
0.18,
0,
],
},
});
map.addLayer({
id: "bezirk-outline",
type: "line",
source: "bezirke",
layout: {},
paint: {
"line-color": "#35DE89",
"line-width": 1.5,
},
});
// add tree nodes
map.addLayer({
id: "watered-trees",
type: "circle",
source: "watered-trees",
"source-layer": "wateredTreesSum_MidSep-0lqlgu",
paint: {
// Make circles larger as the user zooms from z12 to z22.
"circle-radius": {
base: 2,
stops: [
[10, 2],
[22, 100],
],
},
// Color circles by ethnicity, using a `match` expression.
"circle-color": /* other */ "#2F2FA2",
},
});
map.addLayer({
id: "watered-extrusion",
type: "fill-extrusion",
source: "watered-trees-geojson",
paint: {
// Get the `fill-extrusion-color` from the source `color` property.
"fill-extrusion-color": "#2F2FA2",
// Get `fill-extrusion-height` from the source `height` property.
"fill-extrusion-height": ["get", "wassersumme"],
// Get `fill-extrusion-base` from the source `base_height` property.
"fill-extrusion-base": 0,
// Make extrusions slightly opaque to see through indoor walls.
"fill-extrusion-opacity": 0.5,
},
});
// +++ END OF MAP ON LOAD
});
// var calculated = false;
// // calculate Berlin data
// map.on('idle',function(){
// if(map.isSourceLoaded("watered-trees") && calculated == false){
// const features = map.querySourceFeatures('watered-trees', {sourceLayer:'wateredTreesSum_MidSep-0lqlgu'});
// console.log("features", features.slice(-4)[0].properties.id);
// // Because features come from tiled vector data or GeoJSON data that is converted to tiles internally, feature geometries may be split or duplicated across tile boundaries and,
// // as a result, features may appear multiple times in query results!!! WTF
// const key = "id";
// const arrayUniqueByKey = [...new Map(features.map(item =>[item[key], item])).values()];
// console.log("unique by key", arrayUniqueByKey);
// // WTF!!!
// // querySourceFeatures() always returns features which have been loaded into the current viewport.
// // It can't access anything other than the current zoom level.
// let sumG = 0;
// let sumW = 0;
// arrayUniqueByKey.forEach(function(feature){
// sumG = sumG + feature.properties.anzahlG;
// sumW = sumW + feature.properties.wassersumme;
// })
// const sumGiessBerlin = document.getElementById("sumGiessBerlin");
// sumGiessBerlin.innerHTML = sumG;
// const sumWasserBerlin = document.getElementById("sumWasserBerlin");
// sumWasserBerlin.innerHTML = sumW;
// calculated = true;
// }
// })
// When the user moves their mouse over the state-fill layer, we'll update the
// feature state for the feature under the mouse.
map.on("mousemove", "bezirk-fill", function (e) {
if (e.features.length > 0) {
if (bezirkID !== null) {
map.setFeatureState(
{ source: "bezirke", id: bezirkID },
{ hover: false }
);
}
// Use the first found id of the feature.
bezirkID = e.features[0].id;
map.setFeatureState(
{ source: "bezirke", id: bezirkID },
{ hover: true }
);
}
kiezname = e.features[0].properties.Gemeinde_name;
const kieznameElement = document.getElementById("kiezname");
kieznameElement.innerHTML = kiezname;
// +++ calculate numbers for hovered Bezirk
let bezirkTrees = trees.filter((e) => (e.properties.bezirk === kiezname));
let sumBBezirk = bezirkTrees.length;
let sumGBezirk = 0;
let sumWBezirk = 0
bezirkTrees.forEach(function(feature){
sumGBezirk = sumGBezirk + feature.properties.anzahlG;
sumWBezirk = sumWBezirk + feature.properties.wassersumme;
})
let ratioBaum = sumBBezirk/sumB*100;
ratioBaum = ratioBaum.toFixed(2);
let ratioGiess = sumGBezirk/sumG*100;
ratioGiess = ratioGiess.toFixed(2);
let ratioWasser =sumWBezirk/sumW*100;
ratioWasser = ratioWasser.toFixed(2);
const sumBaumBezirk = document.getElementById("sumBaumBezirk");
sumBaumBezirk.innerHTML = numberFormat(sumBBezirk) + " (" + ratioBaum +"%)";
const sumGiessBezirk = document.getElementById("sumGiessBezirk");
sumGiessBezirk.innerHTML = numberFormat(sumGBezirk) + " (" + ratioGiess +"%)";
const sumWasserBezirk = document.getElementById("sumWasserBezirk");
sumWasserBezirk.innerHTML = numberFormat(sumWBezirk) + " (" + ratioWasser +"%)";
});
// When the mouse leaves the state-fill layer, update the feature state of the
// previously hovered feature.
map.on("mouseleave", "bezirk-fill", function () {
if (bezirkID !== null) {
map.setFeatureState(
{ source: "bezirke", id: bezirkID },
{ hover: false }
);
}
bezirkID = null;
});
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
map.on("mouseenter", "watered-trees", function (e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = "pointer";
let coordinates = e.features[0].geometry.coordinates.slice();
let artdtsch = e.features[0].properties.artdtsch;
let strname = e.features[0].properties.strname;
let hausnr = e.features[0].properties.hausnr;
let wassersumme = e.features[0].properties.wassersumme;
let anzahlG = e.features[0].properties.anzahlG;
let alter = e.features[0].properties.standalter;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates based on the feature
popup
.setLngLat(coordinates)
.setHTML(
"<p style='background-color: #35de89; opacity: 0.8; color: white; padding: 0.5em 1em 0.5em 1em; display: block;'><strong>" +
artdtsch +
" (" +
alter +
" Jahre) " +
"</strong></p>" +
"<p><b> Anzahl Gießungen: " +
anzahlG +
"</b></p>" +
"<p><b> Erhaltene Wassermenge: " +
wassersumme +
" Liter</b></p><p>" +
strname +
" Nr. " +
hausnr +
"</p>"
)
.addTo(map);
});
map.on("mouseleave", "watered-trees", function () {
map.getCanvas().style.cursor = "";
popup.remove();
});
</script>
</body>
</html>