Debian Codegraph @

extras/find-fixme/web/www/fixme.openlayers.js

raw
1 //
2 // Routino (extras) fixme web page Javascript
3 //
4 // Part of the Routino routing software.
5 //
6 // This file Copyright 2008-2014, 2019, 2020, 2023 Andrew M. Bishop
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 //
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21
22
23 ////////////////////////////////////////////////////////////////////////////////
24 /////////////////////////////// Initialisation /////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////
26
27 // Process the URL query string and extract the arguments
28
29 var legal={"^lon" : "^[-0-9.]+$",
30 "^lat" : "^[-0-9.]+$",
31 "^zoom" : "^[-0-9.]+$"};
32
33 var args={};
34
35 if(location.search.length>1)
36 {
37 var query,queries;
38
39 query=location.search.replace(/^\?/,"");
40 query=query.replace(/;/g,"&");
41 queries=query.split("&");
42
43 for(var i=0;i<queries.length;i++)
44 {
45 queries[i].match(/^([^=]+)(=(.*))?$/);
46
47 var k=RegExp.$1;
48 var v=decodeURIComponent(RegExp.$3);
49
50 for(var l in legal)
51 {
52 if(k.match(RegExp(l)) && v.match(RegExp(legal[l])))
53 args[k]=v;
54 }
55 }
56 }
57
58
59 ////////////////////////////////////////////////////////////////////////////////
60 ///////////////////////////////// Map handling /////////////////////////////////
61 ////////////////////////////////////////////////////////////////////////////////
62
63 var map;
64 var layerMap=[], layerHighlights, layerVectors;
65 var vectorData=[];
66
67 //
68 // Initialise the 'map' object
69 //
70
71 function map_init() // called from fixme.html
72 {
73 // Create the map (Map URLs and limits are in mapprops.js)
74
75 var extent = ol.extent.boundingExtent([ol.proj.fromLonLat([mapprops.westedge,mapprops.southedge]),
76 ol.proj.fromLonLat([mapprops.eastedge,mapprops.northedge])]);
77
78 map = new ol.Map({target: "map",
79 view: new ol.View({minZoom: mapprops.zoomout,
80 maxZoom: mapprops.zoomin,
81 constrainResolution: true,
82 extent: extent}),
83 controls: []
84 });
85
86 // Add map tile layers
87
88 for(var l=mapprops.mapdata.length-1; l>=0; l--)
89 {
90 mapprops.mapdata[l].tiles.url=mapprops.mapdata[l].tiles.url.replace(/\$\{/g,"{");
91 var urls;
92
93 if(mapprops.mapdata[l].tiles.subdomains===undefined)
94 urls=[mapprops.mapdata[l].tiles.url];
95 else
96 {
97 urls=[];
98
99 for(var s=0; s<mapprops.mapdata[l].tiles.subdomains.length; s++)
100 urls.push(mapprops.mapdata[l].tiles.url.replace(/\{s}/,mapprops.mapdata[l].tiles.subdomains[s]));
101 }
102
103 layerMap[l] = new ol.layer.Tile({title: mapprops.mapdata[l].label,
104 type: 'base',
105 source: new ol.source.XYZ({urls: urls}),
106 visible: l?false:true});
107
108 map.addLayer(layerMap[l]);
109 }
110
111 // Add the controls
112
113 map.addControl(new ol.control.Zoom());
114 map.addControl(new ol.control.ScaleLine());
115 map.addControl(new ol.control.LayerSwitcher());
116
117 // Update the attribution if the layer changes
118
119 function change_attribution_event(event)
120 {
121 for(var l=0; l<mapprops.mapdata.length; l++)
122 if(layerMap[l].getVisible())
123 change_attribution(l);
124 }
125
126 map.getLayerGroup().on("change", change_attribution_event);
127
128 function change_attribution(l)
129 {
130 var data_url =mapprops.mapdata[l].attribution.data_url;
131 var data_text=mapprops.mapdata[l].attribution.data_text;
132 var tile_url =mapprops.mapdata[l].attribution.tile_url;
133 var tile_text=mapprops.mapdata[l].attribution.tile_text;
134
135 document.getElementById("attribution_data").innerHTML="<a href=\"" + data_url + "\" target=\"data_attribution\">" + data_text + "</a>";
136 document.getElementById("attribution_tile").innerHTML="<a href=\"" + tile_url + "\" target=\"tile_attribution\">" + tile_text + "</a>";
137 }
138
139 change_attribution(0);
140
141 // Add two vectors layers (one for highlights that display behind the vectors)
142
143 layerHighlights = new ol.layer.Vector({source: new ol.source.Vector()});
144
145 map.addLayer(layerHighlights);
146
147 layerVectors = new ol.layer.Vector({source: new ol.source.Vector()});
148
149 map.addLayer(layerVectors);
150
151 // Handle feature selection and popup
152
153 map.on("click", function(e) { var first=true; map.forEachFeatureAtPixel(e.pixel, function (feature, layer) { if(first) selectFeature(feature); first=false; }) });
154
155 createPopup();
156
157 // Move the map
158
159 map.on("moveend", (function() { displayMoreData();}), map);
160
161 var lon =args["lon"];
162 var lat =args["lat"];
163 var zoom=args["zoom"];
164
165 if(lon !== undefined && lat !== undefined && zoom !== undefined)
166 {
167 lat = Number(lat);
168 lon = Number(lon);
169 zoom = Number(zoom);
170
171 if(lon<mapprops.westedge) lon=mapprops.westedge;
172 if(lon>mapprops.eastedge) lon=mapprops.eastedge;
173
174 if(lat<mapprops.southedge) lat=mapprops.southedge;
175 if(lat>mapprops.northedge) lat=mapprops.northedge;
176
177 if(zoom<mapprops.zoomout) zoom=mapprops.zoomout;
178 if(zoom>mapprops.zoomin) zoom=mapprops.zoomin;
179
180 map.getView().setCenter(ol.proj.fromLonLat([lon,lat]));
181 map.getView().setZoom(zoom);
182 }
183 else
184 map.getView().fit(extent,map.getSize());
185
186 // Unhide editing URL if variable set
187
188 if(mapprops.editurl !== undefined && mapprops.editurl !== "")
189 {
190 var edit_url=document.getElementById("edit_url");
191
192 edit_url.style.display="";
193 edit_url.href=mapprops.editurl;
194 }
195
196 updateURLs(false);
197 }
198
199
200 //
201 // Format a number in printf("%.5f") format.
202 //
203
204 function format5f(number)
205 {
206 var newnumber=Math.floor(number*100000+0.5);
207 var delta=0;
208
209 if(newnumber>=0 && newnumber<100000) delta= 100000;
210 if(newnumber<0 && newnumber>-100000) delta=-100000;
211
212 var string=String(newnumber+delta);
213
214 var intpart =string.substring(0,string.length-5);
215 var fracpart=string.substring(string.length-5,string.length);
216
217 if(delta>0) intpart="0";
218 if(delta<0) intpart="-0";
219
220 return(intpart + "." + fracpart);
221 }
222
223
224 //
225 // Build a set of URL arguments for the map location
226 //
227
228 function buildMapArguments()
229 {
230 var lonlat = ol.proj.toLonLat(map.getView().getCenter());
231
232 var zoom = map.getView().getZoom();
233
234 if( ! Number.isInteger(zoom) )
235 zoom = format5f(zoom);
236
237 return "lat=" + format5f(lonlat[1]) + ";lon=" + format5f(lonlat[0]) + ";zoom=" + zoom;
238 }
239
240
241 //
242 // Update the URLs
243 //
244
245 function updateURLs(addhistory)
246 {
247 var mapargs=buildMapArguments();
248 var libargs=";library=" + mapprops.library;
249
250 if(!mapprops.libraries)
251 libargs="";
252
253 var links=document.getElementsByTagName("a");
254
255 for(var i=0; i<links.length; i++)
256 {
257 var element=links[i];
258
259 if(element.id == "permalink_url")
260 element.href=location.pathname + "?" + mapargs + libargs;
261
262 if(element.id == "edit_url")
263 element.href=mapprops.editurl + "?" + mapargs;
264 }
265
266 if(addhistory)
267 history.replaceState(null, null, location.pathname + "?" + mapargs + libargs);
268 }
269
270
271 ////////////////////////////////////////////////////////////////////////////////
272 ///////////////////////// Popup and selection handling /////////////////////////
273 ////////////////////////////////////////////////////////////////////////////////
274
275 var popup=null;
276
277 //
278 // Create a popup - independent of map because want it fixed on screen not fixed on map.
279 //
280
281 function createPopup()
282 {
283 popup=document.createElement("div");
284
285 popup.className = "popup";
286
287 popup.innerHTML = "<span></span>";
288
289 popup.style.display = "none";
290
291 popup.style.position = "fixed";
292 popup.style.top = "-4000px";
293 popup.style.left = "-4000px";
294 popup.style.zIndex = "100";
295
296 popup.style.padding = "5px";
297
298 popup.style.opacity=0.85;
299 popup.style.backgroundColor="#C0C0C0";
300 popup.style.border="4px solid #404040";
301
302 document.body.appendChild(popup);
303 }
304
305
306 //
307 // Draw a popup - independent of map because want it fixed on screen not fixed on map.
308 //
309
310 function drawPopup(html)
311 {
312 if(html===null)
313 {
314 popup.style.display="none";
315 return;
316 }
317
318 if(popup.style.display=="none")
319 {
320 var map_div=document.getElementById("map");
321
322 popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px";
323 popup.style.top = map_div.offsetTop +30 + "px";
324 popup.style.width =map_div.clientWidth-120 + "px";
325
326 popup.style.display="";
327 }
328
329 var close="<span style='float: right; cursor: pointer;' onclick='drawPopup(null)'>X</span>";
330
331 popup.innerHTML=close+html;
332 }
333
334
335 //
336 // Select a feature
337 //
338
339 function selectFeature(feature)
340 {
341 if(feature.get('dump'))
342 ajaxGET("fixme.cgi?dump=" + feature.get('dump'), runDumpSuccess);
343
344 layerHighlights.getSource().clear();
345
346 var style = new ol.style.Style({stroke: new ol.style.Stroke({width: 8, color: "#F0F000"}),
347 image: new ol.style.Circle({fill: new ol.style.Fill({color: "#F0F000"}),
348 radius: 5})});
349
350 var highlight = new ol.Feature(feature.getGeometry());
351
352 highlight.setStyle(style);
353
354 layerHighlights.getSource().addFeature(highlight);
355 }
356
357
358 //
359 // Un-select a feature
360 //
361
362 function unselectFeature(feature)
363 {
364 layerHighlights.getSource().clear();
365
366 drawPopup(null);
367 }
368
369
370 //
371 // Display the dump data
372 //
373
374 function runDumpSuccess(response)
375 {
376 var string=response.responseText;
377
378 if(mapprops.browseurl !== undefined && mapprops.browseurl !== "")
379 {
380 var types=["node", "way", "relation"];
381
382 for(var t in types)
383 {
384 var type=types[t];
385
386 var regexp=RegExp(type + " id=&#39;([0-9]+)&#39;");
387
388 var match=string.match(regexp);
389
390 if(match !== null)
391 {
392 var id=match[1];
393
394 string=string.replace(regexp,type + " id=&#39;<a href='" + mapprops.browseurl + "/" + type + "/" + id + "' target='" + type + id + "'>" + id + "</a>&#39;");
395 }
396 }
397 }
398
399 drawPopup(string.split("&gt;&lt;").join("&gt;<br>&lt;").split("<br>&lt;tag").join("<br>&nbsp;&nbsp;&lt;tag"));
400 }
401
402
403 ////////////////////////////////////////////////////////////////////////////////
404 /////////////////////////////// Server handling ////////////////////////////////
405 ////////////////////////////////////////////////////////////////////////////////
406
407 //
408 // Define an AJAX request object
409 //
410
411 function ajaxGET(url,success,failure,state)
412 {
413 var ajaxRequest=new XMLHttpRequest();
414
415 function ajaxGOT(options) {
416 if(this.readyState==4)
417 if(this.status==200)
418 { if(typeof(options.success)=="function") options.success(this,options.state); }
419 else
420 { if(typeof(options.failure)=="function") options.failure(this,options.state); }
421 }
422
423 ajaxRequest.onreadystatechange = function(){ ajaxGOT.call(ajaxRequest,{success: success, failure: failure, state: state}); };
424 ajaxRequest.open("GET", url, true);
425 ajaxRequest.send(null);
426 }
427
428
429 //
430 // Display the status
431 //
432
433 function displayStatus(type,subtype,content)
434 {
435 var child=document.getElementById("result_status").firstChild;
436
437 do
438 {
439 if(child.id !== undefined)
440 child.style.display="none";
441
442 child=child.nextSibling;
443 }
444 while(child !== null);
445
446 var chosen_status=document.getElementById("result_status_" + type);
447
448 chosen_status.style.display="";
449
450 if(subtype !== undefined)
451 {
452 var format_status=document.getElementById("result_status_" + subtype).innerHTML;
453
454 chosen_status.innerHTML=format_status.replace("#",String(content));
455 }
456 }
457
458
459 //
460 // Display data statistics
461 //
462
463 function displayStatistics()
464 {
465 // Use AJAX to get the statistics
466
467 ajaxGET("fixme.cgi?statistics=yes", runStatisticsSuccess);
468 }
469
470
471 //
472 // Success in running data statistics generation.
473 //
474
475 function runStatisticsSuccess(response)
476 {
477 document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>";
478 document.getElementById("statistics_link").style.display="none";
479 }
480
481
482 //
483 // Get the requested data
484 //
485
486 function displayData(datatype) // called from fixme.html
487 {
488 // Delete the old data
489
490 vectorData=[];
491
492 unselectFeature();
493
494 layerVectors.getSource().clear();
495
496 // Print the status
497
498 displayStatus("no_data");
499
500 // Return if just here to clear the data
501
502 if(datatype === "")
503 return;
504
505 displayMoreData();
506 }
507
508
509 function displayMoreData()
510 {
511 // Get the new data
512
513 var mapbounds=map.getView().calculateExtent(map.getSize());
514
515 var url="fixme.cgi";
516
517 url=url + "?lonmin=" + format5f(ol.proj.toLonLat([mapbounds[0],mapbounds[1]])[0]);
518 url=url + ";latmin=" + format5f(ol.proj.toLonLat([mapbounds[0],mapbounds[1]])[1]);
519 url=url + ";lonmax=" + format5f(ol.proj.toLonLat([mapbounds[2],mapbounds[3]])[0]);
520 url=url + ";latmax=" + format5f(ol.proj.toLonLat([mapbounds[2],mapbounds[3]])[1]);
521 url=url + ";data=fixmes";
522
523 // Use AJAX to get the data
524
525 ajaxGET(url, runFixmeSuccess, runFailure);
526
527 updateURLs(true);
528 }
529
530
531 //
532 // Success in getting the error log data
533 //
534
535 function runFixmeSuccess(response)
536 {
537 var lines=response.responseText.split("\n");
538
539 var style = new ol.style.Style({image: new ol.style.Circle({fill: new ol.style.Fill({color: "#FF0000"}),
540 radius: 3})});
541
542 var features=[];
543
544 for(var line=0;line<lines.length;line++)
545 {
546 var words=lines[line].split(" ");
547
548 if(line === 0)
549 continue;
550 else if(words[0] !== "")
551 {
552 var dump=words[0];
553
554 if(vectorData[dump])
555 continue;
556 else
557 vectorData[dump]=1;
558
559 var lat=Number(words[1]);
560 var lon=Number(words[2]);
561
562 var geometry = new ol.geom.Point(ol.proj.fromLonLat([lon,lat]));
563
564 var feature = new ol.Feature({geometry: geometry, dump: dump});
565
566 feature.setStyle(style);
567
568 features.push(feature);
569 }
570 }
571
572 layerVectors.getSource().addFeatures(features);
573
574 displayStatus("data","fixme",Object.keys(vectorData).length);
575 }
576
577
578 //
579 // Failure in getting data.
580 //
581
582 function runFailure(response)
583 {
584 displayStatus("failed");
585 }