mapicgc-gl-js 0.0.37 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/map/Map.js CHANGED
@@ -2,6 +2,10 @@ import maplibregl from "maplibre-gl";
2
2
  // import flatgeobuf from "flatgeobuf";
3
3
  import Pitch3DToggleControl from "../controls/Toggle3DControl.js";
4
4
  // import MeasuresControl from 'maplibre-gl-measures';
5
+ import { MapboxLayer } from "@deck.gl/mapbox";
6
+ import { Tile3DLayer } from "@deck.gl/geo-layers";
7
+ import { Tiles3DLoader } from "@loaders.gl/3d-tiles";
8
+ import { AmbientLight, LightingEffect } from "@deck.gl/core";
5
9
  import {
6
10
  MaplibreExportControl,
7
11
  Size,
@@ -52,8 +56,8 @@ export default class Map {
52
56
  }
53
57
  }
54
58
  }
55
- // console.log('dseeee', options)
56
- options.maxZoom = 18;
59
+
60
+ // options.maxZoom = 18;
57
61
  options.maxPitch = 85;
58
62
  options.maplibreLogo = false;
59
63
  options.attributionControl = false;
@@ -66,6 +70,99 @@ export default class Map {
66
70
  compact: true,
67
71
  })
68
72
  );
73
+
74
+ this.map.on("load", () => {
75
+ // console.log('sss', this.map.getStyle().name)
76
+
77
+ if (window.document.querySelector(".maplibregl-compact-show")) {
78
+ var element = window.document.querySelector(".maplibregl-compact-show");
79
+ element.classList.remove("maplibregl-compact-show");
80
+ }
81
+
82
+ this._dealOrto3dStyle(this.map.getStyle().name);
83
+ });
84
+ }
85
+
86
+ _dealOrto3dStyle(name) {
87
+ try {
88
+ if (name == "orto3d") {
89
+ // console.log('entro', name)
90
+ const ambientLight = new AmbientLight({
91
+ intensity: 4,
92
+ });
93
+
94
+ const lightingEffect = new LightingEffect({
95
+ ambientLight,
96
+ });
97
+
98
+ this.map.setTerrain({
99
+ source: defaultOptions.map3dOptions.terrainSource,
100
+ exaggeration: 1,
101
+ });
102
+
103
+ const citiesMapboxLayer = this._createCitiesMapboxLayer();
104
+
105
+ if (!this.map.getLayer(defaultOptions.map3dOptions.layerId3d)) {
106
+ this.map.addLayer(
107
+ citiesMapboxLayer,
108
+ defaultOptions.map3dOptions.layerIdOrder
109
+ );
110
+ this.map.setLayerZoomRange(
111
+ defaultOptions.map3dOptions.layerId3d,
112
+ defaultOptions.map3dOptions.minZoomRange,
113
+ defaultOptions.map3dOptions.maxZoomRange
114
+ );
115
+
116
+ citiesMapboxLayer.deck.setProps({
117
+ effects: [lightingEffect],
118
+ });
119
+ }
120
+ } else {
121
+ if (this.map.getLayer(defaultOptions.map3dOptions.layerId3d)) {
122
+ this.map.removeLayer(defaultOptions.map3dOptions.layerId3d);
123
+
124
+ this.map.setTerrain(null);
125
+ }
126
+ }
127
+ } catch (error) {
128
+ console.error(`Error dealing orto 3D: ${error.message}`);
129
+ return null;
130
+ }
131
+ }
132
+
133
+ _createCitiesMapboxLayer() {
134
+ try {
135
+ const citiesMapboxLayer = new MapboxLayer({
136
+ id: defaultOptions.map3dOptions.layerId3d,
137
+ type: Tile3DLayer,
138
+ data: defaultOptions.map3dOptions.urlTilesetCities,
139
+ loader: Tiles3DLoader,
140
+
141
+ loadOptions: {
142
+ tileset: {
143
+ viewDistanceScale: 1,
144
+ updateTransforms: false,
145
+ maximumScreenSpaceError:
146
+ defaultOptions.map3dOptions.spaceErrorFactor,
147
+ },
148
+ },
149
+
150
+ onTilesetLoad: (tileset3d) => {
151
+ tileset3d.options.maximumScreenSpaceError =
152
+ defaultOptions.map3dOptions.spaceErrorFactor;
153
+ },
154
+ onTileLoad: (tileHeader) => {
155
+ tileHeader.content.cartographicOrigin.z -=
156
+ defaultOptions.map3dOptions.zfactor;
157
+ },
158
+ operation: "terrain+draw",
159
+ });
160
+
161
+ return citiesMapboxLayer;
162
+ } catch (error) {
163
+ console.error(`Error adding MapboxLayer: ${error.message}`);
164
+ return null;
165
+ }
69
166
  }
