Debian Codegraph @

extras/find-fixme/web/www/fixme.leaflet.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 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 map = L.map("map",
76 {
77 attributionControl: false,
78 zoomControl: false,
79
80 minZoom: mapprops.zoomout,
81 maxZoom: mapprops.zoomin,
82
83 maxBounds: L.latLngBounds(L.latLng(mapprops.southedge,mapprops.westedge),L.latLng(mapprops.northedge,mapprops.eastedge))
84 });
85
86 // Add map tile layers
87
88 var baselayers={};
89
90 for(var l=0; l<mapprops.mapdata.length; l++)
91 {
92 var urls=mapprops.mapdata[l].tiles.url.replace(/\${/g,"{");
93
94 if(mapprops.mapdata[l].tiles.subdomains===undefined)
95 layerMap[l] = L.tileLayer(urls);
96 else
97 layerMap[l] = L.tileLayer(urls, {subdomains: mapprops.mapdata[l].tiles.subdomains});
98
99 baselayers[mapprops.mapdata[l].label]=layerMap[l];
100
101 if(l===0)
102 map.addLayer(layerMap[l]);
103 }
104
105 // Add the controls
106
107 map.addControl(L.control.zoom());
108 map.addControl(L.control.scale());
109 map.addControl(L.control.layers(baselayers));
110
111 // Update the attribution if the layer changes
112
113 function change_attribution_event(event)
114 {
115 for(var l=0; l<mapprops.mapdata.length; l++)
116 if(layerMap[l] == event.layer)
117 change_attribution(l);
118 }
119
120 map.on("baselayerchange",change_attribution_event);
121
122 function change_attribution(l)
123 {
124 var data_url =mapprops.mapdata[l].attribution.data_url;
125 var data_text=mapprops.mapdata[l].attribution.data_text;
126 var tile_url =mapprops.mapdata[l].attribution.tile_url;
127 var tile_text=mapprops.mapdata[l].attribution.tile_text;
128
129 document.getElementById("attribution_data").innerHTML="<a href=\"" + data_url + "\" target=\"data_attribution\">" + data_text + "</a>";
130 document.getElementById("attribution_tile").innerHTML="<a href=\"" + tile_url + "\" target=\"tile_attribution\">" + tile_text + "</a>";
131 }
132
133 change_attribution(0);
134
135 // Add two vectors layers (one for highlights that display behind the vectors)
136
137 layerVectors = L.layerGroup();
138 map.addLayer(layerVectors);
139
140 layerHighlights = L.layerGroup();
141 map.addLayer(layerHighlights);
142
143 // Handle popup
144
145 createPopup();
146
147 // Move the map
148
149 map.on("moveend", (function() { displayMoreData();}));
150
151 var lon =args["lon"];
152 var lat =args["lat"];
153 var zoom=args["zoom"];
154
155 if(lon !== undefined && lat !== undefined && zoom !== undefined)
156 {
157 lat = Number(lat);
158 lon = Number(lon);
159 zoom = Number.parseInt(Number(zoom)+0.5);
160
161 if(lon<mapprops.westedge) lon=mapprops.westedge;
162 if(lon>mapprops.eastedge) lon=mapprops.eastedge;
163
164 if(lat<mapprops.southedge) lat=mapprops.southedge;
165 if(lat>mapprops.northedge) lat=mapprops.northedge;
166
167 if(zoom<mapprops.zoomout) zoom=mapprops.zoomout;
168 if(zoom>mapprops.zoomin) zoom=mapprops.zoomin;
169
170 map.setView(L.latLng(lat,lon),zoom);
171 }
172 else
173 map.fitBounds(map.options.maxBounds);
174
175 // Unhide editing URL if variable set
176
177 if(mapprops.editurl !== undefined && mapprops.editurl !== "")
178 {
179 var edit_url=document.getElementById("edit_url");
180
181 edit_url.style.display="";
182 edit_url.href=mapprops.editurl;
183 }
184
185 updateURLs(false);
186 }
187
188
189 //
190 // Format a number in printf("%.5f") format.
191 //
192
193 function format5f(number)
194 {
195 var newnumber=Math.floor(number*100000+0.5);
196 var delta=0;
197
198 if(newnumber>=0 && newnumber<100000) delta= 100000;
199 if(newnumber<0 && newnumber>-100000) delta=-100000;
200
201 var string=String(newnumber+delta);
202
203 var intpart =string.substring(0,string.length-5);
204 var fracpart=string.substring(string.length-5,string.length);
205
206 if(delta>0) intpart="0";
207 if(delta<0) intpart="-0";
208
209 return(intpart + "." + fracpart);
210 }
211
212
213 //
214 // Build a set of URL arguments for the map location
215 //
216
217 function buildMapArguments()
218 {
219 var lonlat = map.getCenter();
220
221 var zoom = map.getZoom();
222
223 return "lat=" + format5f(lonlat.lat) + ";lon=" + format5f(lonlat.lng) + ";zoom=" + zoom;
224 }
225
226
227 //
228 // Update the URLs
229 //
230
231 function updateURLs(addhistory)
232 {
233 var mapargs=buildMapArguments();
234 var libargs=";library=" + mapprops.library;
235
236 if(!mapprops.libraries)
237 libargs="";
238
239 var links=document.getElementsByTagName("a");
240
241 for(var i=0; i<links.length; i++)
242 {
243 var element=links[i];
244
245 if(element.id == "permalink_url")
246 element.href=location.pathname + "?" + mapargs + libargs;
247
248 if(element.id == "edit_url")
249 element.href=mapprops.editurl + "?" + mapargs;
250 }
251
252 if(addhistory)
253 history.replaceState(null, null, location.pathname + "?" + mapargs + libargs);
254 }
255
256
257 ////////////////////////////////////////////////////////////////////////////////
258 ///////////////////////// Popup and selection handling /////////////////////////
259 ////////////////////////////////////////////////////////////////////////////////
260
261 var popup=null;
262
263 //
264 // Create a popup - independent of map because want it fixed on screen not fixed on map.
265 //
266
267 function createPopup()
268 {
269 popup=document.createElement("div");
270
271 popup.className = "popup";
272
273 popup.innerHTML = "<span></span>";
274
275 popup.style.display = "none";
276
277 popup.style.position = "fixed";
278 popup.style.top = "-4000px";
279 popup.style.left = "-4000px";
280 popup.style.zIndex = "100";
281
282 popup.style.padding = "5px";
283
284 popup.style.opacity=0.85;
285 popup.style.backgroundColor="#C0C0C0";
286 popup.style.border="4px solid #404040";
287
288 document.body.appendChild(popup);
289 }
290
291
292 //
293 // Draw a popup - independent of map because want it fixed on screen not fixed on map.
294 //
295
296 function drawPopup(html)
297 {
298 if(html===null)
299 {
300 popup.style.display="none";
301 return;
302 }
303
304 if(popup.style.display=="none")
305 {
306 var map_div=document.getElementById("map");
307
308 popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px";
309 popup.style.top = map_div.offsetTop +30 + "px";
310 popup.style.width =map_div.clientWidth-120 + "px";
311
312 popup.style.display="";
313 }
314
315 var close="<span style='float: right; cursor: pointer;' onclick='drawPopup(null)'>X</span>";
316
317 popup.innerHTML=close+html;
318 }
319
320
321 //
322 // Select a circleMarker feature
323 //
324
325 function selectCircleMarkerFeature(feature,dump,event)
326 {
327 if(dump)
328 ajaxGET("fixme.cgi?dump=" + dump, runDumpSuccess);
329
330 layerHighlights.clearLayers();
331
332 var highlight = L.circleMarker(feature.getLatLng(),{radius: 2*feature.getRadius(), fill: true, fillColor: "#F0F000", fillOpacity: 1.0,
333 stroke: false});
334
335 layerHighlights.addLayer(highlight);
336
337 highlight.bringToBack();
338 }
339
340
341 //
342 // Un-select a feature
343 //
344
345 function unselectFeature(feature)
346 {
347 layerHighlights.clearLayers();
348
349 drawPopup(null);
350 }
351
352
353 //
354 // Display the dump data
355 //
356
357 function runDumpSuccess(response)
358 {
359 var string=response.responseText;
360
361 if(mapprops.browseurl !== undefined && mapprops.browseurl !== "")
362 {
363 var types=["node", "way", "relation"];
364
365 for(var t in types)
366 {
367 var type=types[t];
368
369 var regexp=RegExp(type + " id=&#39;([0-9]+)&#39;");
370
371 var match=string.match(regexp);
372
373 if(match !== null)
374 {
375 var id=match[1];
376
377 string=string.replace(regexp,type + " id=&#39;<a href='" + mapprops.browseurl + "/" + type + "/" + id + "' target='" + type + id + "'>" + id + "</a>&#39;");
378 }
379 }
380 }
381
382 drawPopup(string.split("&gt;&lt;").join("&gt;<br>&lt;").split("<br>&lt;tag").join("<br>&nbsp;&nbsp;&lt;tag"));
383 }
384
385
386 ////////////////////////////////////////////////////////////////////////////////
387 /////////////////////////////// Server handling ////////////////////////////////
388 ////////////////////////////////////////////////////////////////////////////////
389
390 //
391 // Define an AJAX request object
392 //
393
394 function ajaxGET(url,success,failure,state)
395 {
396 var ajaxRequest=new XMLHttpRequest();
397
398 function ajaxGOT(options) {
399 if(this.readyState==4)
400 if(this.status==200)
401 { if(typeof(options.success)=="function") options.success(this,options.state); }
402 else
403 { if(typeof(options.failure)=="function") options.failure(this,options.state); }
404 }
405
406 ajaxRequest.onreadystatechange = function(){ ajaxGOT.call(ajaxRequest,{success: success, failure: failure, state: state}); };
407 ajaxRequest.open("GET", url, true);
408 ajaxRequest.send(null);
409 }
410
411
412 //
413 // Display the status
414 //
415
416 function displayStatus(type,subtype,content)
417 {
418 var child=document.getElementById("result_status").firstChild;
419
420 do
421 {
422 if(child.id !== undefined)
423 child.style.display="none";
424
425 child=child.nextSibling;
426 }
427 while(child !== null);
428
429 var chosen_status=document.getElementById("result_status_" + type);
430
431 chosen_status.style.display="";
432
433 if(subtype !== undefined)
434 {
435 var format_status=document.getElementById("result_status_" + subtype).innerHTML;
436
437 chosen_status.innerHTML=format_status.replace("#",String(content));
438 }
439 }
440
441
442 //
443 // Display data statistics
444 //
445
446 function displayStatistics()
447 {
448 // Use AJAX to get the statistics
449
450 ajaxGET("fixme.cgi?statistics=yes", runStatisticsSuccess);
451 }
452
453
454 //
455 // Success in running data statistics generation.
456 //
457
458 function runStatisticsSuccess(response)
459 {
460 document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>";
461 document.getElementById("statistics_link").style.display="none";
462 }
463
464
465 //
466 // Get the requested data
467 //
468
469 function displayData(datatype) // called from fixme.html
470 {
471 // Delete the old data
472
473 vectorData=[];
474
475 unselectFeature();
476
477 layerVectors.clearLayers();
478
479 // Print the status
480
481 displayStatus("no_data");
482
483 // Return if just here to clear the data
484
485 if(datatype === "")
486 return;
487
488 displayMoreData();
489 }
490
491
492 function displayMoreData()
493 {
494 // Get the new data
495
496 var mapbounds=map.getBounds();
497
498 var url="fixme.cgi";
499
500 url=url + "?lonmin=" + format5f(mapbounds.getWest());
501 url=url + ";latmin=" + format5f(mapbounds.getSouth());
502 url=url + ";lonmax=" + format5f(mapbounds.getEast());
503 url=url + ";latmax=" + format5f(mapbounds.getNorth());
504 url=url + ";data=fixmes";
505
506 // Use AJAX to get the data
507
508 ajaxGET(url, runFixmeSuccess, runFailure);
509
510 updateURLs(true);
511 }
512
513
514 //
515 // Success in getting the error log data
516 //
517
518 function runFixmeSuccess(response)
519 {
520 var lines=response.responseText.split("\n");
521
522 for(var line=0;line<lines.length;line++)
523 {
524 var words=lines[line].split(" ");
525
526 if(line === 0)
527 continue;
528 else if(words[0] !== "")
529 {
530 var dump=words[0];
531
532 if(vectorData[dump])
533 continue;
534 else
535 vectorData[dump]=1;
536
537 var lat=Number(words[1]);
538 var lon=Number(words[2]);
539
540 var lonlat = L.latLng(lat,lon);
541
542 var feature = L.circleMarker(lonlat,{radius: 3, fill: true, fillColor: "#FF0000", fillOpacity: 1.0,
543 stroke: false});
544
545 feature.on("click", (function(f,d) { return function(evt) { selectCircleMarkerFeature(f,d,evt); }; }(feature,dump)));
546
547 layerVectors.addLayer(feature);
548 }
549 }
550
551 displayStatus("data","fixme",Object.keys(vectorData).length);
552 }
553
554
555 //
556 // Failure in getting data.
557 //
558
559 function runFailure(response)
560 {
561 displayStatus("failed");
562 }