script.js 14 KB

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