-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairCologneMapOverview.html
More file actions
396 lines (353 loc) · 14.1 KB
/
airCologneMapOverview.html
File metadata and controls
396 lines (353 loc) · 14.1 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
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="http://openlayers.org/en/v3.3.0/css/ol.css" type="text/css">
<style>
.ol-popup {
position: absolute;
background-color: white;
-webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
padding: 15px;
border-radius: 10px;
border: 1px solid #cccccc;
bottom: 12px;
left: -50px;
min-width: 280px;
}
.ol-popup:after,
.ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}
.ol-popup-closer:after {
content: "X";
}
.map {
height: 512px;
width: 512px;
}
.divT {
background-color: white;
height: 30px;
}
.half-map {
float: left;
height: calc(50% - 100px);
width: 49%;
background-color: #b5d0d0;
border: solid 1px black;
}
.half-map2 {
height: calc(50% - 100px);
width: 50%;
background-color: #b5d0d0;
}
.mouse-position {
position: absolute;
left: 70px;
top: 80px;
z-index: 1000;
font-weight: bold;
background-color: rgba(0, 60, 136, 0.298039);
padding: 5px;
border-radius: 4px;
}
</style>
<script src="http://openlayers.org/en/v3.3.0/build/ol-debug.js" type="text/javascript"></script>
<script src="https://cdn.polyfill.io/v2/polyfill.js?features=features=default,fetch"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>SensorMapOverview</title>
</head>
<body>
<h2>OpenAirCologneTestÜbersicht - Click Sensor</h2>
<div id="map" class="map">
</div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
<div id="mouse-position" class="mouse-position"></div>
<script type="text/javascript">
var sensors;
var layerSensor;
var getStyle;
var vectorsensors;
var map;
// kein cors bisher bei openair.cologne, deswegen mit JSONP
// Probleme mit dem IE und Edge
var url = "https://openair.cologne/api";
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
}).done(function(answer) {
sensors = answer;
console.log(sensors);
getMap();
});
/**
* Elements that make up the popup.
*/
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
/**
* Create an overlay to anchor the popup to the map.
*/
var overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
/**
* Add a click handler to hide the popup.
* @return {boolean} Don't follow the href.
*/
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
function getMap() {
var stroke = new ol.style.Stroke({
color: 'black',
width: 2
});
var fillR = new ol.style.Fill({
color: 'red'
});
var fillY = new ol.style.Fill({
color: 'yellow'
});
var fillG = new ol.style.Fill({
color: 'green'
});
var fillNull = new ol.style.Fill({
color: 'grey'
});
var starR = new ol.style.Style({
image: new ol.style.RegularShape({
fill: fillR,
stroke: stroke,
points: 5,
radius: 10,
radius2: 4,
angle: 0
})
});
starG = new ol.style.Style({
image: new ol.style.RegularShape({
fill: fillG,
stroke: stroke,
points: 5,
radius: 10,
radius2: 4,
angle: 0
})
});
starNull = new ol.style.Style({
image: new ol.style.RegularShape({
fill: fillNull,
stroke: stroke,
points: 5,
radius: 10,
radius2: 4,
angle: 0
})
});
vectorsensors = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(sensors, {
featureProjection: 'EPSG:3857'
}),
attributions: [new ol.Attribution({
html: "<br/><a href=\"http://openair.codingcologne.de\">OpenAir Cologne Contributors, </a>"
})]
});
layerSensor = new ol.layer.Vector({
source: vectorsensors,
style: starNull,
projection: 'EPSG:3857'
});
var layerOsm = new ol.layer.Tile({
source: new ol.source.OSM({url:'https://tile.openstreetmap.org/{z}/{x}/{y}.png'})
});
var view = new ol.View({
center: ol.proj.transform([6.9619, 50.9389], 'EPSG:4326', 'EPSG:3857'),
zoom: 11
});
map = new ol.Map({
target: 'map',
overlays: [overlay],
layers: [layerOsm, layerSensor],
view: view,
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: true
})
}).extend([
new ol.control.ScaleLine(),
new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(4),
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position')
})
])
});
map.on('singleclick', function(evt) {
var coordinate = evt.coordinate;
var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
coordinate, 'EPSG:3857', 'EPSG:4326'));
var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
//you can add a condition on layer to restrict the listener
return feature;
});
function timeConverter(UNIX_timestamp) {
var a = new Date(UNIX_timestamp);
var months = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec;
return time;
}
if (feature) {
fetch("https://babeauf.nt.fh-koeln.de/SensorCloud/Messwert/Last/SenID/" + feature.values_.sensor_id)
// wegen ie arrow function raus
//.then((resp) => resp.json()) // Transform the data into json
.then(function(resp) {
return resp.json();
})
.then(function(data) {
// Here you get the data to modify as you please
var contentAir = '<p>Letzter gemessener Wert:</p>';
console.log(data);
for (i = 0; i < data.length; i++) {
var splitter = data[i].n.split(":");
contentAir += splitter[1] + ': ' + data[i].sv + '</br>';
}
contentAir += '</br>Am ' + timeConverter(parseInt(data[1].t));
//contentAir += '<p>'+feature.values_.sensor_id+'</p>';
contentAir += '<p>MEZ nicht berücksichtigt</p>';
content.innerHTML = contentAir;
overlay.setPosition(coordinate);
})
.catch(function(error) {
// If there is any error you will catch them here
});
}
});
// load all the single values
map.once('postrender', function(event) {
// timeout da die Kacheln noch gertig geladen werden müssen
setTimeout(function() {
promiseAll();
}, 2000);
});
function get(url) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
if (req.status == 200) {
var jsonAr = JSON.parse(req.response);
// kein responseUrl im IE
// id = req.responseURL
//var idA = req.responseURL.split("/");
var idA = url.split("/");
var id = idA[7];
// values = req.response
jsonAr.forEach(function(property) {
var pA = property.n.split(":");
var component = pA[1];
var cVal = property.sv;
for (i = 0; i < sensors.features.length; i++) {
// console.log(sensors.features[i].properties.sensor_id + " "+ id);
if (vectorsensors.getFeatures()[i].values_.sensor_id == id) {
//console.log('id ' + id + 'vectorsensors ', vectorsensors.getFeatures()[i]);
vectorsensors.getFeatures()[i].setProperties({
date: property.t
});
}
}
});
// console.log(sensors.features);
resolve(req.response);
} else {
//reject(Error(req.statusText));
// fehler ignorieren
resolve(Error(req.statusText));
}
};
req.onerror = function() {
//reject(Error("Network Error"));
// fehler ignorieren
resolve(Error(req.statusText));
};
req.send();
});
}
// get all values of the sensors
function promiseAll() {
var urls = [];
sensors.features.forEach(function(feature) {
urls.push(get('https://babeauf.nt.fh-koeln.de/SensorCloud/Messwert/Last/SenID/' + feature.properties.sensor_id + ''));
});
//console.log(urls);
// arrow raus wegen ie
//Promise.all(urls).then(values => {
Promise.all(urls).then(function(values) {
layerSensor.getSource().forEachFeature(function(feature) {
var style;
//console.log('sensor ' + Number(feature.values_.date));
// 1h = 3600000 ms , 3 h = 10800000
var dann = Date.now() - 10800000;
//var dann = Date.now();
if (Number(feature.values_.date) > (Date.now() - 10800000)) {
style = starG;
}
if (Number(feature.values_.date) <= (Date.now() - 10800000)) {
style = starR;
}
if (!Number(feature.values_.date)) {
console.log('Test ', feature.values_);
style = starNull;
}
feature.setStyle(style);
});
});
}
}
</script>
</body>
</html>