script.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. widths = [12, 13, 13, 13, 13, 13, 13, 13];
  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.su ? '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.su ? '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. var style = {color: "#85f385", smoothFactor : 0.4, fillOpacity: 0.2, id : group.id};
  32. if (group.new) {
  33. style.color = "blue";
  34. }
  35. if (z < 16) {
  36. style.weight = 11;
  37. style.opacity = 0.5;
  38. style.fillOpacity = 0.5;
  39. }
  40. return L.polygon(group.poly, style)
  41. }
  42. function sugArr(sug, z) {
  43. return L.polyline(sug.arrow, {id : sug.id, color: 'blue', smoothFactor : 0.1, weight: 4, opacity:0.5});
  44. }
  45. function renderStat(stat) {
  46. var attrrows = {};
  47. var ll = {lat: stat.lat, lon: stat.lon};
  48. var content = document.createElement('div');
  49. content.setAttribute("id", "nav")
  50. var attrTbl = document.createElement('table');
  51. attrTbl.setAttribute("id", "attr-tbl")
  52. var suggD = document.createElement('table');
  53. suggD.setAttribute("id", "sugg")
  54. if (stat.attrs.name) {
  55. 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>)";
  56. } else {
  57. 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>";
  58. }
  59. content.appendChild(attrTbl);
  60. content.appendChild(suggD);
  61. var tbody = document.createElement('tbody');
  62. attrTbl.appendChild(tbody);
  63. for (var key in stat.attrs) {
  64. var row = document.createElement('tr');
  65. var col1 = document.createElement('td');
  66. var col2 = document.createElement('td');
  67. tbody.appendChild(row);
  68. row.appendChild(col1);
  69. row.appendChild(col2);
  70. col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\"><tt>" + key + "</tt></a>";
  71. for (var i = 0; i < stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>"+ "<br>";
  72. attrrows[key] = row;
  73. }
  74. for (var i = 0; i < stat.attrerrs.length; i++) {
  75. var err = stat.attrerrs[i];
  76. var row = attrrows[err.attr[0]];
  77. row.className = "err-" + Math.round(err.conf * 10);
  78. var info = document.createElement('div');
  79. if (err.other_osmid != stat.osmid) {
  80. 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>";
  81. } else {
  82. info.innerHTML = "Does not match '" + err.other_attr[0] + "' = '" + err.other_attr[1];
  83. }
  84. info.className = 'attr-err-info';
  85. row.childNodes[1].appendChild(info);
  86. }
  87. if (stat.su.length) {
  88. suggD.innerHTML = "<span class='sugtit'>Suggestions</span>";
  89. }
  90. for (var i = 0; i < stat.su.length; i++) {
  91. var sugg = stat.su[i];
  92. var suggDiv = document.createElement('div');
  93. if (sugg.type == 1) {
  94. suggDiv.innerHTML = "&bull; Move node into a <span class='grouplink' onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")'>new relation</span> <tt>public_transport=stop_area</tt>";
  95. } else if (sugg.type == 2) {
  96. 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>.";
  97. } else if (sugg.type == 3) {
  98. suggDiv.innerHTML = "&bull; Move node from existing relation <a onmouseover='groupHl( " + sugg.orig_gid + ")' onmouseout='groupUnHl( " + sugg.orig_gid + ")' href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a> into a <span class='grouplink' onmouseover='groupHl( " + sugg.target_gid + ")' onmouseout='groupUnHl( " + sugg.target_gid + ")'>new relation</span> <tt>public_transport=stop_area</tt>";
  99. } else if (sugg.type == 4) {
  100. suggDiv.innerHTML = "&bull; Move node from existing relation <a onmouseover='groupHl( " + sugg.orig_gid + ")' onmouseout='groupUnHl( " + sugg.orig_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>.";
  101. } else if (sugg.type == 5) {
  102. suggDiv.innerHTML = "&bull; Move node out of existing relation <a onmouseover='groupHl( " + sugg.orig_gid + ")' onmouseout='groupUnHl( " + sugg.orig_gid + ")' href=\"https://www.openstreetmap.org/relation/" + sugg.orig_osm_rel_id + "\" target=\"_blank\">" + sugg.orig_osm_rel_id + "</a>!";
  103. }
  104. suggD.appendChild(suggDiv);
  105. }
  106. if (map.getZoom() < 18) {
  107. map.setView(ll, 18, {animate: true});
  108. }
  109. L.popup({opacity: 0.8})
  110. .setLatLng(ll)
  111. .setContent(content)
  112. .openOn(map);
  113. }
  114. function openStat(id) {
  115. var xmlhttp = new XMLHttpRequest();
  116. xmlhttp.onreadystatechange = function() {
  117. if (this.readyState == 4 && this.status == 200) {
  118. var content = JSON.parse(this.responseText);
  119. renderStat(content);
  120. }
  121. };
  122. xmlhttp.open("GET", "http://localhost:9090/stat?id=" + id, true);
  123. xmlhttp.send();
  124. }
  125. function renderGroup(grp, ll) {
  126. var attrrows = {};
  127. var content = document.createElement('div');
  128. content.setAttribute("id", "nav");
  129. var newMembers = document.createElement('div');
  130. newMembers.setAttribute("id", "group-stations-new")
  131. newMembers.innerHTML = "<span class='newmemberstit'>New Members</span>";
  132. var oldMembers = document.createElement('div');
  133. oldMembers.setAttribute("id", "group-stations-old")
  134. oldMembers.innerHTML = "<span class='oldmemberstit'>Existing Members</span>";
  135. if (grp.osmid == 1) {
  136. content.innerHTML = "<span class='grouplink' onmouseover='groupHl( " + grp.id + ")' onmouseout='groupUnHl( " + grp.id + ")'>New Relation <tt>public_transport=stop_area</tt></span>";
  137. } else {
  138. content.innerHTML = "OSM relation <a onmouseover='groupHl( " + grp.id + ")' onmouseout='groupUnHl( " + grp.id + ")' target='_blank' href='https://www.openstreetmap.org/relation/" + grp.osmid + "'>" + grp.osmid + "</a>";
  139. }
  140. content.appendChild(newMembers);
  141. if (grp.osmid != 1) content.appendChild(oldMembers);
  142. for (var key in grp.stations) {
  143. var stat = grp.stations[key];
  144. var row = document.createElement('div');
  145. if (stat.attrs.name) {
  146. row.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>)";
  147. } else {
  148. row.innerHTML = "Node <a onmouseover='nodeHl( " + stat.id + ")' onmouseout='nodeUnHl( " + stat.id + ")' target='_blank' href='https://www.openstreetmap.org/node/" + stat.osmid + "'>" + stat.osmid + "</a>";
  149. }
  150. if (grp.osmid == 1 || stat.orig_group != grp.id) newMembers.appendChild(row);
  151. else oldMembers.appendChild(row);
  152. }
  153. if (map.getZoom() < 18) {
  154. map.setView(ll, 18, {animate: true});
  155. }
  156. L.popup({opacity: 0.8})
  157. .setLatLng(ll)
  158. .setContent(content)
  159. .openOn(map);
  160. }
  161. function openGroup(id, ll) {
  162. var xmlhttp = new XMLHttpRequest();
  163. xmlhttp.onreadystatechange = function() {
  164. if (this.readyState == 4 && this.status == 200) {
  165. var content = JSON.parse(this.responseText);
  166. renderGroup(content, ll);
  167. }
  168. };
  169. xmlhttp.open("GET", "http://localhost:9090/group?id=" + id, true);
  170. xmlhttp.send();
  171. }
  172. function groupHl(id) {
  173. if (!document.groupIdx[id]) return;
  174. document.groupIdx[id].setStyle({
  175. 'weight': 6,
  176. 'fillOpacity':0.5
  177. });
  178. }
  179. function groupUnHl(id) {
  180. if (!document.groupIdx[id]) return;
  181. document.groupIdx[id].setStyle({
  182. 'weight': 3,
  183. 'fillOpacity':0.2
  184. });
  185. }
  186. function nodeHl(id) {
  187. if (!document.nodeIdx[id]) return;
  188. document.nodeIdx[id].setStyle({
  189. 'weight': 5,
  190. 'color' : "#eecc00"
  191. });
  192. }
  193. function nodeUnHl(id) {
  194. if (!document.nodeIdx[id]) return;
  195. document.nodeIdx[id].setStyle({
  196. 'weight': 1,
  197. 'color' : "black"
  198. });
  199. }
  200. var map = L.map('map', {
  201. renderer: L.canvas()
  202. }).setView([47.9965, 7.8469], 13);
  203. L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
  204. maxZoom: 23,
  205. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>',
  206. id: 'mapbox.streets'
  207. }).addTo(map);
  208. var layer = L.featureGroup().addTo(map);
  209. var labelLayer = L.featureGroup().addTo(map);
  210. map.on("moveend", function () {
  211. render();
  212. });
  213. function render() {
  214. var xmlhttp = new XMLHttpRequest();
  215. if (map.getZoom() < 11) {
  216. xmlhttp.onreadystatechange = function() {
  217. if (this.readyState == 4 && this.status == 200) {
  218. var content = JSON.parse(this.responseText);
  219. layer.clearLayers();
  220. labelLayer.clearLayers();
  221. var blur = 22 - map.getZoom();
  222. var rad = 25 - map.getZoom();
  223. layer.addLayer(L.heatLayer(content.ok, {max: 500, gradient: {0: '#cbf7cb', 0.5: '#78f378', 1: '#29c329'}, minOpacity: 0.65, blur: blur, radius: rad}));
  224. layer.addLayer(L.heatLayer(content.sugg, {max: 500, gradient: {0: '#7f7fbd', 0.5: '#4444b3', 1: '#0606c1'}, minOpacity: 0.65, blur: blur-3, radius: Math.min(12, rad-3)}));
  225. layer.addLayer(L.heatLayer(content.err, {max: 500, gradient: {0: '#f39191', 0.5: '#ff5656', 1: '#ff0000'}, minOpacity: 0.75, blur: blur-3, radius: Math.min(10, rad-3), maxZoom: 15}));
  226. }
  227. };
  228. xmlhttp.open("GET", "http://localhost:9090/heatmap?z=" + map.getZoom() + "&bbox=" + [map.getBounds().getSouthWest().lat, map.getBounds().getSouthWest().lng, map.getBounds().getNorthEast().lat, map.getBounds().getNorthEast().lng].join(","), true);
  229. xmlhttp.send();
  230. } else {
  231. xmlhttp.onreadystatechange = function() {
  232. if (this.readyState == 4 && this.status == 200) {
  233. var content = JSON.parse(this.responseText);
  234. layer.clearLayers();
  235. labelLayer.clearLayers();
  236. document.groupIdx = {};
  237. document.nodeIdx = {};
  238. var stations = [];
  239. var labels = [];
  240. for (var i = 0; i < content.stats.length; i++) {
  241. stat = content.stats[i];
  242. var ndMarker = marker(stat, map.getZoom());
  243. stations.push(ndMarker);
  244. document.nodeIdx[stat.id] = ndMarker;
  245. }
  246. var groups = [];
  247. for (var i = 0; i < content.groups.length; i++) {
  248. group = content.groups[i];
  249. var groupPoly = poly(group, map.getZoom());
  250. groups.push(groupPoly);
  251. document.groupIdx[group.id] = groupPoly;
  252. }
  253. var suggs = [];
  254. for (var i = 0; i < content.su.length; i++) {
  255. sugg = content.su[i];
  256. var a = sugArr(sugg, map.getZoom());
  257. suggs.push(a);
  258. }
  259. if (map.getZoom() > 15) {
  260. labelLayer.addLayer(L.featureGroup(labels));
  261. layer.addLayer(L.featureGroup(suggs).on('click', function(a) { openStat(a.layer.options.id); }));
  262. }
  263. if (map.getZoom() > 13) {
  264. layer.addLayer(L.featureGroup(groups).on('click', function(a) { openGroup(a.layer.options.id, a.layer.getBounds().getCenter());}));
  265. }
  266. layer.addLayer(L.featureGroup(stations).on('click', function(a) {
  267. openStat(a.layer.options.id);}));
  268. }
  269. };
  270. 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);
  271. xmlhttp.send();
  272. }
  273. }
  274. render();