script.js 15 KB

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