70
167
 
71
168
  /**
@@ -74,10 +171,12 @@ export default class Map {
74
171
  * @returns {Object} - The current position of the search result.
75
172
  */
76
173
 
77
- addGeocoderICGC() {
174
+ addGeocoderICGC(position) {
78
175
  try {
79
176
  // console.log("hello geocoder");
80
-
177
+ if (position === undefined ){
178
+ position = 'top-right'
179
+ }
81
180
  const urlSearchPelias =
82
181
  "https://eines.icgc.cat/geocodificador/autocompletar?text=";
83
182
 
@@ -137,7 +236,7 @@ export default class Map {
137
236
  };
138
237
 
139
238
  // Pass in or define a geocoding API that matches the above
140
- this.map.addControl(new MaplibreGeocoder(geocoderApi, options));
239
+ this.map.addControl(new MaplibreGeocoder(geocoderApi, options), position);
141
240
  let inputsearch = document.getElementsByClassName(
142
241
  "maplibregl-ctrl-geocoder--input"
143
242
  );
@@ -272,7 +371,6 @@ inputsearch[0].addEventListener('input', function(event) {
272
371
  * @param {string} name - The geometry name (e.g., 'buildings').
273
372
  * @param {Object} options - Additional options for configuring the layer.
274
373
  */
275
-
276
374
  async fetchData(url, name, options) {
277
375
  try {
278
376
  const response = await fetch(url);
@@ -399,6 +497,28 @@ inputsearch[0].addEventListener('input', function(event) {
399
497
  const response = await fetch(url);
400
498
  const geojson = await response.json();
401
499
 
500
+ const menuGroup = document.getElementById("menu-group");
501
+
502
+
503
+
504
+ if (featureTree !== 'all'){
505
+
506
+
507
+ const nameTitle = document.createElement("div");
508
+ nameTitle.id = "titleDivMenu"
509
+ nameTitle.textContent = name;
510
+ menuGroup.appendChild(nameTitle);
511
+
512
+ const featureTreeTitle = document.createElement("div");
513
+ featureTreeTitle.id = "titleDivMenuSub"
514
+ featureTreeTitle.textContent = `📂 ${featureTree}`;
515
+ menuGroup.appendChild(featureTreeTitle);
516
+
517
+
518
+ }else{
519
+
520
+ }
521
+
402
522
  let type = geojson.features[0].geometry.type;
403
523
 
404
524
  if ( featureTree === 'all'){
@@ -506,21 +626,21 @@ if ( featureTree === 'all'){
506
626
  }
507
627
  // geojsonStore = geojson
508
628
  // map.addLayerTree(geojson);
509
- map.addMenuItem(name);
510
629
 
630
+ map.addMenuItem(name);
631
+ map.addFeatureQuery(name)
511
632
  }else{
512
633
 
513
634
  let field= featureTree
514
- // console.log('field', field, geojson.features)
515
635
 
516
- // let lll = geojson.features
517
636
  const layers = {};
518
637
  geojson.features.forEach(feature => {
519
638
  const fieldMarker = feature.properties[field];
520
-
521
- // Verificar si la capa ya existe
639
+ // aqui podriem mirar si te simbologia i afegir-la a la capa
640
+
641
+
522
642
  if (!layers[fieldMarker]) {
523
- // Si no existe, crear la capa
643
+
524
644
  if (type.includes("ine")) {
525
645
  //line
526
646
  if (options !== undefined){
@@ -630,7 +750,6 @@ geojson.features.forEach(feature => {
630
750
  }
631
751
  }
632
752
 
633
- // Agregar la nueva capa al objeto de capas
634
753
  layers[fieldMarker] = true;
635
754
  map.addMenuItem(fieldMarker);
636
755
  }
@@ -639,78 +758,68 @@ geojson.features.forEach(feature => {
639
758
 
640
759
  }
641
760
 
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
- //add feature queries
651
- // map.addFeatureQuery(name);
652
761
  } catch (error) {
653
762
  console.error(`Error fetching data: ${error.message}`);
654
763
  }
655
764
  }
656
765
 
657
- async geocodeAddress() {
658
- try {
659
- var inputElement = document.getElementById("addressInput");
660
- var address = inputElement.value;
661
- const response = await fetch(url);
662
- const geojson = await response.json();
663
- var resultsContainer = document.getElementById("results");
664
- resultsContainer.innerHTML = ""; // Limpiar resultados anteriores
665
-
666
- function geocode(address) {
667
- var result = turf.filter(geojson, "address", address);
668
- return result.features.slice(0, 5);
669
- }
670
-
671
- var results = geocode(address);
672
-
673
- if (results.length > 0) {
674
- results.forEach(function (feature) {
675
- var coordinates = feature.geometry.coordinates;
676
- var address = feature.properties.address;
677
-
678
- var resultItem = document.createElement("div");
679
- resultItem.innerHTML =
680
- "<strong>" +
681
- address +
682
- "</strong><br>Coordenadas: " +
683
- coordinates.join(", ");
684
-
685
- resultsContainer.appendChild(resultItem);
686
- });
687
- } else {
688
- resultsContainer.innerHTML =
689
- "No se encontraron resultados para la dirección: " + address;
690
- }
691
- } catch (error) {}
692
- }
693
- /**
694
- * Fetches GeoJSON data from a URL and adds a corresponding layer to the map based on the specified geometry type.
695
- * @param {string} url - The URL to fetch GeoJSON data from.
696
- * @param {string} type - The geometry type (e.g., 'line', 'polygon', 'point').
697
- * @param {string} name - The geometry name (e.g., 'buildings').
698
- * @param {Object} options - Additional options for configuring the layer.
699
- */
700
-
701
- async fetchDataWithSearchbox(url, type, name, optionsGeo, options) {
702
- try {
703
- const response = await fetch(url);
704
- const geojson = await response.json();
705
-
706
- function geocode(address) {
707
- var result = turf.filter(geojson, "address", address);
708
- return result.features.slice(0, 5);
709
- }
710
- } catch (error) {
711
- console.error(`Error fetching data: ${error.message}`);
712
- }
713
- }
766
+ // async geocodeAddress() {
767
+ // try {
768
+ // var inputElement = document.getElementById("addressInput");
769
+ // var address = inputElement.value;
770
+ // const response = await fetch(url);
771
+ // const geojson = await response.json();
772
+ // var resultsContainer = document.getElementById("results");
773
+ // resultsContainer.innerHTML = ""; // Limpiar resultados anteriores
774
+
775
+ // function geocode(address) {
776
+ // var result = turf.filter(geojson, "address", address);
777
+ // return result.features.slice(0, 5);
778
+ // }
779
+
780
+ // var results = geocode(address);
781
+
782
+ // if (results.length > 0) {
783
+ // results.forEach(function (feature) {
784
+ // var coordinates = feature.geometry.coordinates;
785
+ // var address = feature.properties.address;
786
+
787
+ // var resultItem = document.createElement("div");
788
+ // resultItem.innerHTML =
789
+ // "<strong>" +
790
+ // address +
791
+ // "</strong><br>Coordenadas: " +
792
+ // coordinates.join(", ");
793
+
794
+ // resultsContainer.appendChild(resultItem);
795
+ // });
796
+ // } else {
797
+ // resultsContainer.innerHTML =
798
+ // "No se encontraron resultados para la dirección: " + address;
799
+ // }
800
+ // } catch (error) {}
801
+ // }
802
+ // /**
803
+ // * Fetches GeoJSON data from a URL and adds a corresponding layer to the map based on the specified geometry type.
804
+ // * @param {string} url - The URL to fetch GeoJSON data from.
805
+ // * @param {string} type - The geometry type (e.g., 'line', 'polygon', 'point').
806
+ // * @param {string} name - The geometry name (e.g., 'buildings').
807
+ // * @param {Object} options - Additional options for configuring the layer.
808
+ // */
809
+
810
+ // async fetchDataWithSearchbox(url, type, name, optionsGeo, options) {
811
+ // try {
812
+ // const response = await fetch(url);
813
+ // const geojson = await response.json();
814
+
815
+ // function geocode(address) {
816
+ // var result = turf.filter(geojson, "address", address);
817
+ // return result.features.slice(0, 5);
818
+ // }
819
+ // } catch (error) {
820
+ // console.error(`Error fetching data: ${error.message}`);
821
+ // }
822
+ // }
714
823
 
715
824
  /**
716
825
  * Adds an event listener to the map.
@@ -761,6 +870,7 @@ geojson.features.forEach(feature => {
761
870
  setStyle(style, options) {
762
871
  try {
763
872
  console.log("kk", style);
873
+ /*
764
874
  for (const key of Styles) {
765
875
  if (Styles.hasOwnProperty(key)) {
766
876
  const styl = Styles[key];
@@ -770,6 +880,7 @@ geojson.features.forEach(feature => {
770
880
  }
771
881
  }
772
882
  }
883
+ */
773
884
  console.log("base", style);
774
885
 
775
886
  if (options !== undefined) {
@@ -777,6 +888,20 @@ geojson.features.forEach(feature => {
777
888
  } else {
778
889
  this.map.setStyle(style);
779
890
  }
891
+
892
+ this.map.on("styledata", () => {
893
+ if (window.document.querySelector(".maplibregl-compact-show")) {
894
+ var element = window.document.querySelector(
895
+ ".maplibregl-compact-show"
896
+ );
897
+ element.classList.remove("maplibregl-compact-show");
898
+ }
899
+
900
+ this._dealOrto3dStyle(this.map.getStyle().name);
901
+ //init test3d
902
+
903
+ //end test 3d
904
+ });
780
905
  } catch (error) {
781
906
  console.error(`Error setting style: ${error.message}`);
782
907
  }
@@ -811,15 +936,33 @@ geojson.features.forEach(feature => {
811
936
  /**
812
937
  * Sets layout property for a layer on the map.
813
938
  * @function setLayoutProperty
814
- * @param {Object} object - Object containing layer ID and property to set.
939
+ * @param {Object} object - Object containing layer ID.
940
+ * @param {Object} property - Object containing property to set.
941
+ * @param {Object} value - Object containing value.
815
942
  */
816
- setLayoutProperty(object) {
943
+ setLayoutProperty(object, property, value) {
817
944
  try {
818
- this.map.setLayoutProperty(object);
945
+ this.map.setLayoutProperty(object, property, value);
819
946
  } catch (error) {
820
947
  console.error(`Error setting layout property: ${error.message}`);
821
948
  }
822
949
  }
950
+
951
+ /**
952
+ * Sets layout property for a layer on the map.
953
+ * @function setPaintProperty
954
+ * @param {Object} object - Object containing layer ID.
955
+ * @param {Object} property - Object containing property to set.
956
+ * @param {Object} value - Object containing value.
957
+ */
958
+ setPaintProperty(object, property, value) {
959
+ try {
960
+ this.map.setPaintProperty(object, property, value);
961
+ } catch (error) {
962
+ console.error(`Error setting paint property: ${error.message}`);
963
+ }
964
+ }
965
+
823
966
  /**
824
967
  * Adds a control to the map with the specified position.
825
968
  * @function addControl
@@ -938,14 +1081,21 @@ geojson.features.forEach(feature => {
938
1081
  */
939
1082
  setTerrain(options) {
940
1083
  try {
941
- this.map.on("load", () => {
942
- return this.map.setTerrain(options);
943
- });
1084
+ //this.map.on("load", () => {
1085
+ return this.map.setTerrain(options);
1086
+ //});
944
1087
  // this.map.getZoom();
945
1088
  } catch (error) {
946
1089
  console.error(`Error setting terrain: ${error.message}`);
947
1090
  }
948
1091
  }
1092
+ setLayerZoomRange(layerId, nimZoom, maxZoom) {
1093
+ try {
1094
+ return this.map.setLayerZoomRange(layerId, nimZoom, maxZoom);
1095
+ } catch (error) {
1096
+ console.error(`Error setting terrain: ${error.message}`);
1097
+ }
1098
+ }
949
1099
 
950
1100
  /**
951
1101
  * Retrieves the canvas of the map.
@@ -976,10 +1126,11 @@ geojson.features.forEach(feature => {
976
1126
  * Adds a layer to the map.
977
1127
  * @function addLayer
978
1128
  * @param {Object} layer - Options for the layer to add.
1129
+ * @param {string} layerIdOrder - Optional layer Id draw position.
979
1130
  */
980
- addLayer(layer) {
1131
+ addLayer(layer, layerIdOrder) {
981
1132
  try {
982
- this.map.addLayer(layer);
1133
+ this.map.addLayer(layer, layerIdOrder);
983
1134
  } catch (error) {
984
1135
  console.error(`Error adding layer: ${error.message}`);
985
1136
  }
@@ -1113,7 +1264,7 @@ geojson.features.forEach(feature => {
1113
1264
  /**
1114
1265
  * Adds base layers to the map.
1115
1266
  * @function addBasemapsICGC
1116
- * @param {Object[]} baseLayers - Array of base layer objects.
1267
+ * @param {Object[]} basesArray - Array of base layer objects.
1117
1268
  */
1118
1269
  addBasemapsICGC(basesArray) {
1119
1270
  try {
@@ -1568,6 +1719,7 @@ geojson.features.forEach(feature => {
1568
1719
  /**
1569
1720
  * Adds a fullscreen control to the map.
1570
1721
  * @function addFullScreen
1722
+ * @param {string} [position='top-right'] - Position to add the control on the map.
1571
1723
  */
1572
1724
  addFullScreen(position) {
1573
1725
  try {
@@ -1768,8 +1920,7 @@ geojson.features.forEach(feature => {
1768
1920
  * Adds an ICGC image layer to the map based on the specified name and year.
1769
1921
  * @function addImageLayerICGC
1770
1922
  * @param {string} name - The name of the layer. Mandatory. options: 'orto', 'geo', 'slope', 'dem', 'relleu', etc.
1771
- * @param {integer} year - The year of the image layer (optional, applicable only for certain layers like 'orto').
1772
- */
1923
+ */
1773
1924
  addImageLayerICGC(name) {
1774
1925
  try {
1775
1926
  console.log("name", name);
@@ -1859,9 +2010,8 @@ geojson.features.forEach(feature => {
1859
2010
  * Adds an ICGC vector layer to the map based on the specified name and year.
1860
2011
  * @function addVectorLayerICGC
1861
2012
  * @param {string} url - The url of the vector layer.
1862
- * @param {string} year - The year associated with the vector layer (optional).
1863
2013
  */
1864
- async addVectorLayerICGC(layerUrl, name, year) {
2014
+ async addVectorLayerICGC(layerUrl) {
1865
2015
  try {
1866
2016
  function getKeyByUrl(url) {
1867
2017
  for (const key in Layers.VectorAdmin) {
@@ -1945,6 +2095,7 @@ geojson.features.forEach(feature => {
1945
2095
  // let op = Terrains.find(
1946
2096
  // (objeto) => objeto.name === resolution
1947
2097
  // );
2098
+ // console.log('res', resolution)
1948
2099
  let op;
1949
2100
  for (const key in Terrains) {
1950
2101
  if (Terrains.hasOwnProperty(key)) {
@@ -1952,26 +2103,33 @@ geojson.features.forEach(feature => {
1952
2103
 
1953
2104
  if (objeto === resolution) {
1954
2105
  op = objeto;
1955
- // Si encontramos el objeto, podemos salir del bucle
2106
+
1956
2107
  }
1957
2108
  }
1958
2109
  }
1959
2110
 
1960
2111
  let urlTerrainICGC = op;
2112
+ if (resolution.includes('terrarium') ){
2113
+ // console.log('rsssssses', resolution)
2114
+ // Add terrain source
2115
+ this.map.addSource("terrainICGC-src", {
2116
+ type: "raster-dem",
2117
+ tiles: [urlTerrainICGC],
2118
+ tileSize: 512,
2119
+ encoding: "terrarium",
2120
+ maxzoom: 16,
2121
+ });
2122
+ }else{
2123
+ this.map.addSource("terrainICGC-src", {
2124
+ type: "raster-dem",
2125
+ tiles: [urlTerrainICGC],
2126
+ tileSize: 512,
2127
+ maxzoom: 16,
2128
+ });
2129
+ }
2130
+
1961
2131
 
1962
- // Add terrain source
1963
- this.map.addSource("terrainICGC-src", {
1964
- type: "raster-dem",
1965
- tiles: [urlTerrainICGC],
1966
- tileSize: 512,
1967
- maxzoom: 16,
1968
- });
1969
-
1970
- // Add terrain layer
1971
- // this.map.setTerrain({
1972
- // source: "terrainMapZen",
1973
- // exaggeration: 1.5,
1974
- // });
2132
+
1975
2133
  this.map.setTerrain({
1976
2134
  source: "terrainICGC-src",
1977
2135
  exaggeration: 1.5,