script.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, id : group.id})
  32. return L.polygon(group.poly, {color: '#85f385', smoothFactor : 1, fillOpacity:0.2, id : group.id})
  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 renderGroup(grp, ll) {
  121. var attrrows = {};
  122. var content = document.createElement('div');
  123. content.setAttribute("id", "nav")
  124. var attrTbl = document.createElement('table');
  125. attrTbl.setAttribute("id", "attr-tbl")
  126. var suggD = document.createElement('table');
  127. suggD.setAttribute("id", "sugg")
  128. content.innerHTML = "Relation <a onmouseover='groupHl( " + grp.id + ")' onmouseout='groupUnHl( " + grp.id + ")' target='_blank' href='https://www.openstreetmap.org/relation/" + grp.osmid + "'>" + grp.osmid + "</a>";
  129. content.appendChild(attrTbl);
  130. content.appendChild(suggD);
  131. var tbody = document.createElement('tbody');
  132. attrTbl.appendChild(tbody);
  133. for (var key in grp.stations) {
  134. //var row = document.createElement('tr');
  135. //var col1 = document.createElement('td');
  136. //var col2 = document.createElement('td');
  137. //tbody.appendChild(row);
  138. //row.appendChild(col1);
  139. //row.appendChild(col2);
  140. //col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\">" + key + "</a>";
  141. //for (var i = 0; i < stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>"+ "<br>";
  142. //attrrows[key] = row;
  143. }
  144. if (map.getZoom() < 18) {
  145. map.setView(ll, 18, {animate: true});
  146. }
  147. L.popup({opacity: 0.8})
  148. .setLatLng(ll)
  149. .setContent(content)
  150. .openOn(map);
  151. }
  152. function openGroup(id, ll) {
  153. var xmlhttp = new XMLHttpRequest();
  154. xmlhttp.onreadystatechange = function() {
  155. if (this.readyState == 4 && this.status == 200) {
  156. var content = JSON.parse(this.responseText);
  157. renderGroup(content, ll);
  158. }
  159. };
  160. xmlhttp.open("GET", "http://localhost:9090/group?id=" + id, true);
  161. xmlhttp.send();
  162. }
  163. function groupHl(id) {
  164. if (!document.groupIdx[id]) return;
  165. document.groupIdx[id].setStyle({
  166. 'weight': 6,
  167. 'fillOpacity':0.5
  168. });
  169. }
  170. function groupUnHl(id) {
  171. if (!document.groupIdx[id]) return;
  172. document.groupIdx[id].setStyle({
  173. 'weight': 3,
  174. 'fillOpacity':0.2
  175. });
  176. }
  177. function nodeHl(id) {
  178. if (!document.nodeIdx[id]) return;
  179. document.nodeIdx[id].setStyle({
  180. 'weight': 5,
  181. 'color' : "#eecc00"
  182. });
  183. }
  184. function nodeUnHl(id) {
  185. if (!document.nodeIdx[id]) return;
  186. document.nodeIdx[id].setStyle({
  187. 'weight': 1,
  188. 'color' : "black"
  189. });
  190. }
  191. var map = L.map('map', {
  192. renderer: L.canvas()
  193. }).setView([47.9965, 7.8469], 13);
  194. L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
  195. maxZoom: 23,
  196. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>',
  197. id: 'mapbox.streets'
  198. }).addTo(map);
  199. var layer = L.featureGroup().addTo(map);
  200. var labelLayer = L.featureGroup().addTo(map);
  201. map.on("moveend", function () {
  202. render();
  203. });
  204. function render() {
  205. var xmlhttp = new XMLHttpRequest();
  206. if (map.getZoom() < 11) {
  207. xmlhttp.onreadystatechange = function() {
  208. if (this.readyState == 4 && this.status == 200) {
  209. var content = JSON.parse(this.responseText);
  210. layer.clearLayers();
  211. labelLayer.clearLayers();
  212. var blur = 21 - map.getZoom();
  213. var rad = 21 - map.getZoom();
  214. blur = Math.max(5, Math.min(9, blur));
  215. rad = Math.min(10, rad);
  216. layer.addLayer(L.heatLayer(content.ok, {gradient: {0: '#cbf7cb', 0.5: '#78f378', 1: '#29c329'}, minOpacity: 1, blur: blur, radius: rad, maxZoom: 15}));
  217. layer.addLayer(L.heatLayer(content.sugg, {gradient: {0: '#7f7fbd', 0.5: '#4444b3', 1: '#0606c1'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  218. layer.addLayer(L.heatLayer(content.err, {gradient: {0: '#f39191', 0.5: '#ff5656', 1: '#ff0000'}, minOpacity: 1, blur: blur-3, radius: rad-3, maxZoom: 15}));
  219. }
  220. };
  221. 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);
  222. xmlhttp.send();
  223. } else {
  224. xmlhttp.onreadystatechange = function() {
  225. if (this.readyState == 4 && this.status == 200) {
  226. var content = JSON.parse(this.responseText);
  227. layer.clearLayers();
  228. labelLayer.clearLayers();
  229. document.groupIdx = {};
  230. document.nodeIdx = {};
  231. var stations = [];
  232. var labels = [];
  233. for (var i = 0; i < content.stats.length; i++) {
  234. stat = content.stats[i];
  235. var ndMarker = marker(stat, map.getZoom());
  236. stations.push(ndMarker);
  237. document.nodeIdx[stat.id] = ndMarker;
  238. }
  239. var groups = [];
  240. for (var i = 0; i < content.groups.length; i++) {
  241. group = content.groups[i];
  242. var groupPoly = poly(group, map.getZoom());
  243. groups.push(groupPoly);
  244. document.groupIdx[group.id] = groupPoly;
  245. }
  246. var suggs = [];
  247. for (var i = 0; i < content.suggestions.length; i++) {
  248. sugg = content.suggestions[i];
  249. var a = sugArr(sugg, map.getZoom());
  250. suggs.push(a[0]);
  251. suggs.push(a[1]);
  252. }
  253. if (map.getZoom() > 15) {
  254. labelLayer.addLayer(L.featureGroup(labels));
  255. layer.addLayer(L.featureGroup(groups).on('click', function(a) { openGroup(a.layer.options.id, a.layer.getBounds().getCenter());}));
  256. layer.addLayer(L.featureGroup(suggs).on('click', function(a) { openStat(a.layer.options.id, a.layer.getBounds().getCenter());}));
  257. }
  258. layer.addLayer(L.featureGroup(stations).on('click', function(a) {
  259. openStat(a.layer.options.id, a.layer.getBounds().getCenter());}));
  260. }
  261. };
  262. 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);
  263. xmlhttp.send();
  264. }
  265. }
  266. render();