-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-dwd-map-with-layers.html
258 lines (240 loc) · 8.95 KB
/
simple-dwd-map-with-layers.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
<!DOCTYPE html>
<html>
<head>
<title>DWD Unwetterwarnungen</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;}
#map {
width: 100%;
position: relative;
z-index: 1;}
#legend {
position: absolute;
bottom: 5px;
left: 5px;
background: white;
padding: 5px;
border: 1px solid #ccc;
z-index: 2;
font-size: 12px;}
.legendBox {
display: flex;
margin-bottom: 5px;}
.legendBox table {
flex: 1;
border-collapse: collapse;}
.legendBox table td {
padding: 3px;
border: 1px solid #ccc;
white-space: nowrap;
font-size: 10px;}
.box {
width: 16px;
height: 16px;
display: inline-block;
margin-right: 3px;
vertical-align: middle;}
.legendBox p {
margin: 0; }
.legendBox img {
vertical-align: middle;
margin-right: 3px;}
.darkred {
background-color: darkred;}
.red {
background-color: red;}
.orange {
background-color: orange;}
.yellow {
background-color: yellow;}
.violet_hitze {
background-color: violet;}
.violet_uv {
background-color: violet;}
.green {
background-color: green;}
.#layerControlContainer {
position: absolute;
top: 10px;
right: 10px;
background-color: white;
padding: 10px;
z-index: 1000;
border: 1px solid #ccc;
border-radius: 4px;
max-width: 300px;}
.leaflet-control-layers-list {
max-height: 400px;
overflow-y: auto;
direction: rtl;}
.leaflet-control-layers-list label {
display: block;
margin-bottom: 5px;
padding: 3px 5px;}
.leaflet-control-layers-list input[type="checkbox"] {
margin-right: 5px;}
.vorab { width: 16px;
height: 16px;
position: relative;
background-color: white;
vertical-align: middle;
border: 1px solid red;
background-image: linear-gradient(137deg, #ff0000 12.50%, #ffffff 12.50%, #ffffff 50%, #ff0000 50%, #ff0000 62.50%, #ffffff 62.50%, #ffffff 100%);
background-size: 5.87px 5.47px;}
@media (max-width: 400px) {
#legend {
font-size: 10px;
max-width: 150px;
left: 1px !important;}
.legendBox table td {
padding: 2px;
font-size: 8px; }
.box {
width: 12px;
height: 12px; }}
</style>
</head>
<body>
<div id="map"></div>
<div id="legend">
<div class="legendBox">
<table>
<tbody>
<tr>
<td><div class="box darkred" title="Extrem Unwetter (Stufe 4)"></div></td>
<td>Extrem Unwetter (Stufe 4)</td>
</tr>
<tr>
<td><div class="box red" title="Unwetter (Stufe 3)"></div></td>
<td>Unwetter (Stufe 3)</td>
</tr>
<tr>
<td><div class="box orange" title="Markantes Wetter (Stufe 2)"></div></td>
<td>Markantes Wetter (Stufe 2)</td>
</tr>
<tr>
<td><div class="box yellow" title="Wetterwarnungen (Stufe 1)"></div></td>
<td>Wetterwarnungen (Stufe 1)</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td><div class="vorab"></div></td>
<td>Vorabinformation Unwetter</td>
</tr>
<tr>
<td><div class="box violet_hitze" title="Hitzewarnungen"></div></td>
<td>Hitzewarnung</td>
</tr>
<tr>
<td><div class="box violet_uv" title="UV-Warnungen"></div></td>
<td>UV-Warnung</td>
</tr>
<tr>
<td><div class="box green" title="Keine Warnungen"></div></td>
<td>Keine Warnungen</td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
var map = L.map('map').setView([51.1657, 10.4515], 6); // Mitte von Deutschland
// CartoDB Positron TileLayer
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
maxZoom: 18,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>'
}).addTo(map);
var wmsLayer = null;
// Fetch GetCapabilities and add layers
function fetchAndAddLayers() {
fetch('https://maps.dwd.de/geoserver/ows?service=WMS&version=1.3.0&request=GetCapabilities')
.then(response => response.text())
.then(text => (new window.DOMParser()).parseFromString(text, "text/xml"))
.then(xml => {
var layers = xml.querySelectorAll('Layer > Name');
var layerControl = L.control.layers().addTo(map);
layers.forEach(layer => {
var layerName = layer.textContent;
var newWmsLayer = L.tileLayer.wms("https://maps.dwd.de/geoserver/ows?", {
layers: layerName,
format: 'image/png',
transparent: true,
version: '1.3.0',
attribution: "DWD",
opacity: 0.5
});
layerControl.addOverlay(newWmsLayer, layerName);
// Select & check default layer "Warnungen_Gemeinden"
if (layerName === 'dwd:Warnungen_Gemeinden') {
map.addLayer(newWmsLayer);
wmsLayer = newWmsLayer;
}
});
})
.catch(error => console.error('Error fetching GetCapabilities:', error));
}
fetchAndAddLayers();
// Change opacity of the currently active layer
function changeLayerOpacity(opacity) {
if (wmsLayer) {
wmsLayer.setOpacity(opacity);
}
}
changeLayerOpacity(0.7);
map.on('click', function(e) {
var url = getFeatureInfoUrl(e.latlng);
fetch(url)
.then(response => response.text())
.then(data => {
var content = parseGetFeatureInfoResponse(data);
if (content.trim() !== "") {
L.popup()
.setLatLng(e.latlng)
.setContent(content)
.openOn(map);
} else {
alert("No information available at this location.");
}
})
.catch(error => console.error('Error fetching GetFeatureInfo:', error));
});
function getFeatureInfoUrl(latlng) {
if (!wmsLayer) return '';
var point = map.latLngToContainerPoint(latlng, map.getZoom());
var size = map.getSize();
var params = {
request: 'GetFeatureInfo',
service: 'WMS',
srs: 'EPSG:4326',
styles: '',
transparent: true,
version: '1.3.0',
format: 'text/html',
bbox: map.getBounds().toBBoxString(),
height: size.y,
width: size.x,
layers: wmsLayer.options.layers,
query_layers: wmsLayer.options.layers,
info_format: 'text/html',
i: point.x,
j: point.y
};
return wmsLayer._url + L.Util.getParamString(params, wmsLayer._url, true);
}
function parseGetFeatureInfoResponse(data) {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = data;
var popupContent = tempDiv.querySelector('body').innerHTML;
return popupContent;
}
</script>
</body>
</html>