script.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. backend = "http://staty.cs.uni-freiburg.de/backend/";
  5. function marker(stat, z) {
  6. if (z > 15) {
  7. return L.circle(
  8. [stat.lat, stat.lon], {
  9. color: 'black',
  10. fillColor: stat.attrerrs ? 'red' : stat.su ? '#0000c3' : '#78f378',
  11. radius: mwidths[23 - z],
  12. fillOpacity: 1,
  13. weight: 1,
  14. id: stat.id
  15. }
  16. );
  17. } else {
  18. return L.polyline(
  19. [
  20. [stat.lat, stat.lon],
  21. [stat.lat, stat.lon]
  22. ], {
  23. color: stat.attrerrs ? 'red' : stat.su ? '#0000c3' : '#78f378',
  24. weight: widths[15 - z],
  25. opacity: opas[15 - z],
  26. id: stat.id
  27. }
  28. );
  29. }
  30. }
  31. function poly(group, z) {
  32. var style = {
  33. color: "#85f385",
  34. smoothFactor: 0.4,
  35. fillOpacity: 0.2,
  36. id: group.id
  37. };
  38. if (group.new) {
  39. style.color = "#0000c3";
  40. }
  41. if (z < 16) {
  42. style.weight = 11;
  43. style.opacity = 0.5;
  44. style.fillOpacity = 0.5;
  45. }
  46. return L.polygon(group.poly, style)
  47. }
  48. function sugArr(sug, z) {
  49. return L.polyline(sug.arrow, {
  50. id: sug.id,
  51. color: '#0000c3',
  52. smoothFactor: 0.1,
  53. weight: 4,
  54. opacity: 0.5
  55. });
  56. }
  57. function renderStat(stat) {
  58. var attrrows = {};
  59. var ll = {
  60. lat: stat.lat,
  61. lon: stat.lon
  62. };
  63. var content = document.createElement('div');
  64. content.setAttribute("id", "nav")
  65. var attrTbl = document.createElement('table');
  66. attrTbl.setAttribute("id", "attr-tbl")
  67. var suggD = document.createElement('div');
  68. suggD.setAttribute("id", "sugg")
  69. if (stat.attrs.name) {
  70. 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>)";
  71. } else {
  72. 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>";
  73. }
  74. content.appendChild(attrTbl);
  75. content.appendChild(suggD);
  76. var tbody = document.createElement('tbody');
  77. attrTbl.appendChild(tbody);
  78. for (var key in stat.attrs) {
  79. var row = document.createElement('tr');
  80. var col1 = document.createElement('td');
  81. var col2 = document.createElement('td');
  82. col2.className = "err-wrap";
  83. tbody.appendChild(row);
  84. row.appendChild(col1);
  85. row.appendChild(col2);
  86. col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\"><tt>" + key + "</tt></a>";
  87. for (var i = 0; i < stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>" + "<br>";
  88. attrrows[key] = row;
  89. }
  90. for (var i = 0; i < stat.attrerrs.length; i++) {
  91. var err = stat.attrerrs[i];
  92. var row = attrrows[err.attr[0]];
  93. row.className = "err-" + Math.round(err.conf * 10);
  94. var info = document.createElement('div');
  95. if (err.other_osmid != stat.osmid) {
  96. info.innerHTML = "Does not match <tt>" + err.other_attr[0] + "</tt> in 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>";
  97. } else {
  98. info.innerHTML = "Does not match '" + err.other_attr[0] + "' = '" + err.other_attr[1];
  99. }
  100. info.className = 'attr-err-info';
  101. row.childNodes[1].appendChild(info);
  102. }
  103. var suggList = document.createElement('ul');
  104. if (stat.su.length) {
  105. var a = document.createElement('span');
  106. a.className = "sugtit";
  107. a.innerHTML = "Suggestions";
  108. suggD.appendChild(a);
  109. }
  110. suggD.appendChild(suggList);
  111. for (var i = 0; i < stat.su.length; i++) {
  112. var sugg = stat.su[i];
  113. var suggDiv = document.createElement('li');
  114. if (sugg.type == 1) {
  115. suggDiv.innerHTML = "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>";
  116. } else if (sugg.type == 2) {
  117. suggDiv.innerHTML = "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>.";
  118. } else if (sugg.type == 3) {
  119. suggDiv.innerHTML = "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>";
  120. } else if (sugg.type == 4) {
  121. suggDiv.innerHTML = "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 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>.";
  122. } else if (sugg.type == 5) {
  123. suggDiv.innerHTML = "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>";
  124. } else if (sugg.type == 6) {
  125. suggDiv.innerHTML = "Fix attribute <tt>" + sugg.attr + "</tt>";
  126. }
  127. suggList.appendChild(suggDiv);
  128. }
  129. if (map.getZoom() < 18) {
  130. map.setView(ll, 18, {
  131. animate: true
  132. });
  133. }
  134. L.popup({
  135. opacity: 0.8
  136. })
  137. .setLatLng(ll)
  138. .setContent(content)
  139. .openOn(map);
  140. }
  141. function openStat(id) {
  142. window.xmlhttp = new XMLHttpRequest();
  143. window.xmlhttp.onreadystatechange = function() {
  144. if (this.readyState == 4 && this.status == 200 && this == window.xmlhttp) {
  145. var content = JSON.parse(this.responseText);
  146. renderStat(content);
  147. }
  148. };
  149. window.xmlhttp.open("GET", backend + "/stat?id=" + id, true);
  150. window.xmlhttp.send();
  151. }
  152. function renderGroup(grp, ll) {
  153. var attrrows = {};
  154. var content = document.createElement('div');
  155. content.setAttribute("id", "nav");
  156. var newMembers = document.createElement('div');
  157. newMembers.setAttribute("id", "group-stations-new")
  158. newMembers.innerHTML = "<span class='newmemberstit'>New Members</span>";
  159. var oldMembers = document.createElement('div');
  160. oldMembers.setAttribute("id", "group-stations-old")
  161. oldMembers.innerHTML = "<span class='oldmemberstit'>Existing Members</span>";
  162. if (grp.osmid == 1) {
  163. content.innerHTML = "<span class='grouplink' onmouseover='groupHl( " + grp.id + ")' onmouseout='groupUnHl( " + grp.id + ")'>New relation</span> <tt>public_transport=stop_area</tt>";
  164. } else {
  165. 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>";
  166. }
  167. content.appendChild(newMembers);
  168. if (grp.osmid != 1) content.appendChild(oldMembers);
  169. for (var key in grp.stations) {
  170. var stat = grp.stations[key];
  171. var row = document.createElement('div');
  172. if (stat.attrs.name) {
  173. 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>)";
  174. } else {
  175. 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>";
  176. }
  177. if (grp.osmid == 1 || stat.orig_group != grp.id) newMembers.appendChild(row);
  178. else {
  179. oldMembers.appendChild(row);
  180. if (stat.group != grp.id) {
  181. row.className = "del-stat";
  182. }
  183. }
  184. }
  185. if (map.getZoom() < 18) {
  186. map.setView(ll, 18, {
  187. animate: true
  188. });
  189. }
  190. L.popup({
  191. opacity: 0.8
  192. })
  193. .setLatLng(ll)
  194. .setContent(content)
  195. .openOn(map);
  196. }
  197. function openGroup(id, ll) {
  198. window.xmlhttp = new XMLHttpRequest();
  199. window.xmlhttp.onreadystatechange = function() {
  200. if (this.readyState == 4 && this.status == 200 && this == window.xmlhttp) {
  201. var content = JSON.parse(this.responseText);
  202. renderGroup(content, ll);
  203. }
  204. };
  205. window.xmlhttp.open("GET", backend + "/group?id=" + id, true);
  206. window.xmlhttp.send();
  207. }
  208. function groupHl(id) {
  209. if (!document.groupIdx[id]) return;
  210. document.groupIdx[id].setStyle({
  211. 'weight': 6,
  212. 'fillOpacity': 0.5
  213. });
  214. }
  215. function groupUnHl(id) {
  216. if (!document.groupIdx[id]) return;
  217. document.groupIdx[id].setStyle({
  218. 'weight': 3,
  219. 'fillOpacity': 0.2
  220. });
  221. }
  222. function nodeHl(id) {
  223. if (!document.nodeIdx[id]) return;
  224. document.nodeIdx[id].setStyle({
  225. 'weight': 5,
  226. 'color': "#eecc00"
  227. });
  228. }
  229. function nodeUnHl(id) {
  230. if (!document.nodeIdx[id]) return;
  231. document.nodeIdx[id].setStyle({
  232. 'weight': 1,
  233. 'color': "black"
  234. });
  235. }
  236. var map = L.map('map', {
  237. renderer: L.canvas(),
  238. attributionControl: false,
  239. }).setView([47.9965, 7.8469], 13);
  240. map.addControl(L.control.attribution({
  241. position: 'bottomright',
  242. prefix: '&copy; <a href="http://ad.cs.uni-freiburg.de">University of Freiburg, Chair of Algorithms and Data Structures</a>'
  243. }));
  244. L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
  245. maxZoom: 23,
  246. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>',
  247. id: 'mapbox.streets'
  248. }).addTo(map);
  249. var layer = L.featureGroup().addTo(map);
  250. var labelLayer = L.featureGroup().addTo(map);
  251. map.on("moveend", function() {
  252. render();
  253. });
  254. function render() {
  255. window.xmlhttp = new XMLHttpRequest();
  256. if (map.getZoom() < 11) {
  257. window.xmlhttp.onreadystatechange = function() {
  258. if (this.readyState == 4 && this.status == 200 && this == window.xmlhttp) {
  259. var content = JSON.parse(this.responseText);
  260. layer.clearLayers();
  261. labelLayer.clearLayers();
  262. var blur = 22 - map.getZoom();
  263. var rad = 25 - map.getZoom();
  264. layer.addLayer(L.heatLayer(content.ok, {
  265. max: 500,
  266. gradient: {
  267. 0: '#cbf7cb',
  268. 0.5: '#78f378',
  269. 1: '#29c329'
  270. },
  271. minOpacity: 0.65,
  272. blur: blur,
  273. radius: rad
  274. }));
  275. layer.addLayer(L.heatLayer(content.sugg, {
  276. max: 500,
  277. gradient: {
  278. 0: '#7f7fbd',
  279. 0.5: '#4444b3',
  280. 1: '#0606c1'
  281. },
  282. minOpacity: 0.65,
  283. blur: blur - 3,
  284. radius: Math.min(12, rad - 3)
  285. }));
  286. layer.addLayer(L.heatLayer(content.err, {
  287. max: 500,
  288. gradient: {
  289. 0: '#f39191',
  290. 0.5: '#ff5656',
  291. 1: '#ff0000'
  292. },
  293. minOpacity: 0.75,
  294. blur: blur - 3,
  295. radius: Math.min(10, rad - 3),
  296. maxZoom: 15
  297. }));
  298. }
  299. };
  300. window.xmlhttp.open("GET", backend + "/heatmap?z=" + map.getZoom() + "&bbox=" + [map.getBounds().getSouthWest().lat, map.getBounds().getSouthWest().lng, map.getBounds().getNorthEast().lat, map.getBounds().getNorthEast().lng].join(","), true);
  301. window.xmlhttp.send();
  302. } else {
  303. window.xmlhttp.onreadystatechange = function() {
  304. if (this.readyState == 4 && this.status == 200 && this == window.xmlhttp) {
  305. var content = JSON.parse(this.responseText);
  306. layer.clearLayers();
  307. labelLayer.clearLayers();
  308. document.groupIdx = {};
  309. document.nodeIdx = {};
  310. var stations = [];
  311. var labels = [];
  312. for (var i = 0; i < content.stats.length; i++) {
  313. stat = content.stats[i];
  314. var ndMarker = marker(stat, map.getZoom());
  315. stations.push(ndMarker);
  316. document.nodeIdx[stat.id] = ndMarker;
  317. }
  318. var groups = [];
  319. for (var i = 0; i < content.groups.length; i++) {
  320. group = content.groups[i];
  321. var groupPoly = poly(group, map.getZoom());
  322. groups.push(groupPoly);
  323. document.groupIdx[group.id] = groupPoly;
  324. }
  325. var suggs = [];
  326. for (var i = 0; i < content.su.length; i++) {
  327. sugg = content.su[i];
  328. var a = sugArr(sugg, map.getZoom());
  329. suggs.push(a);
  330. }
  331. if (map.getZoom() > 13) {
  332. layer.addLayer(L.featureGroup(groups).on('click', function(a) {
  333. openGroup(a.layer.options.id, a.layer.getBounds().getCenter());
  334. }));
  335. }
  336. if (map.getZoom() > 15) {
  337. labelLayer.addLayer(L.featureGroup(labels));
  338. layer.addLayer(L.featureGroup(suggs).on('click', function(a) {
  339. openStat(a.layer.options.id);
  340. }));
  341. }
  342. layer.addLayer(L.featureGroup(stations).on('click', function(a) {
  343. openStat(a.layer.options.id);
  344. }));
  345. }
  346. };
  347. window.xmlhttp.open("GET", backend + "/map?bbox=" + [map.getBounds().getSouthWest().lat, map.getBounds().getSouthWest().lng, map.getBounds().getNorthEast().lat, map.getBounds().getNorthEast().lng].join(","), true);
  348. window.xmlhttp.send();
  349. }
  350. }
  351. render();