script.js 16 KB

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