@geogirafe/lib-geoportal 1.3.0-dev.3018313151 → 1.3.0-dev.3046054236

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.
@@ -5,7 +5,12 @@ import WMSCapabilities from 'ol/format/WMSCapabilities.js';
5
5
  import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo.js';
6
6
  import GeoJSON from 'ol/format/GeoJSON.js';
7
7
  import SelectionParam from '../../models/selectionparam.js';
8
- import Exception from 'jsts/java/lang/Exception.js';
8
+ import { debounce } from '../utils/debounce.js';
9
+ /**
10
+ * Abstract class for WMS client.
11
+ * This class is responsible for linking the WMS layers to OL layers on the map and
12
+ * more generally, to their linked OGC server.
13
+ */
9
14
  export default class WmsClient {
10
15
  map;
11
16
  ogcServer;
@@ -15,6 +20,15 @@ export default class WmsClient {
15
20
  wmsGetFeatureInfo = new WMSGetFeatureInfo();
16
21
  capabilityPromise = null;
17
22
  capabilities = null;
23
+ basemapLayers = new Map();
24
+ groupedLayers = new Map();
25
+ debounceUpdateLayers = debounce(this.updateLayerDirect.bind(this), 20);
26
+ layersToUpdate = new Map();
27
+ constructor(ogcServer, map, context) {
28
+ this.ogcServer = ogcServer;
29
+ this.map = map;
30
+ this.context = context;
31
+ }
18
32
  get state() {
19
33
  return this.context.stateManager.state;
20
34
  }
@@ -23,216 +37,372 @@ export default class WmsClient {
23
37
  this.context.configManager.Config.gmfauth?.audienceExcludedPaths ??
24
38
  []);
25
39
  }
26
- // The Id of this dictionary if an unique ID that allow the differenciantion of server queries.
27
- // For example, a combination of server URL and ImageType could be used.
28
- // Each element of this dictionary will generate 1 WMS server query
29
- layers = [];
30
- olayer;
31
- // Independent layers are layers that need to be queried alone
32
- // (not combine to other WMS layers in the same query)
33
- // The treeItemId will be used as key for this dictionary
34
- independentLayers = {};
35
- basemapLayers = {};
36
- constructor(ogcServer, map, context) {
37
- this.ogcServer = ogcServer;
38
- this.map = map;
39
- this.context = context;
40
+ /**
41
+ * Add a new basemap layer to the map.
42
+ */
43
+ addBasemapLayer(layerWms) {
44
+ const source = this.createImageWMSSource([layerWms]);
45
+ const oLayer = new ImageLayer({
46
+ source: source,
47
+ opacity: layerWms.opacity
48
+ });
49
+ // For basemap, set a minimal number (arbitrary defined to less than -5000)
50
+ oLayer.setZIndex(-5000 - layerWms.order);
51
+ this.basemapLayers.set(layerWms.treeItemId, {
52
+ layerWmsArray: [layerWms],
53
+ oLayer: oLayer
54
+ });
55
+ this.map.addLayer(oLayer);
40
56
  }
57
+ /**
58
+ * Remove every basemap layer from the map.
59
+ */
41
60
  removeAllBasemapLayers() {
42
- for (const basmapLayer of Object.values(this.basemapLayers)) {
43
- this.map.removeLayer(basmapLayer.olayer);
61
+ const pairedLayers = Array.from(this.basemapLayers.values());
62
+ for (const { oLayer } of pairedLayers) {
63
+ this.map.removeLayer(oLayer);
44
64
  }
45
- this.basemapLayers = {};
65
+ this.basemapLayers.clear();
46
66
  }
67
+ /**
68
+ * Add a new layer to the map.
69
+ * The expected "activeState" is already "on"!
70
+ * This is expected to be immediate.
71
+ */
47
72
  addLayer(layerWms) {
48
- this.addLayerInternal(layerWms);
49
- this.manageLayerOptions(layerWms);
50
- }
51
- addLayerInternal(layerWms) {
52
- this.layers.push(layerWms);
53
- if (!this.olayer) {
54
- // Create a new ol layer and add it to the right server
55
- this.olayer = new ImageLayer();
56
- // Set zindex for this new layer
57
- // (The bigger the order is, the deeper in the map it should be displayed.)
58
- // (order is the inverse of z-index)
59
- this.olayer.setZIndex(-layerWms.order);
60
- this.map.addLayer(this.olayer);
61
- }
62
- const source = this.createImageWMSSource();
63
- this.olayer.setSource(source);
73
+ this.updateLayerDirect([layerWms]);
64
74
  }
65
- createImageWMSSource(layerList = this.layers) {
66
- const url = this.ogcServer.url;
67
- const imageType = this.ogcServer.imageType;
68
- const orderedLayers = layerList.slice().sort((l1, l2) => {
69
- return l2.order - l1.order;
70
- });
71
- const orderedLayerNames = this.getOpenLayerLayerNames(orderedLayers);
72
- const requestedUrl = new URL(url);
73
- const shouldExclude = this.audienceExcludedPaths.some((pattern) => new RegExp(pattern).test(requestedUrl.pathname));
74
- const crossOrigin = this.state.oauth.audience?.includes(requestedUrl.hostname) && !shouldExclude ? 'use-credentials' : 'anonymous';
75
- const source = new ImageWMS({
76
- url: url,
77
- params: {
78
- LAYERS: orderedLayerNames,
79
- FORMAT: imageType
80
- },
81
- crossOrigin: crossOrigin
82
- });
83
- // We intercept the event in order to set an error icon if the WMS query has an error
84
- // Otherwise we do no see anything on the client.
85
- source.on('imageloaderror', () => {
86
- for (const layerWms of layerList) {
87
- this.context.layerManager.setError(layerWms, 'Image cannot be loaded from WMS Server');
88
- }
75
+ /**
76
+ * Remove a layer from the map.
77
+ * The expected "activeState" is already "off"!
78
+ * This is expected to be immediate.
79
+ */
80
+ removeLayer(layerWms) {
81
+ this.updateLayerDirect([layerWms]);
82
+ }
83
+ /**
84
+ * Change the opacity of layers on the map.
85
+ * This can come from a group of layers and will be delayed.
86
+ */
87
+ changeOpacity(layerWms) {
88
+ this.updateLayerDebounced(layerWms);
89
+ }
90
+ /**
91
+ * Change the filter of a layer on the map.
92
+ */
93
+ changeFilter(layerWms) {
94
+ this.updateLayerDirect([layerWms]);
95
+ }
96
+ /**
97
+ * Change the time restriction of layers on the map.
98
+ * This can come from a group of layers and will be delayed.
99
+ */
100
+ changeTimeRestriction(layerWms) {
101
+ this.updateLayerDebounced(layerWms);
102
+ }
103
+ /**
104
+ * Change the Swiper of a layer on the map.
105
+ * This is expected to be immediate.
106
+ */
107
+ prepareSwipe(layerWms) {
108
+ this.updateLayerDirect([layerWms]);
109
+ }
110
+ /**
111
+ * Refresh the Z-Index of every layer on the map.
112
+ */
113
+ refreshZIndexes() {
114
+ // We don't have any information about what moved. Redo all groups.
115
+ const allWmsLayers = [];
116
+ Array.from(this.groupedLayers.keys()).forEach((groupKey) => {
117
+ const group = this.groupedLayers.get(groupKey);
118
+ allWmsLayers.push(...group.layerWmsArray);
119
+ this.removeGroupedLayers(groupKey);
89
120
  });
90
- source.on('imageloadend', () => {
91
- for (const layerWms of layerList) {
92
- this.context.layerManager.unsetError(layerWms);
121
+ this.updateLayerDirect(allWmsLayers);
122
+ }
123
+ /**
124
+ * @returns true if a matching (OL) layer exists on the map, false otherwise.
125
+ */
126
+ layerExists(layerWms) {
127
+ const groupLayerInfo = this.findExistingGroupLayerInfo([layerWms]);
128
+ return !!(this.layerIsBasemapLayer(layerWms) || (groupLayerInfo && this.groupedLayers.get(groupLayerInfo)));
129
+ }
130
+ /**
131
+ * @returns the (OL) layer associated with the given layerWms if the layer exists on the map.
132
+ * The resulting layer MUST NOT be edited. Edit the model instead.
133
+ */
134
+ getOLayer(layerWms) {
135
+ if (this.basemapLayers.has(layerWms.treeItemId)) {
136
+ return this.basemapLayers.get(layerWms.treeItemId)?.oLayer ?? null;
137
+ }
138
+ const groupLayerInfo = this.findExistingGroupLayerInfo([layerWms]);
139
+ if (groupLayerInfo) {
140
+ const pairedLayer = this.groupedLayers.get(groupLayerInfo);
141
+ if (pairedLayer) {
142
+ return pairedLayer.oLayer;
93
143
  }
94
- });
95
- return source;
144
+ }
145
+ return null;
96
146
  }
97
- addBasemapLayer(layerWms) {
98
- const source = this.createImageWMSSource([layerWms]);
99
- const olayer = new ImageLayer({
100
- source: source,
101
- opacity: layerWms.opacity
102
- });
103
- // For basemap, set a minimal number (arbitrary defined to less than -5000)
104
- olayer.setZIndex(-5000 - layerWms.order);
105
- this.basemapLayers[layerWms.treeItemId] = {
106
- layerWms: layerWms,
107
- olayer: olayer
108
- };
109
- this.map.addLayer(olayer);
147
+ /**
148
+ * @returns The array of LayerWms, indicating which layer are grouped together.
149
+ */
150
+ getLayerWmsArrays() {
151
+ return Array.from(this.groupedLayers.values()).map((pairedLayer) => pairedLayer.layerWmsArray);
110
152
  }
111
- removeLayer(layerWms) {
112
- if (this.layerExists(layerWms)) {
113
- if (layerWms.treeItemId in this.independentLayers) {
114
- const olayer = this.independentLayers[layerWms.treeItemId].olayer;
115
- delete this.independentLayers[layerWms.treeItemId];
116
- this.map.removeLayer(olayer);
153
+ /**
154
+ * Update layer but with a small debounce to wait on other layers with similar changes.
155
+ * Useful to update multiple layers at once and avoid multiple rendering
156
+ * of the (OL) layer (avoid a blinking effect).
157
+ * @param layerWms
158
+ * @private
159
+ */
160
+ updateLayerDebounced(layerWms) {
161
+ this.layersToUpdate.set(layerWms.treeItemId, layerWms);
162
+ this.debounceUpdateLayers(Array.from(this.layersToUpdate.values()));
163
+ }
164
+ /**
165
+ * Update layers directly, without debounce.
166
+ * @private
167
+ */
168
+ updateLayerDirect(layerWmsArray) {
169
+ this.layersToUpdate.clear();
170
+ // Sort the layer per order (z-index).
171
+ layerWmsArray.sort((lWms1, lWms2) => lWms1.order - lWms2.order);
172
+ // Make groups regarding the properties of the layers (opacity, filter, etc.).
173
+ // Follow the order to not group a layer to a group after another group with no similar properties (respect z-index)
174
+ let previousLayerInfo;
175
+ let groupedLayers = [];
176
+ const groupOfGroupedLayers = [];
177
+ layerWmsArray.forEach((lWms) => {
178
+ const layerInfo = this.getLayerWmsInfo(lWms);
179
+ if (previousLayerInfo && this.isLayerInfoIdentical(previousLayerInfo, layerInfo)) {
180
+ // Note BGE 29-10-2026: Ideally check also if the group is possible regarding the layers from another OGC server.
181
+ groupedLayers.push(lWms);
117
182
  }
118
- else if (this.layerInStandardLayers(layerWms)) {
119
- // Get existing ol layer for this server and remove the wms layer from the source
120
- this.layers = this.layers.filter((l) => l.treeItemId !== layerWms.treeItemId);
121
- if (this.layers.length > 0) {
122
- // There are still layers in the list. => We update the layer source
123
- const source = this.createImageWMSSource();
124
- this.olayer.setSource(source);
125
- }
126
- else if (this.olayer) {
127
- // No more layer here.
128
- // => We simply remove the whole layer
129
- this.map.removeLayer(this.olayer);
130
- delete this.olayer;
131
- }
183
+ else {
184
+ previousLayerInfo = layerInfo;
185
+ groupedLayers = [lWms];
186
+ groupOfGroupedLayers.push(groupedLayers);
187
+ }
188
+ });
189
+ // Update/create Ol layers for every group.
190
+ groupOfGroupedLayers.forEach((groupedLayersWms) => this.updateGroupedLayers(groupedLayersWms));
191
+ }
192
+ /**
193
+ * Update, change, or add new groups of layers (GroupLayerInfo => PairedLayers (OL and LayerWms)).
194
+ * @param layerWmsArray layerWms with the assumption that they can be grouped together.
195
+ * @private
196
+ */
197
+ updateGroupedLayers(layerWmsArray) {
198
+ const groupLayerInfo = this.findExistingGroupLayerInfo(layerWmsArray);
199
+ if (groupLayerInfo) {
200
+ // The layer already exists.
201
+ const pairedLayers = this.groupedLayers.get(groupLayerInfo);
202
+ const amountOfLayer = pairedLayers?.layerWmsArray.length ?? 0;
203
+ if (amountOfLayer === layerWmsArray.length) {
204
+ // The same number of layers is changed? Update the group.
205
+ this.updateLayerGroup(groupLayerInfo);
132
206
  }
133
207
  else {
134
- console.warn('Nothing to remove !');
208
+ // The number of layers is changed? Delete the group and add new layers.
209
+ this.deleteGroupsAndCallUpdateOnLayers(layerWmsArray);
135
210
  }
136
211
  }
137
212
  else {
138
- console.error(`Cannot remove WMS-Layer ${layerWms.name} from the map: it does not exist!`);
213
+ // The layer does not exist. Add new layers and regroup new groups.
214
+ this.addNewGroupedLayers(layerWmsArray);
215
+ this.regroupSameGroups();
139
216
  }
140
217
  }
141
- layerExists(layerWms) {
142
- return (this.layerInStandardLayers(layerWms) ||
143
- this.layerIsIndependentLayer(layerWms) ||
144
- this.layerIsBasemapLayer(layerWms));
218
+ /**
219
+ * Find a GroupLayerInfo that matches at least one layer of the provided array.
220
+ * @returns The existing GroupLayerInfo (key of the list of existing layers) or undefined.
221
+ * @private
222
+ */
223
+ findExistingGroupLayerInfo(layerWmsArray) {
224
+ const layerWmsArrayTreeIds = new Set(layerWmsArray.map((layerWms) => layerWms.treeItemId));
225
+ const oLayers = Array.from(this.groupedLayers.keys());
226
+ return oLayers.find((oLayer) => this.groupedLayers.get(oLayer)?.layerWmsArray.some((lWms) => layerWmsArrayTreeIds.has(lWms.treeItemId)));
145
227
  }
146
- layerInStandardLayers(layerWms) {
147
- return this.layers.some((l) => l.treeItemId === layerWms.treeItemId);
228
+ /**
229
+ * Returns a LayerInfo (opacity, hasFilter, etc.) of a LayerWms.
230
+ * It's not possible to request multiple layer with a filter, therefore
231
+ * a unique string is passed as "hasFilter" info if the layer has an active
232
+ * filter on it.
233
+ * @returns the LayerInfo of a LayerWms.
234
+ * @private
235
+ */
236
+ getLayerWmsInfo(layerWms) {
237
+ return {
238
+ opacity: layerWms.opacity,
239
+ hasFilter: layerWms.hasFilter ? layerWms.treeItemId : undefined,
240
+ hasTimeRestriction: layerWms.timeRestriction,
241
+ swip: layerWms.swiped !== 'no'
242
+ };
148
243
  }
149
- layerIsIndependentLayer(layerWms) {
150
- return layerWms.treeItemId in this.independentLayers;
244
+ /**
245
+ * @returns {boolean} Returns true if the two LayerInfo objects are identical; otherwise, false.
246
+ */
247
+ isLayerInfoIdentical(layerInfo1, layerInfo2) {
248
+ return (layerInfo1.opacity === layerInfo2.opacity &&
249
+ layerInfo1.hasFilter === layerInfo2.hasFilter &&
250
+ layerInfo1.hasTimeRestriction === layerInfo2.hasTimeRestriction &&
251
+ layerInfo1.swip === layerInfo2.swip);
151
252
  }
152
- layerIsBasemapLayer(layerWms) {
153
- return layerWms.treeItemId in this.basemapLayers;
253
+ /**
254
+ * @returns {GroupLayerInfo} Returns a GroupLayerInfo object containing the LayerInfo and zIndexes.
255
+ * @private
256
+ */
257
+ getGroupedLayerInfo(layerWmsArray) {
258
+ layerWmsArray.sort((a, b) => a.order - b.order);
259
+ const layerInfo = this.getLayerWmsInfo(layerWmsArray[0]);
260
+ return {
261
+ ...layerInfo,
262
+ zIndexes: layerWmsArray.map((layer) => layer.order),
263
+ names: layerWmsArray.map((layer) => layer.name)
264
+ };
154
265
  }
155
- getOLayer(layerWms) {
156
- if (layerWms.treeItemId in this.independentLayers) {
157
- return this.independentLayers[layerWms.treeItemId].olayer;
158
- }
159
- if (layerWms.treeItemId in this.basemapLayers) {
160
- return this.basemapLayers[layerWms.treeItemId].olayer;
161
- }
162
- if (this.layerInStandardLayers(layerWms)) {
163
- return this.olayer;
266
+ /**
267
+ * Deletes the group and remove the matching OL layer from the map.
268
+ * @private
269
+ */
270
+ removeGroupedLayers(groupLayerInfo) {
271
+ const pairedLayers = this.groupedLayers.get(groupLayerInfo);
272
+ const oLayer = pairedLayers?.oLayer;
273
+ if (oLayer) {
274
+ this.map.removeLayer(oLayer);
164
275
  }
165
- return null;
166
- }
167
- changeOpacity(layerWms) {
168
- this.manageLayerOptions(layerWms);
276
+ this.groupedLayers.delete(groupLayerInfo);
169
277
  }
170
- changeFilter(layerWms) {
171
- this.manageLayerOptions(layerWms);
172
- }
173
- changeTimeRestriction(layerWms) {
174
- this.manageLayerOptions(layerWms);
278
+ /**
279
+ * Creates a new group, create and add an OL layer to the map.
280
+ * @private
281
+ */
282
+ addNewGroupedLayers(layerWmsArray) {
283
+ const newGroupedLayerInfo = this.getGroupedLayerInfo(layerWmsArray);
284
+ const activeLayers = layerWmsArray.filter((l) => l.active);
285
+ if (activeLayers.length === 0) {
286
+ return;
287
+ }
288
+ this.groupedLayers.set(newGroupedLayerInfo, {
289
+ layerWmsArray: layerWmsArray,
290
+ oLayer: this.createOlLayer(activeLayers)
291
+ });
175
292
  }
176
- prepareSwipe(layerWms) {
177
- this.manageLayerOptions(layerWms);
293
+ /**
294
+ * For all provided layers, delete the group and remove the matching OL layer from the map.
295
+ * Create then new groups (and new ol layers) with all layers in the impacted groups.
296
+ * @private
297
+ */
298
+ deleteGroupsAndCallUpdateOnLayers(layerWmsArray) {
299
+ const allLayersToUpdate = new Map();
300
+ layerWmsArray.forEach((lWms) => {
301
+ allLayersToUpdate.set(lWms.treeItemId, lWms);
302
+ const groupLayerInfo = this.findExistingGroupLayerInfo([lWms]);
303
+ if (groupLayerInfo) {
304
+ const layersWmsFromGroup = this.groupedLayers.get(groupLayerInfo)?.layerWmsArray ?? [];
305
+ layersWmsFromGroup.forEach((lWmsFromGroup) => {
306
+ allLayersToUpdate.set(lWmsFromGroup.treeItemId, lWmsFromGroup);
307
+ });
308
+ this.removeGroupedLayers(groupLayerInfo);
309
+ }
310
+ });
311
+ this.updateLayerDirect(Array.from(allLayersToUpdate.values()));
178
312
  }
179
- manageLayerOptions(layerWms) {
180
- if (layerWms.activeState === 'off') {
313
+ /**
314
+ * Compare the LayerGroupInfo and the properties of the contained layer to update the OL layer.
315
+ * Then, update the LayerGroupInfo then to match again the state of the contained layers.
316
+ * @private
317
+ */
318
+ updateLayerGroup(groupLayerInfo) {
319
+ const pairedLayer = this.groupedLayers.get(groupLayerInfo);
320
+ const layerWms = pairedLayer?.layerWmsArray ?? [];
321
+ const firstLayerWms = layerWms[0];
322
+ if (!firstLayerWms || !pairedLayer) {
323
+ console.error('No layerWms found');
181
324
  return;
182
325
  }
183
- if (!this.layerExists(layerWms)) {
184
- throw new Error(`Cannot change filter for layer ${layerWms.name}: it does not exist`);
326
+ const newGroupLayerInfo = this.getGroupedLayerInfo(layerWms);
327
+ if (groupLayerInfo.opacity !== newGroupLayerInfo.opacity) {
328
+ pairedLayer.oLayer.setOpacity(newGroupLayerInfo.opacity);
185
329
  }
186
- const isBasemapLayer = layerWms.treeItemId in this.basemapLayers;
187
- const isLayerIndependent = layerWms.treeItemId in this.independentLayers;
188
- // TODO SMS: if all layers share the same opacity, then mustBeIndependent can be false even if isTransparent is true.
189
- // This will be changed in the future.
190
- const mustBeIndependent = layerWms.hasFilter || layerWms.hasTimeRestriction || layerWms.isTransparent || layerWms.swiped !== 'no';
191
- if (!isBasemapLayer) {
192
- if (isLayerIndependent && !mustBeIndependent) {
193
- const olayer = this.independentLayers[layerWms.treeItemId].olayer;
194
- // We delete the layer from the transparent layers
195
- delete this.independentLayers[layerWms.treeItemId];
196
- this.map.removeLayer(olayer);
197
- // And add it to the normal layer again
198
- this.addLayerInternal(layerWms);
199
- }
200
- else if (!isLayerIndependent && mustBeIndependent) {
201
- this.makeLayerIndependent(layerWms);
202
- }
330
+ else if (groupLayerInfo.zIndexes[0] !== newGroupLayerInfo.zIndexes[0]) {
331
+ pairedLayer.oLayer.setZIndex(newGroupLayerInfo.zIndexes[0]);
203
332
  }
204
- const olayer = this.getOLayer(layerWms);
205
- if (!olayer) {
206
- throw new Exception('The layer must exist at this state!');
333
+ else {
334
+ // Redo the group to be sure to handle the update.
335
+ this.removeGroupedLayers(groupLayerInfo);
336
+ this.addNewGroupedLayers(layerWms);
207
337
  }
208
- if (layerWms.hasValidOpacity) {
209
- olayer.setOpacity(layerWms.opacity);
338
+ // Update the group key
339
+ if (this.groupedLayers.has(groupLayerInfo)) {
340
+ this.groupedLayers.delete(groupLayerInfo);
341
+ this.groupedLayers.set(newGroupLayerInfo, pairedLayer);
210
342
  }
211
- this.updateLayerFilter(layerWms, olayer);
212
- this.updateTimeRestriction(layerWms, olayer);
343
+ // Regroup same groups if needed.
344
+ this.regroupSameGroups();
213
345
  }
214
- makeLayerIndependent(layerWms) {
215
- if (layerWms.treeItemId in this.independentLayers) {
216
- // The layer is already independent. => nothing to do here.
346
+ /**
347
+ * Regroup same groups if needed.
348
+ * Iterate on every group in the order (z-index) and regroup consecutive groups sharing the same properties.
349
+ * @private
350
+ */
351
+ regroupSameGroups() {
352
+ const zIndexOrderedGroupLayers = Array.from(this.groupedLayers.keys()).sort((group1, group2) => group1.zIndexes[0] - group2.zIndexes[0]);
353
+ const layerWmsArrayToMerge = [];
354
+ const firstGroupLayerInfo = zIndexOrderedGroupLayers.shift();
355
+ if (!firstGroupLayerInfo) {
356
+ return;
217
357
  }
218
- else if (this.layerInStandardLayers(layerWms)) {
219
- // First, we remove the layer from the default layer
220
- this.removeLayer(layerWms);
221
- // Then, we create a new layer
222
- const source = this.createImageWMSSource([layerWms]);
223
- const olayer = new ImageLayer({
224
- source: source,
225
- opacity: layerWms.opacity
226
- });
227
- this.independentLayers[layerWms.treeItemId] = { layerWms: layerWms, olayer: olayer };
228
- this.map.addLayer(olayer);
358
+ const lastGroup = zIndexOrderedGroupLayers.reduce((previousGroupLayerInfo, groupLayerInfo) => {
359
+ const previousGroupWmsLayersArray = this.groupedLayers.get(previousGroupLayerInfo)?.layerWmsArray ?? [];
360
+ if (this.isLayerInfoIdentical(previousGroupLayerInfo, groupLayerInfo)) {
361
+ // Note BGE 29-10-2026: Ideally check also if the group is possible regarding the layers from another OGC server.
362
+ layerWmsArrayToMerge.push(...previousGroupWmsLayersArray);
363
+ this.removeGroupedLayers(previousGroupLayerInfo);
364
+ }
365
+ else if (layerWmsArrayToMerge.length) {
366
+ this.removeGroupedLayers(previousGroupLayerInfo);
367
+ this.addNewGroupedLayers([...layerWmsArrayToMerge, ...previousGroupWmsLayersArray]);
368
+ layerWmsArrayToMerge.length = 0;
369
+ }
370
+ return groupLayerInfo;
371
+ }, firstGroupLayerInfo);
372
+ if (layerWmsArrayToMerge.length && lastGroup) {
373
+ const lastGroupWmsLayersArray = this.groupedLayers.get(lastGroup)?.layerWmsArray ?? [];
374
+ this.removeGroupedLayers(lastGroup);
375
+ this.addNewGroupedLayers([...layerWmsArrayToMerge, ...lastGroupWmsLayersArray]);
229
376
  }
230
- else {
231
- throw new Error('A layer can be made independent only if it has already been added to the map.');
377
+ }
378
+ /**
379
+ * @returns a newly created ol layer with the right properties and options.
380
+ * @private
381
+ */
382
+ createOlLayer(layerWms) {
383
+ const oLayer = new ImageLayer();
384
+ // Set zindex for this new layer
385
+ // (The bigger the order is, the deeper in the map it should be displayed.)
386
+ // (order is the inverse of z-index)
387
+ oLayer.setZIndex(-layerWms[0].order);
388
+ this.map.addLayer(oLayer);
389
+ const source = this.createImageWMSSource(layerWms);
390
+ oLayer.setSource(source);
391
+ this.manageLayerOptions(layerWms[0], oLayer);
392
+ return oLayer;
393
+ }
394
+ manageLayerOptions(layerWms, oLayer) {
395
+ if (layerWms.hasValidOpacity) {
396
+ oLayer.setOpacity(layerWms.opacity);
232
397
  }
398
+ this.updateLayerFilter(layerWms, oLayer);
399
+ this.updateTimeRestriction(layerWms, oLayer);
400
+ }
401
+ layerIsBasemapLayer(layerWms) {
402
+ return this.basemapLayers.has(layerWms.treeItemId);
233
403
  }
234
- updateLayerFilter(layerWms, olayer) {
235
- const source = olayer.getSource();
404
+ updateLayerFilter(layerWms, oLayer) {
405
+ const source = oLayer.getSource();
236
406
  if (layerWms.hasFilter) {
237
407
  const filterStr = WmsClient.buildFilterQuery(layerWms);
238
408
  source.updateParams({ FILTER: filterStr });
@@ -246,23 +416,10 @@ export default class WmsClient {
246
416
  }
247
417
  }
248
418
  }
249
- /**
250
- * MapServer wants 1 filter per underlying WFS queryLayer, all included in parentheses
251
- * QGIS-server wants 1 filter per underlying WFS queryLayer, each in its own in parentheses
252
- *
253
- * MapServer: (<filter>...</filter><filter>...</filter>)
254
- * QGIS-server: (<filter>...</filter>)(<filter>...</filter>)
255
- */
256
- static buildFilterQuery(layerWms) {
257
- if (layerWms.ogcServer.type === 'qgisserver') {
258
- return WmsClientQgis.buildFilterQuery(layerWms);
259
- }
260
- return WmsClientMapServer.buildFilterQuery(layerWms);
261
- }
262
- updateTimeRestriction(layerWms, olayer) {
263
- const source = olayer.getSource();
419
+ updateTimeRestriction(layerWms, oLayer) {
420
+ const source = oLayer.getSource();
264
421
  if (layerWms.hasTimeRestriction) {
265
- olayer.getSource().updateParams({ TIME: layerWms.timeRestriction });
422
+ oLayer.getSource().updateParams({ TIME: layerWms.timeRestriction });
266
423
  }
267
424
  else {
268
425
  // If present, remove the time parameter
@@ -273,25 +430,56 @@ export default class WmsClient {
273
430
  }
274
431
  }
275
432
  }
433
+ createImageWMSSource(layerList) {
434
+ const url = this.ogcServer.url;
435
+ const imageType = this.ogcServer.imageType;
436
+ const orderedLayers = layerList.slice().sort((l1, l2) => {
437
+ return l2.order - l1.order;
438
+ });
439
+ const orderedLayerNames = this.getOpenLayerLayerNames(orderedLayers);
440
+ const requestedUrl = new URL(url);
441
+ const shouldExclude = this.audienceExcludedPaths.some((pattern) => new RegExp(pattern).test(requestedUrl.pathname));
442
+ const crossOrigin = this.state.oauth.audience?.includes(requestedUrl.hostname) && !shouldExclude ? 'use-credentials' : 'anonymous';
443
+ const source = new ImageWMS({
444
+ url: url,
445
+ params: {
446
+ LAYERS: orderedLayerNames,
447
+ FORMAT: imageType
448
+ },
449
+ crossOrigin: crossOrigin
450
+ });
451
+ // We intercept the event to set an error icon if the WMS query has an error.
452
+ // Otherwise, we do not see anything on the client.
453
+ source.on('imageloaderror', () => {
454
+ for (const layerWms of layerList) {
455
+ this.context.layerManager.setError(layerWms, 'Image cannot be loaded from WMS Server');
456
+ }
457
+ });
458
+ source.on('imageloadend', () => {
459
+ for (const layerWms of layerList) {
460
+ this.context.layerManager.unsetError(layerWms);
461
+ }
462
+ });
463
+ return source;
464
+ }
276
465
  selectFeatures(extent) {
277
- if (this.layers.length === 0 && !this.independentLayers) {
466
+ if (this.groupedLayers.size === 0) {
278
467
  return;
279
468
  }
280
469
  const selectionParams = [];
281
- if (this.ogcServer.wfsSupport && this.layers.length > 0) {
282
- selectionParams.push(new SelectionParam(this.ogcServer, this.layers, this.state.projection, extent, this.olayer));
283
- }
284
- else {
285
- // This will need a WMS GetFeatureInfo.
286
- // In this case multiple layers are not allowed, and we have to create 1 selection param per layer
287
- for (const layer of this.layers) {
288
- selectionParams.push(new SelectionParam(this.ogcServer, [layer], this.state.projection, extent, this.olayer));
470
+ Array.from(this.groupedLayers.values()).forEach((pairedLayers) => {
471
+ if (this.ogcServer.wfsSupport) {
472
+ selectionParams.push(new SelectionParam(this.ogcServer, pairedLayers.layerWmsArray, this.state.projection, extent, pairedLayers.oLayer));
289
473
  }
290
- }
291
- for (const key in this.independentLayers) {
292
- const indepLayer = this.independentLayers[key];
293
- selectionParams.push(new SelectionParam(this.ogcServer, [indepLayer.layerWms], this.state.projection, extent, indepLayer.olayer));
294
- }
474
+ else {
475
+ // No WFS support.
476
+ // This will need a WMS GetFeatureInfo.
477
+ // In this case multiple layers are not allowed, and we have to create 1 selection param per layer
478
+ for (const layerWms of pairedLayers.layerWmsArray) {
479
+ selectionParams.push(new SelectionParam(this.ogcServer, [layerWms], this.state.projection, extent, pairedLayers.oLayer));
480
+ }
481
+ }
482
+ });
295
483
  this.state.selection.selectionParameters.push(...selectionParams);
296
484
  }
297
485
  /**
@@ -299,11 +487,9 @@ export default class WmsClient {
299
487
  */
300
488
  selectFeaturesByQuery(query) {
301
489
  const selectionParams = [];
302
- selectionParams.push(new SelectionParam(this.ogcServer, this.layers, this.state.projection, undefined, this.olayer, query));
303
- for (const key in this.independentLayers) {
304
- const indepLayer = this.independentLayers[key];
305
- selectionParams.push(new SelectionParam(this.ogcServer, [indepLayer.layerWms], this.state.projection, undefined, indepLayer.olayer, query));
306
- }
490
+ Array.from(this.groupedLayers.values()).forEach((pairedLayers) => {
491
+ selectionParams.push(new SelectionParam(this.ogcServer, pairedLayers.layerWmsArray, this.state.projection, undefined, pairedLayers.oLayer, query));
492
+ });
307
493
  this.state.selection.selectionParameters.push(...selectionParams);
308
494
  }
309
495
  async getFeatureInfo(selectionParam) {
@@ -367,23 +553,6 @@ export default class WmsClient {
367
553
  });
368
554
  return features;
369
555
  }
370
- refreshZIndexes() {
371
- // Recalculate source for Layers
372
- if (this.layers.length > 0) {
373
- // TODO: openLayer zindex = -order of the last layer in this.layers? not consistent: to improve?
374
- for (const layerWms of this.layers) {
375
- const zindex = -layerWms.order;
376
- this.olayer.setZIndex(zindex);
377
- }
378
- const source = this.createImageWMSSource(this.layers);
379
- this.olayer.setSource(source);
380
- }
381
- // Manage independant layers
382
- for (const obj of Object.values(this.independentLayers)) {
383
- const zindex = -obj.layerWms.order;
384
- obj.olayer.setZIndex(zindex);
385
- }
386
- }
387
556
  async getWmsCapabilities(params) {
388
557
  if (this.capabilityPromise !== null) {
389
558
  return this.capabilityPromise;
@@ -414,9 +583,23 @@ export default class WmsClient {
414
583
  })();
415
584
  return this.capabilityPromise;
416
585
  }
586
+ /**
587
+ * MapServer wants 1 filter per underlying WFS queryLayer, all included in parentheses
588
+ * QGIS server wants 1 filter per underlying WFS queryLayer, each in its own in parentheses
589
+ *
590
+ * MapServer: (<filter>...</filter><filter>...</filter>)
591
+ * QGIS-server: (<filter>...</filter>)(<filter>...</filter>)
592
+ */
593
+ static buildFilterQuery(layerWms) {
594
+ if (layerWms.ogcServer.type === 'qgisserver') {
595
+ return WmsClientQgis.buildFilterQuery(layerWms);
596
+ }
597
+ return WmsClientMapServer.buildFilterQuery(layerWms);
598
+ }
417
599
  }
418
600
  export class WmsClientQgis extends WmsClient {
419
- /** QGIS-server does not filter on a WMS layer made from multiple underlying WFS queryLayers
601
+ /**
602
+ * QGIS server does not filter on a WMS layer made from multiple underlying WFS queryLayers
420
603
  * Solution: directly query the queryLayers
421
604
  */
422
605
  getOpenLayerLayerNames(layerList) {
@@ -426,8 +609,7 @@ export class WmsClientQgis extends WmsClient {
426
609
  return layerNames;
427
610
  }
428
611
  else {
429
- const layerNames = layerList.map((l) => l.layers ?? l.name);
430
- return layerNames;
612
+ return layerList.map((l) => l.layers ?? l.name);
431
613
  }
432
614
  }
433
615
  static buildFilterQuery(layerWms) {
@@ -435,8 +617,8 @@ export class WmsClientQgis extends WmsClient {
435
617
  return '';
436
618
  }
437
619
  const filter = layerWms.filter;
438
- const nbQuerylayers = layerWms.queryLayers.split(',').length;
439
- return ('(' + filter.toWmsGetMapFilter() + ')').repeat(nbQuerylayers);
620
+ const nbQueryLayers = layerWms.queryLayers.split(',').length;
621
+ return ('(' + filter.toWmsGetMapFilter() + ')').repeat(nbQueryLayers);
440
622
  }
441
623
  }
442
624
  export class WmsClientMapServer extends WmsClient {
@@ -448,8 +630,8 @@ export class WmsClientMapServer extends WmsClient {
448
630
  return '';
449
631
  }
450
632
  const filter = layerWms.filter;
451
- const nbQuerylayers = layerWms.queryLayers.split(',').length;
452
- return '(' + filter.toWmsGetMapFilter().repeat(nbQuerylayers) + ')';
633
+ const nbQueryLayers = layerWms.queryLayers.split(',').length;
634
+ return '(' + filter.toWmsGetMapFilter().repeat(nbQueryLayers) + ')';
453
635
  }
454
636
  }
455
637
  export const WmsClientDefault = WmsClientQgis;