script.js 15 KB

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