script.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. widths = [12, 13, 14, 15, 16, 16, 16, 16];
  2. opas = [0.8, 0.6, 0.5, 0.5, 0.4];
  3. mwidths = [1, 1, 1, 1.5, 2, 3, 5, 6, 6, 4, 3, 2];
  4. function $(id) { return document.getElementById(id); }
  5. function marker(stat, z) {
  6. if (z > 15) {
  7. return L.circle(
  8. [stat.lat, stat.lon],
  9. {
  10. color: 'black',
  11. fillColor: stat.suggestion ? 'blue' : stat.attrerrs ? 'red' : '#78f378',
  12. radius: mwidths[23 - z],
  13. fillOpacity : 1,
  14. weight : 1,
  15. id : stat.id
  16. }
  17. );
  18. } else {
  19. return L.polyline(
  20. [[stat.lat, stat.lon], [stat.lat, stat.lon]],
  21. {
  22. color: stat.suggestion ? 'blue' : stat.attrerrs ? 'red' : '#78f378',
  23. weight: widths[15 - z],
  24. opacity: opas[15 - z],
  25. id : stat.id
  26. }
  27. );
  28. }
  29. }
  30. function poly(group, z) {
  31. if (group.new) return L.polygon(group.poly, {color: "blue", smoothFactor : 0.4})
  32. return L.polygon(group.poly, {color: '#85f385', smoothFactor : 1})
  33. }
  34. function sugArr(sug, z) {
  35. return [
  36. L.polyline(sug.arrow, {id : sug.id, color: 'black', smoothFactor : 1, weight: 2, opacity:0.5}),
  37. L.polyline(sug.arrow, {id : sug.id, color: 'blue', smoothFactor : 1, weight: 1, opacity:0.5})
  38. ]
  39. ;
  40. }
  41. function renderStat(stat, ll) {
  42. var attrrows = {};
  43. var content = document.createElement('div');
  44. content.setAttribute("id", "nav")
  45. var attrTbl = document.createElement('table');
  46. attrTbl.setAttribute("id", "attr-tbl")
  47. var suggD = document.createElement('table');
  48. suggD.setAttribute("id", "sugg")
  49. content.innerHTML = "OSM node <a target='_blank' href='https://www.openstreetmap.org/node/" + stat.osmid + "'>" + stat.osmid + "</a>";
  50. content.appendChild(attrTbl);
  51. content.appendChild(suggD);
  52. var tbody = document.createElement('tbody');
  53. attrTbl.appendChild(tbody);
  54. for (var key in stat.attrs) {
  55. var row = document.createElement('tr');
  56. var col1 = document.createElement('td');
  57. var col2 = document.createElement('td');
  58. tbody.appendChild(row);
  59. row.appendChild(col1);
  60. row.appendChild(col2);
  61. col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\">" + key + "</a>";
  62. for (var i = 0; i < stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>"+ "<br>";
  63. attrrows[key] = row;
  64. }
  65. for (var i = 0; i < stat.attrerrs.length; i++) {
  66. var err = stat.attrerrs[i];
  67. var row = attrrows[err.attr[0]];
  68. row.className = "err-" + Math.round(err.conf * 10);
  69. var info = document.createElement('div');
  70. if (err.other_osmid != stat.osmid) {
  71. info.innerHTML = "Does not match node <a target=\"_blank\" href=\"https://www.openstreetmap.org/node/" + err.other_osmid + "\">" + err.other_osmid + "</a>";
  72. } else {
  73. info.innerHTML = "Does not match '" + err.other_attr[0] + "' = '" + err.other_attr[1];
  74. }
  75. info.className = 'attr-err-info';
  76. row.childNodes[1].appendChild(info);
  77. }
  78. for (var i = 0; i < stat.suggestions.length; i++) {
  79. var sugg = stat.suggestions[i];
  80. var suggDiv = document.createElement('div');
  81. if (sugg.type == 1) {
  82. suggDiv.innerHTML = "Move node into a new relation (internal id=" + sugg.target_gid + ")";
  83. } else if (sugg.type == 2) {
  84. suggDiv.innerHTML = "Move node into existing relation <a href=\"https://www.openstreetmap.org/relation/" + sugg.target_osm_rel_id + "\" target=\"_blank\">" + sugg.target_osm_rel_id + "</a>.";
  85. } else if (sugg.type == 3) {
  86. suggDiv.innerHTML = "Move node from existing relation <a href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a> into new relation (internal id=" + sugg.target_gid + ")";
  87. } else if (sugg.type == 4) {
  88. suggDiv.innerHTML = "Move node from existing relation <a href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a> into existing relation <a href=\"https://www.openstreetmap.org/relation/" + sugg.target_osm_rel_id + "\" target=\"_blank\">" + sugg.target_osm_rel_id + "</a>.";
  89. } else if (sugg.type == 5) {
  90. suggDiv.innerHTML = "Move node out of existing relation <a href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a>!";
  91. }
  92. suggD.appendChild(suggDiv);
  93. }
  94. if (map.getZoom() < 18) map.setView(ll, 18);
  95. L.popup({opacity: 0.8})
  96. .setLatLng(ll)
  97. .setContent(content.innerHTML)
  98. .openOn(map);
  99. }
  100. function openStat(id, ll) {
  101. var xmlhttp = new XMLHttpRequest();
  102. xmlhttp.onreadystatechange = function() {
  103. if (this.readyState == 4 && this.status == 200) {
  104. var content = JSON.parse(this.responseText);
  105. renderStat(content, ll);
  106. }
  107. };
  108. xmlhttp.open("GET", "http://localhost:9090/stat?id=" + id, true);
  109. xmlhttp.send();
  110. }
  111. var map = L.map('map', {
  112. renderer: L.canvas()
  113. }).setView([47.9965, 7.8469], 13);
  114. L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
  115. maxZoom: 23,
  116. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>',
  117. id: 'mapbox.streets'
  118. }).addTo(map);
  119. var layer = L.featureGroup().addTo(map);
  120. var labelLayer = L.featureGroup().addTo(map);
  121. map.on("moveend", function () {
  122. render();
  123. });
  124. function render() {
  125. var xmlhttp = new XMLHttpRequest();
  126. if (map.getZoom() < 11) {
  127. xmlhttp.onreadystatechange = function() {
  128. if (this.readyState == 4 && this.status == 200) {
  129. var content = JSON.parse(this.responseText);
  130. layer.clearLayers();
  131. labelLayer.clearLayers();
  132. var blur = 21 - map.getZoom();
  133. var rad = 21 - map.getZoom();
  134. blur = Math.max(5, Math.min(9, blur));
  135. rad = Math.min(10, rad);
  136. layer.addLayer(L.heatLayer(content.ok, {gradient: {0: '#cbf7cb', 0.5: '#78f378', 1: '#29c329'}, minOpacity: 1, blur: blur, radius: rad, maxZoom: 15}));
  137. layer.addLayer(L.heatLayer(content.sugg, {gradient: {0: '#7f7fbd', 0.5: '#4444b3', 1: '#0606c1'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  138. layer.addLayer(L.heatLayer(content.err, {gradient: {0: '#f39191', 0.5: '#ff5656', 1: '#ff0000'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  139. }
  140. };
  141. xmlhttp.open("GET", "http://localhost:9090/heatmap?bbox=" + [map.getBounds().getSouthWest().lat, map.getBounds().getSouthWest().lng, map.getBounds().getNorthEast().lat, map.getBounds().getNorthEast().lng].join(","), true);
  142. xmlhttp.send();
  143. } else {
  144. xmlhttp.onreadystatechange = function() {
  145. if (this.readyState == 4 && this.status == 200) {
  146. var content = JSON.parse(this.responseText);
  147. layer.clearLayers();
  148. labelLayer.clearLayers();
  149. var stations = [];
  150. var labels = [];
  151. for (var i = 0; i < content.stats.length; i++) {
  152. stat = content.stats[i];
  153. stations.push(marker(stat, map.getZoom()));
  154. }
  155. var groups = [];
  156. for (var i = 0; i < content.groups.length; i++) {
  157. group = content.groups[i];
  158. groups.push(poly(group, map.getZoom()));
  159. }
  160. var suggs = [];
  161. for (var i = 0; i < content.suggestions.length; i++) {
  162. sugg = content.suggestions[i];
  163. var a = sugArr(sugg, map.getZoom());
  164. suggs.push(a[0]);
  165. suggs.push(a[1]);
  166. }
  167. if (map.getZoom() > 15) {
  168. labelLayer.addLayer(L.featureGroup(labels));
  169. layer.addLayer(L.featureGroup(groups));
  170. layer.addLayer(L.featureGroup(suggs).on('click', function(a) { openStat(a.layer.options.id, a.latlng);}));
  171. }
  172. layer.addLayer(L.featureGroup(stations).on('click', function(a) {
  173. openStat(a.layer.options.id, a.latlng);}));
  174. }
  175. };
  176. xmlhttp.open("GET", "http://localhost:9090/map?bbox=" + [map.getBounds().getSouthWest().lat, map.getBounds().getSouthWest().lng, map.getBounds().getNorthEast().lat, map.getBounds().getNorthEast().lng].join(","), true);
  177. xmlhttp.send();
  178. }
  179. }
  180. render();