index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import * as mapboxgl from "mapbox-gl";
  2. import Bbox from "@turf/bbox";
  3. import BboxPolygon from "@turf/bbox-polygon";
  4. import Center from "@turf/center";
  5. import BooleanContains from "@turf/boolean-contains";
  6. import BooleanOverlap from "@turf/boolean-overlap";
  7. import Buffer from "@turf/buffer";
  8. import confini from "./layers/confini.json";
  9. import confiniHole from "./layers/confini_hole.json";
  10. import aree_omogenee from "./layers/aree_omogenee.json";
  11. import scuole_buffer from "./layers/scuole_buffer.json";
  12. import luoghi_monumentali_buffer from "./layers/luoghi_monumentali_buffer.json";
  13. import mercati_buffer from "./layers/mercati_buffer.json";
  14. import musei_buffer from "./layers/musei_buffer.json";
  15. import ospedali_cliniche_buffer from "./layers/ospedali_cliniche_buffer.json";
  16. import stazioni_ferrovie_buffer from "./layers/stazioni_ferrovie_buffer.json";
  17. import stazioni_metro_buffer from "./layers/stazioni_metro_buffer.json";
  18. import parchi_buffer from "./layers/parchi_buffer.json";
  19. import "./style.css";
  20. mapboxgl.accessToken =
  21. "pk.eyJ1IjoidGVvIiwiYSI6IllvZUo1LUkifQ.dirqtn275pAKdnqtLM2HSw";
  22. const bounds = Bbox(confini);
  23. const maxBounds = Bbox(Buffer(confini, 100));
  24. const map = new mapboxgl.Map({
  25. container: "map-home",
  26. zoom: 11,
  27. center: Center(confini).geometry.coordinates,
  28. style: "mapbox://styles/teo/cjyxhh5n92wy51ck4a4uiymtu/draft",
  29. maxBounds: [[maxBounds[0], maxBounds[1]], [maxBounds[2], maxBounds[3]]],
  30. maxZoom: 16,
  31. scrollZoom: false
  32. });
  33. const nav = new mapboxgl.NavigationControl();
  34. map.addControl(nav, "top-right");
  35. map.fitBounds([[bounds[0], bounds[1]], [bounds[2], bounds[3]]], {
  36. padding: 20
  37. });
  38. // const slider = document.getElementById("slider");
  39. // const sliderValue = document.getElementById("slider-value");
  40. map.on("load", function() {
  41. map.setLayoutProperty("building", "visibility", "none");
  42. map.setLayoutProperty("building-outline", "visibility", "none");
  43. map.addSource("aree_omogeneeSource", {
  44. type: "geojson",
  45. data: aree_omogenee
  46. });
  47. map.addSource("scuoleBufferSource", {
  48. type: "geojson",
  49. data: scuole_buffer
  50. });
  51. map.addSource("luoghiMonumentaliBufferSource", {
  52. type: "geojson",
  53. data: luoghi_monumentali_buffer
  54. });
  55. map.addSource("mercatiBufferSource", {
  56. type: "geojson",
  57. data: mercati_buffer
  58. });
  59. map.addSource("museiBufferSource", {
  60. type: "geojson",
  61. data: musei_buffer
  62. });
  63. map.addSource("ospedaliClinicheBufferSource", {
  64. type: "geojson",
  65. data: ospedali_cliniche_buffer
  66. });
  67. map.addSource("stazioniFerrovieBufferSource", {
  68. type: "geojson",
  69. data: stazioni_ferrovie_buffer
  70. });
  71. map.addSource("stazioniMetroBufferSource", {
  72. type: "geojson",
  73. data: stazioni_metro_buffer
  74. });
  75. map.addSource("parchiBufferSource", {
  76. type: "geojson",
  77. data: parchi_buffer
  78. });
  79. map.addSource("confiniHoleSource", {
  80. type: "geojson",
  81. data: confiniHole
  82. });
  83. map.addSource("confiniSource", {
  84. type: "geojson",
  85. data: confini
  86. });
  87. map.addSource("osmbuildings", {
  88. type: "vector",
  89. url: "https://data.osmbuildings.org/0.2/rkc8ywdl/tile.json"
  90. });
  91. const layers = map.getStyle().layers;
  92. //console.log(layers);
  93. let labelLayerId = "";
  94. for (let i = 0; i < layers.length; i++) {
  95. if (layers[i].type === "symbol" && layers[i].layout["text-field"]) {
  96. labelLayerId = layers[i].id;
  97. break;
  98. }
  99. }
  100. map.addLayer(
  101. {
  102. id: "aree_omogenee",
  103. type: "fill",
  104. source: "aree_omogeneeSource",
  105. paint: {
  106. "fill-color": "#f5553d",
  107. "fill-opacity": 1
  108. }
  109. },
  110. "building-outline"
  111. );
  112. map.addLayer(
  113. {
  114. id: "parchi_buffer",
  115. type: "fill",
  116. source: "parchiBufferSource",
  117. paint: {
  118. "fill-color": "#f5553d",
  119. "fill-opacity": 1
  120. }
  121. },
  122. "building-outline"
  123. );
  124. map.addLayer(
  125. {
  126. id: "mercati_buffer",
  127. type: "fill",
  128. source: "mercatiBufferSource",
  129. paint: {
  130. "fill-color": "#f5553d",
  131. "fill-opacity": 1
  132. }
  133. },
  134. "building-outline"
  135. );
  136. map.addLayer(
  137. {
  138. id: "scuole_buffer",
  139. type: "fill",
  140. source: "scuoleBufferSource",
  141. paint: {
  142. "fill-color": "#f5553d",
  143. "fill-opacity": 1
  144. }
  145. },
  146. "building-outline"
  147. );
  148. map.addLayer(
  149. {
  150. id: "musei_buffer",
  151. type: "fill",
  152. source: "museiBufferSource",
  153. paint: {
  154. "fill-color": "#f5553d",
  155. "fill-opacity": 1
  156. }
  157. },
  158. "building-outline"
  159. );
  160. map.addLayer(
  161. {
  162. id: "ospedali_cliniche_buffer",
  163. type: "fill",
  164. source: "ospedaliClinicheBufferSource",
  165. paint: {
  166. "fill-color": "#f5553d",
  167. "fill-opacity": 1
  168. }
  169. },
  170. "building-outline"
  171. );
  172. map.addLayer(
  173. {
  174. id: "stazioni_ferrovie_buffer",
  175. type: "fill",
  176. source: "stazioniFerrovieBufferSource",
  177. paint: {
  178. "fill-color": "#f5553d",
  179. "fill-opacity": 1
  180. }
  181. },
  182. "building-outline"
  183. );
  184. map.addLayer(
  185. {
  186. id: "stazioni_metro_buffer",
  187. type: "fill",
  188. source: "stazioniMetroBufferSource",
  189. paint: {
  190. "fill-color": "#f5553d",
  191. "fill-opacity": 1
  192. }
  193. },
  194. "building-outline"
  195. );
  196. map.addLayer(
  197. {
  198. id: "luoghi_monumentali_buffer",
  199. type: "fill",
  200. source: "luoghiMonumentaliBufferSource",
  201. paint: {
  202. "fill-color": "#f5553d",
  203. "fill-opacity": 1
  204. }
  205. },
  206. "building-outline"
  207. );
  208. map.addLayer(
  209. {
  210. id: "buildings-base",
  211. type: "fill",
  212. source: "osmbuildings",
  213. "source-layer": "building",
  214. // filter: ["all", ["==", "minHeight", 0]],
  215. // layout: {},
  216. paint: {
  217. "fill-color": "#fff",
  218. "fill-opacity": { stops: [[16, 0], [17.5, 1]] }
  219. }
  220. },
  221. labelLayerId
  222. );
  223. map.addLayer(
  224. {
  225. id: "buildings-3d",
  226. type: "fill-extrusion",
  227. source: "osmbuildings",
  228. "source-layer": "building",
  229. paint: {
  230. "fill-extrusion-color": "#f5553d",
  231. "fill-extrusion-height": [
  232. "interpolate",
  233. ["linear"],
  234. ["zoom"],
  235. 15,
  236. 0,
  237. 15.05,
  238. [
  239. "number",
  240. ["get", "height"],
  241. ["*", ["number", ["get", "levels"], 3], 3]
  242. ]
  243. ],
  244. "fill-extrusion-base": [
  245. "interpolate",
  246. ["linear"],
  247. ["zoom"],
  248. 15,
  249. 0,
  250. 15.05,
  251. [
  252. "number",
  253. ["get", "minHeight"],
  254. ["*", ["number", ["get", "minLevel"], 0], 3]
  255. ]
  256. ],
  257. "fill-extrusion-opacity": {
  258. stops: [[15, 0], [15.5, 1], [16, 1], [17.5, 0.6]]
  259. }
  260. },
  261. //filter: ["!=", ["string", ["get", "shape"], ""], "none"]
  262. filter: ["match", ["get", "id"], "", true, false]
  263. },
  264. labelLayerId
  265. );
  266. map.addLayer(
  267. {
  268. id: "confiniHole",
  269. source: "confiniHoleSource",
  270. type: "fill",
  271. paint: {
  272. "fill-color": "#fff"
  273. }
  274. },
  275. "settlement-subdivision-label"
  276. );
  277. map.addLayer(
  278. {
  279. id: "confini",
  280. source: "confiniSource",
  281. type: "line",
  282. paint: {
  283. "line-color": "#000"
  284. }
  285. },
  286. "settlement-subdivision-label"
  287. );
  288. });
  289. map.on("moveend", function() {
  290. if (map.getZoom() >= 15) {
  291. const aulenti = aree_omogenee.features.filter(
  292. f => f.properties.area === "C.so Garibladi, C.so Como, Gae Aulenti"
  293. )[0];
  294. let buildings = map.queryRenderedFeatures({
  295. layers: ["buildings-base"],
  296. filter: ["!=", ["string", ["get", "shape"], ""], "none"]
  297. });
  298. let features = map.queryRenderedFeatures({
  299. layers: [
  300. "aree_omogenee",
  301. "parchi_buffer",
  302. "luoghi_monumentali_buffer",
  303. "mercati_buffer",
  304. "musei_buffer",
  305. "ospedali_cliniche_buffer",
  306. "scuole_buffer",
  307. "stazioni_ferrovie_buffer",
  308. "stazioni_metro_buffer"
  309. ]
  310. });
  311. const filtered = buildings
  312. .filter(b => {
  313. let inside = false;
  314. for (let feature of features) {
  315. //console.log(b, feature);
  316. if (BooleanContains(feature, b)) {
  317. inside = true;
  318. break;
  319. } else {
  320. inside = false;
  321. }
  322. }
  323. return inside;
  324. })
  325. .map(b => b.properties.id);
  326. const ids = [...new Set(filtered)].filter(i => i);
  327. const filter = ["match", ["get", "id"], ids, true, false];
  328. if (ids.length) {
  329. map.setFilter("buildings-3d", filter);
  330. }
  331. }
  332. });
  333. map.on("zoomend", function() {
  334. if (map.getZoom() >= 15) {
  335. const aulenti = aree_omogenee.features.filter(
  336. f => f.properties.area === "C.so Garibladi, C.so Como, Gae Aulenti"
  337. )[0];
  338. let buildings = map.queryRenderedFeatures({
  339. layers: ["buildings-base"],
  340. filter: ["!=", ["string", ["get", "shape"], ""], "none"]
  341. });
  342. let features = map.queryRenderedFeatures({
  343. layers: [
  344. "aree_omogenee",
  345. "parchi_buffer",
  346. "luoghi_monumentali_buffer",
  347. "mercati_buffer",
  348. "musei_buffer",
  349. "ospedali_cliniche_buffer",
  350. "scuole_buffer",
  351. "stazioni_ferrovie_buffer",
  352. "stazioni_metro_buffer"
  353. ]
  354. });
  355. const filtered = buildings
  356. .filter(b => {
  357. let inside = false;
  358. for (let feature of features) {
  359. //console.log(b, feature);
  360. if (BooleanContains(feature, b)) {
  361. inside = true;
  362. break;
  363. } else {
  364. inside = false;
  365. }
  366. }
  367. return inside;
  368. })
  369. .map(b => b.properties.id);
  370. const ids = [...new Set(filtered)].filter(i => i);
  371. const filter = ["match", ["get", "id"], ids, true, false];
  372. if (ids.length) {
  373. map.setFilter("buildings-3d", filter);
  374. }
  375. }
  376. });
  377. // var toggleableLayerIds = ["wbbuilding", "aereal"];
  378. //
  379. // for (var i = 0; i < toggleableLayerIds.length; i++) {
  380. // var id = toggleableLayerIds[i];
  381. //
  382. // var link = document.createElement("a");
  383. // link.href = "#";
  384. // link.className = "active";
  385. // link.textContent = id;
  386. //
  387. // link.onclick = function(e) {
  388. // var clickedLayer = this.textContent;
  389. // e.preventDefault();
  390. // e.stopPropagation();
  391. //
  392. // var visibility = map.getLayoutProperty(clickedLayer, "visibility");
  393. //
  394. // if (visibility === "visible") {
  395. // map.setLayoutProperty(clickedLayer, "visibility", "none");
  396. // this.className = "";
  397. // } else {
  398. // this.className = "active";
  399. // map.setLayoutProperty(clickedLayer, "visibility", "visible");
  400. // }
  401. // };
  402. //
  403. // var layers = document.getElementById("menu");
  404. // layers.appendChild(link);
  405. // }