script.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.attrerrs ? 'red' : stat.suggestion ? 'blue' : '#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.attrerrs ? 'red' : stat.suggestion ? 'blue' : '#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, fillOpacity:0.2})
  32. return L.polygon(group.poly, {color: '#85f385', smoothFactor : 1, fillOpacity:0.2})
  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. if (stat.attrs.name) {
  50. content.innerHTML = "Node <a onmouseover='nodeHl( " + stat.id + ")' onmouseout='nodeUnHl( " + stat.id + ")' target='_blank' href='https://www.openstreetmap.org/node/" + stat.osmid + "'>" + stat.osmid + "</a> (<b>\"" + stat.attrs.name + "\"</b>)";
  51. } else {
  52. content.innerHTML = "Node <a onmouseover='nodeHl( " + stat.id + ")' onmouseout='nodeUnHl( " + stat.id + ")' target='_blank' href='https://www.openstreetmap.org/node/" + stat.osmid + "'>" + stat.osmid + "</a>";
  53. }
  54. content.appendChild(attrTbl);
  55. content.appendChild(suggD);
  56. var tbody = document.createElement('tbody');
  57. attrTbl.appendChild(tbody);
  58. for (var key in stat.attrs) {
  59. var row = document.createElement('tr');
  60. var col1 = document.createElement('td');
  61. var col2 = document.createElement('td');
  62. tbody.appendChild(row);
  63. row.appendChild(col1);
  64. row.appendChild(col2);
  65. col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\">" + key + "</a>";
  66. for (var i = 0; i < stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>"+ "<br>";
  67. attrrows[key] = row;
  68. }
  69. for (var i = 0; i < stat.attrerrs.length; i++) {
  70. var err = stat.attrerrs[i];
  71. var row = attrrows[err.attr[0]];
  72. row.className = "err-" + Math.round(err.conf * 10);
  73. var info = document.createElement('div');
  74. if (err.other_osmid != stat.osmid) {
  75. info.innerHTML = "Does not match node <a onmouseover='nodeHl( " + err.other + ")' onmouseout='nodeUnHl( " + err.other + ")' target=\"_blank\" href=\"https://www.openstreetmap.org/node/" + err.other_osmid + "\">" + err.other_osmid + "</a>";
  76. } else {
  77. info.innerHTML = "Does not match '" + err.other_attr[0] + "' = '" + err.other_attr[1];
  78. }
  79. info.className = 'attr-err-info';
  80. row.childNodes[1].appendChild(info);
  81. }
  82. if (stat.suggestions.length) {
  83. suggD.innerHTML = "<span class='sugtit'>Suggestions</span>";
  84. }
  85. for (var i = 0; i < stat.suggestions.length; i++) {
  86. var sugg = stat.suggestions[i];
  87. var suggDiv = document.createElement('div');
  88. if (sugg.type == 1) {
  89. suggDiv.innerHTML = "&bull; Move node into a <span class='grouplink' onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")'>new relation</span>";
  90. } else if (sugg.type == 2) {
  91. suggDiv.innerHTML = "&bull; Move node into existing relation <a onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")' href=\"https://www.openstreetmap.org/relation/" + sugg.target_osm_rel_id + "\" target=\"_blank\">" + sugg.target_osm_rel_id + "</a>.";
  92. } else if (sugg.type == 3) {
  93. suggDiv.innerHTML = "&bull; Move node from existing relation <a onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")' 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 + ")";
  94. } else if (sugg.type == 4) {
  95. suggDiv.innerHTML = "&bull; Move node from existing relation <a onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")' 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>.";
  96. } else if (sugg.type == 5) {
  97. suggDiv.innerHTML = "&bull; Move node out of existing relation <a onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")' href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a>!";
  98. }
  99. suggD.appendChild(suggDiv);
  100. }
  101. if (map.getZoom() < 18) {
  102. map.setView(ll, 18, {animate: true});
  103. }
  104. L.popup({opacity: 0.8})
  105. .setLatLng(ll)
  106. .setContent(content)
  107. .openOn(map);
  108. }
  109. function openStat(id, ll) {
  110. var xmlhttp = new XMLHttpRequest();
  111. xmlhttp.onreadystatechange = function() {
  112. if (this.readyState == 4 && this.status == 200) {
  113. var content = JSON.parse(this.responseText);
  114. renderStat(content, ll);
  115. }
  116. };
  117. xmlhttp.open("GET", "http://localhost:9090/stat?id=" + id, true);
  118. xmlhttp.send();
  119. }
  120. function groupHl(id) {
  121. if (!document.groupIdx[id]) return;
  122. document.groupIdx[id].setStyle({
  123. 'weight': 6,
  124. 'fillOpacity':0.5
  125. });
  126. }
  127. function groupUnHl(id) {
  128. if (!document.groupIdx[id]) return;
  129. document.groupIdx[id].setStyle({
  130. 'weight': 3,
  131. 'fillOpacity':0.2
  132. });
  133. }
  134. function nodeHl(id) {
  135. if (!document.nodeIdx[id]) return;
  136. document.nodeIdx[id].setStyle({
  137. 'weight': 5,
  138. 'color' : "#eecc00"
  139. });
  140. }
  141. function nodeUnHl(id) {
  142. if (!document.nodeIdx[id]) return;
  143. document.nodeIdx[id].setStyle({
  144. 'weight': 1,
  145. 'color' : "black"
  146. });
  147. }
  148. var map = L.map('map', {
  149. renderer: L.canvas()
  150. }).setView([47.9965, 7.8469], 13);
  151. L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
  152. maxZoom: 23,
  153. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>',
  154. id: 'mapbox.streets'
  155. }).addTo(map);
  156. var layer = L.featureGroup().addTo(map);
  157. var labelLayer = L.featureGroup().addTo(map);
  158. map.on("moveend", function () {
  159. render();
  160. });
  161. function render() {
  162. var xmlhttp = new XMLHttpRequest();
  163. if (map.getZoom() < 11) {
  164. xmlhttp.onreadystatechange = function() {
  165. if (this.readyState == 4 && this.status == 200) {
  166. var content = JSON.parse(this.responseText);
  167. layer.clearLayers();
  168. labelLayer.clearLayers();
  169. var blur = 21 - map.getZoom();
  170. var rad = 21 - map.getZoom();
  171. blur = Math.max(5, Math.min(9, blur));
  172. rad = Math.min(10, rad);
  173. layer.addLayer(L.heatLayer(content.ok, {gradient: {0: '#cbf7cb', 0.5: '#78f378', 1: '#29c329'}, minOpacity: 1, blur: blur, radius: rad, maxZoom: 15}));
  174. layer.addLayer(L.heatLayer(content.sugg, {gradient: {0: '#7f7fbd', 0.5: '#4444b3', 1: '#0606c1'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  175. layer.addLayer(L.heatLayer(content.err, {gradient: {0: '#f39191', 0.5: '#ff5656', 1: '#ff0000'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  176. }
  177. };
  178. 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);
  179. xmlhttp.send();
  180. } else {
  181. xmlhttp.onreadystatechange = function() {
  182. if (this.readyState == 4 && this.status == 200) {
  183. var content = JSON.parse(this.responseText);
  184. layer.clearLayers();
  185. labelLayer.clearLayers();
  186. document.groupIdx = {};
  187. document.nodeIdx = {};
  188. var stations = [];
  189. var labels = [];
  190. for (var i = 0; i < content.stats.length; i++) {
  191. stat = content.stats[i];
  192. var ndMarker = marker(stat, map.getZoom());
  193. stations.push(ndMarker);
  194. document.nodeIdx[stat.id] = ndMarker;
  195. }
  196. var groups = [];
  197. for (var i = 0; i < content.groups.length; i++) {
  198. group = content.groups[i];
  199. var groupPoly = poly(group, map.getZoom());
  200. groups.push(groupPoly);
  201. document.groupIdx[group.id] = groupPoly;
  202. }
  203. var suggs = [];
  204. for (var i = 0; i < content.suggestions.length; i++) {
  205. sugg = content.suggestions[i];
  206. var a = sugArr(sugg, map.getZoom());
  207. suggs.push(a[0]);
  208. suggs.push(a[1]);
  209. }
  210. if (map.getZoom() > 15) {
  211. labelLayer.addLayer(L.featureGroup(labels));
  212. layer.addLayer(L.featureGroup(groups).on('click', function(a) { console.log(a.layer.options);}));
  213. layer.addLayer(L.featureGroup(suggs).on('click', function(a) { openStat(a.layer.options.id, a.layer.getBounds().getCenter());}));
  214. }
  215. layer.addLayer(L.featureGroup(stations).on('click', function(a) {
  216. openStat(a.layer.options.id, a.layer.getBounds().getCenter());}));
  217. }
  218. };
  219. 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);
  220. xmlhttp.send();
  221. }
  222. }
  223. render();