script.js 15 KB

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