-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhubeau_temperature_viewer.html
292 lines (254 loc) · 10.2 KB
/
hubeau_temperature_viewer.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Stations température Hub'Eau</title>
</head>
<body>
<nav class="navbar navbar-dark bg-dark rounded" style="height: 7vh;">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">
<img src="https://cdn-icons-png.flaticon.com/512/179/179181.png" alt="" width="30" height="30"
class="d-inline-block align-text-top">
Stations température Hub'Eau
</a>
</div>
</nav>
<div class="container-fluid mx-0 px-0">
<div class="row">
<div class="col">
<div id="mapid" style="width: 100%; height: 93vh;"></div>
</div>
<div class="col">
<div id="loading-station">
<div class="spinner-grow text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<b>Stations température de Hub'Eau en cours de chargement...</b>
</div>
<div id="loaded-station" style="display: none;">
<h1>Détail station</h1>
<div>Cliquer sur une station pour afficher le détail de ses températures.</div>
<div>Les stations et leurs données sont récupérées automatiquement à partir de l'API
<a href="https://hubeau.eaufrance.fr/page/api-temperature-continu" target="_blank">"Température des cours d'eau"</a>
de <a href="https://hubeau.eaufrance.fr/page/api-temperature-continu" target="_blank">Hub'Eau</a>.
</div>
<div id="station-detail"></div>
<div class="form" id="form-graph" style="display: none;">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Date de début</label>
<input type="date" id="start" name="date-start" class="form-control"/>
</div>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Date de fin</label>
<input type="date" id="end" name="date-end" class="form-control"/>
</div>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Nombre de données</label>
<input type="number" id="nb_donnees" name="nb-donnees" value="10000" class="form-control"/>
</div>
<button type="submit" class="btn btn-primary" onclick="generateGraph()">Regénérer le graphique</button>
<button type="submit" class="btn btn-primary" onclick="download()">Télécharger les données</button>
</div>
<div id="graph-temp"></div>
<div class="d-flex justify-content-center">
<div class="spinner-grow text-primary" role="status" id="graph-spinner" style="display: none;">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- script and library-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<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>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<script src="https://cdn.plot.ly/plotly-2.27.0.min.js" charset="utf-8"></script>
<script>
var current_station = undefined;
const basemaps = {
Scan25: L.tileLayer(
"https://wxs.ign.fr/5a3efc2bq1dv1hz6gthcb445/wmts?&REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&STYLE=normal&TILEMATRIXSET=PM&FORMAT=image/jpeg&LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",
{ opacity: 0.5 },
{ minZoom: 0, maxZoom: 18, attribution: "IGN-F/Geoportail", tileSize: 256 }
),
Plan_ign: L.tileLayer(
"https://wxs.ign.fr/cartes/wmts?&REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&STYLE=normal&TILEMATRIXSET=PM&FORMAT=image/png&LAYER=GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",
{ minZoom: 0, maxZoom: 18, attribution: "IGN-F/Geoportail", tileSize: 256 }
),
orthoPhoto: L.tileLayer(
"https://wxs.ign.fr/ortho/wmts?&REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&STYLE=normal&TILEMATRIXSET=PM&FORMAT=image/jpeg&LAYER=ORTHOIMAGERY.ORTHOPHOTOS&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",
{ minZoom: 0, maxZoom: 18, attribution: "IGN-F/Geoportail", tileSize: 256 }
),
OpenStreetMap: L.tileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{ attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }
),
OpenTopoMap: L.tileLayer(
'http://a.tile.opentopomap.org/{z}/{x}/{y}.png',
{ attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors | Design : © <a href="http://opentopomap.org/">OpenTopoMap</a> (CC-BY-SA)' }
)
};
var map = L.map('mapid', {
layers: [
basemaps.Plan_ign
]
}).setView([45.7, 3.72], 6);
var layerControl = L.control.layers(basemaps).addTo(map);
option = {
onEachFeature: function (feature, layer) {
if ((feature.properties, { noHide: false })) {
layer.bindPopup(
`<div class='text-center'><h3><small>
${feature.properties.libelle_station}
</small></h3>
<button class="btn btn-primary" onclick="get_detail_station('${feature.properties.code_station}')">Détail</button>
<div>`
);
}
},
};
$.getJSON(
'https://hubeau.eaufrance.fr/api/v1/temperature/station?exact_count=true&format=geojson&size=2000',
function (geojson) {
var outdoor = L.geoJSON(geojson, option);
outdoor.addTo(map);
document.getElementById("loading-station").style.display = "none";
document.getElementById("loaded-station").style.display = "block";
}
);
function get_detail_station(code_station) {
current_station=code_station;
fetch(`https://hubeau.eaufrance.fr/api/v1/temperature/station?format=json&code_station=${code_station}&limit=1`)
.then(response => {
return response.json()
})
.then((data) => {
var detail_station = data.data[0];
detail = document.getElementById('station-detail');
detail.innerHTML = `
<div><b>Nom : </b>${detail_station.libelle_station}</div>
<div><b>localisation : </b>${detail_station.localisation}</div>
<div><b>Cours d'eau : </b>${detail_station.libelle_cours_eau}</div>
`
})
detail = document.getElementById('station-detail');
generateGraph()
document.getElementById("form-graph").style.display = "block";
}
function download() {
const url = `https://hubeau.eaufrance.fr/api/v1/temperature/chronique.csv?code_station=${current_station}&limit=20000&sort=desc`
// Download the CSV file
let anchor = document.createElement('a');
anchor.href = 'data:text/csv;charset=utf-8,' + encodeURI(url);
anchor.target = '_blank';
anchor.download = 'chronique.csv';
anchor.click();
}
function get_extra_params() {
// Get query params
extra_params = {}
date_debut = document.getElementById("start").value
if (date_debut) {
extra_params["date_debut_mesure"] = date_debut
}
date_fin = document.getElementById("end").value
if (date_fin) {
extra_params["date_fin_mesure"] = date_fin
}
return extra_params
}
function generateGraph() {
document.getElementById("graph-spinner").style.display = "block";
document.getElementById('graph-temp').style.display = "none";
let graph_data = {
"x": [],
"y": [],
}
get_data(current_station, 1, graph_data)
}
function get_data(code_station, page, graph_data) {
// Get query params
extra_params = get_extra_params()
nb_donnees = document.getElementById("nb_donnees").value
if (nb_donnees == -1) {
nb_pages = 300
}
else {
nb_pages = (nb_donnees / 10000 ) + 1
}
fetch(`https://hubeau.eaufrance.fr/api/v1/temperature/chronique?format=json&code_station=${code_station}&sort=desc&limit=10000&page=${page}&`+ new URLSearchParams(extra_params) )
.then(response => {
return response.json()
})
.then((data) => {
if ((data.data.length ==0) || (page > nb_pages)) {
create_graph(graph_data["x"], graph_data["y"]);
document.getElementById("graph-spinner").style.display = "none";
document.getElementById('graph-temp').style.display = "block";
}
else {
populate_data(data.data, graph_data);
get_data(code_station, page+1, graph_data)
}
})
}
function populate_data(data, graph_data) {
data.forEach(element => {
if ((element["resultat"] > 0) && (element["resultat"] <30)) {
graph_data["x"].push(element["date_mesure_temp"]+"T"+element["heure_mesure_temp"])
graph_data["y"].push(element["resultat"])
}
});
}
function create_graph(data_x, data_y) {
graph_div = document.getElementById('graph-temp');
Plotly.newPlot( graph_div, [{
x: data_x,
y: data_y
}], {
margin: { t: 0 },
xaxis: {
hoverformat: '%Y-%m-%dT%H:%S',
rangeslider: {
bordercolor: '#000',
bgcolor: '#d9d9d930',
borderwidth: 1,
},
}, } );
}
</script>
<style>
.leaflet-bottom {
position: fixed;
}
.legend {
background-color: rgba(255, 255, 255, 0.7);
padding: 15px;
}
.legend div label,
.legend div span {
display: block;
text-align: left;
font-size: 9px;
}
.legend div span {
height: 15px;
width: 30px;
margin-right: 5px;
}
.tiles {
z-index: 100;
}
</style>
</body>
</html>