Skip to content

Commit 3f870bc

Browse files
committed
Javascript - Add API to retrieve invalid layers
1 parent c529c94 commit 3f870bc

File tree

12 files changed

+97
-34
lines changed

12 files changed

+97
-34
lines changed

assets/src/modules/Config.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class Config {
6767
this._options = null;
6868
this._layers = null;
6969
this._layerTree = null;
70+
this._invalid_layers = null;
7071
this._baselayers = null;
7172
this._layersOrder = null;
7273
this._hasMetadata = true;
@@ -182,11 +183,25 @@ export class Config {
182183
*/
183184
get layerTree() {
184185
if (this._layerTree == null) {
185-
this._layerTree = buildLayerTreeConfig(this._theWmsCapabilities.Capability.Layer, this.layers);
186+
this._invalid_layers = [];
187+
this._layerTree = buildLayerTreeConfig(
188+
this._theWmsCapabilities.Capability.Layer, this.layers, this._invalid_layers);
186189
}
187190
return this._layerTree;
188191
}
189192

193+
/**
194+
* List of invalid layers, not found in the Lizmap configuration file, but found in the WMS GetCapabilities.
195+
* Note, the getter layerTree must be called before at least one time, otherwise, a null is returned.
196+
* @type {String[]}
197+
*/
198+
get invalidLayersNotFoundInCfg() {
199+
if (this._layerTree == null) {
200+
return null;
201+
}
202+
return this._invalid_layers;
203+
}
204+
190205
/**
191206
* Config base layers
192207
* @type {BaseLayersConfig}

assets/src/modules/State.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ export class State extends EventDispatcher {
6161
*/
6262
get layersAndGroupsCollection() {
6363
if (this._collection == null) {
64-
this._collection = new LayersAndGroupsCollection(this._initialConfig.layerTree, this._initialConfig.layersOrder, this._initialConfig.options.hideGroupCheckbox);
64+
this._collection = new LayersAndGroupsCollection(
65+
this._initialConfig.layerTree,
66+
this._initialConfig.layersOrder,
67+
this._initialConfig.options.hideGroupCheckbox,
68+
);
6569
// Dispatch events from groups and layers
6670
this._collection.addListener(this.dispatch.bind(this), 'group.visibility.changed');
6771
this._collection.addListener(this.dispatch.bind(this), 'group.opacity.changed');

assets/src/modules/config/LayerTree.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { AttributionConfig } from './Attribution.js';
1212
import { LayerConfig, LayersConfig } from './Layer.js';
1313

1414
/**
15-
* Class representing a wMS layer Geographic Bounding Box
15+
* Class representing a WMS layer Geographic Bounding Box
1616
* @class
1717
* @augments Extent
1818
*/
@@ -403,29 +403,34 @@ export class LayerTreeGroupConfig extends LayerTreeItemConfig {
403403
/**
404404
* Function to build layer tree items config based on WMS capabilities
405405
* @function
406-
* @param {object} wmsCapaLayerGroup - the wms layer capabilities
406+
* @param {object} wmsCapaLayerGroup - the WMS layer capabilities
407407
* @param {LayersConfig} layersCfg - the lizmap layers config instance
408-
* @param {number} level - the wms layer level
409-
* @returns {LayerTreeItemConfig[]} the layer tree items of the wms layer
408+
* @param {number} level - the WMS layer level
409+
* @param {Array[string]} invalid - List of invalid WMS layer name
410+
411+
* @returns {Array[LayerTreeItemConfig[], String[]]} The layer tree items of the WMS layer, and the list of invalid layer names
410412
*/
411-
function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level) {
413+
function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level, invalid) {
412414
let items = [];
415+
413416
if (!wmsCapaLayerGroup.hasOwnProperty('Layer')) {
414417
return items;
415418
}
419+
416420
for(const wmsCapaLayer of wmsCapaLayerGroup.Layer) {
417421
const wmsName = wmsCapaLayer.Name;
418422
const cfg = layersCfg.getLayerConfigByWmsName(wmsName);
419423
if (cfg == null) {
420-
console.log('The WMS layer name `'+ wmsName +'` is unknown!');
424+
invalid.push(wmsName);
421425
continue;
422426
}
423-
if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length != 0) {
424-
const groupItems = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1);
427+
428+
if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length !== 0) {
429+
const groupItems = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1, invalid);
425430
items.push(new LayerTreeGroupConfig(cfg.name, level+1, groupItems, wmsCapaLayer, cfg));
426431
} else {
427432
// avoid to add the baseLayers group to the map if it doesn't contain any layer.
428-
if(wmsName.toLowerCase() != 'baselayers') {
433+
if(wmsName.toLowerCase() !== 'baselayers') {
429434
items.push(new LayerTreeLayerConfig(cfg.name, level+1, wmsCapaLayer, cfg));
430435
}
431436

@@ -437,11 +442,12 @@ function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level) {
437442
/**
438443
* Function to build the root layer tree config based on WMS capabilities
439444
* @function
440-
* @param {object} wmsCapaLayerRoot - the wms root layer capabilities
445+
* @param {object} wmsCapaLayerRoot - the WMS root layer capabilities
441446
* @param {LayersConfig} layersCfg - the lizmap layers config instance
442-
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities
447+
* @param {Array[string]} invalid - List of invalid WMS layer name
448+
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities, and the list of invalid layer names.
443449
*/
444-
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg) {
445-
let items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
446-
return new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot);
450+
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg, invalid = []) {
451+
const items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
452+
return [new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot), invalid];
447453
}

tests/js-units/node/config/baselayer.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,9 @@ describe('BaseLayersConfig', function () {
619619
config.layers[blName] = blGroupCfg;
620620

621621
const layers = new LayersConfig(config.layers);
622-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
623-
622+
let invalid = [];
623+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
624+
expect(invalid).to.have.length(0);
624625
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
625626
expect(root.name).to.be.eq('root')
626627
expect(root.type).to.be.eq('group')
@@ -665,8 +666,9 @@ describe('BaseLayersConfig', function () {
665666
expect(config).to.not.be.undefined
666667

667668
const layers = new LayersConfig(config.layers);
668-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
669-
669+
let invalid = [];
670+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
671+
expect(invalid).to.have.length(0);
670672
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
671673
expect(root.name).to.be.eq('root')
672674
expect(root.type).to.be.eq('group')
@@ -870,8 +872,10 @@ describe('BaseLayersConfig', function () {
870872
});
871873
}
872874

873-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
875+
let invalid = [];
876+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
874877

878+
expect(invalid).to.have.length(0);
875879
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
876880
expect(root.name).to.be.eq('root')
877881
expect(root.type).to.be.eq('group')
@@ -1016,8 +1020,10 @@ describe('BaseLayersConfig', function () {
10161020
});
10171021
}
10181022

1019-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
1023+
let invalid = [];
1024+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
10201025

1026+
expect(invalid).to.have.length(0);
10211027
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
10221028
expect(root.name).to.be.eq('root')
10231029
expect(root.type).to.be.eq('group')

tests/js-units/node/config/layerTree.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ describe('buildLayerTreeConfig', function () {
176176

177177
const layers = new LayersConfig(config.layers);
178178

179-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
179+
let invalid = [];
180+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
181+
182+
expect(invalid).to.have.length(0);
180183
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
181184
expect(root.name).to.be.eq('root')
182185
expect(root.type).to.be.eq('group')
@@ -301,7 +304,10 @@ describe('buildLayerTreeConfig', function () {
301304

302305
const layers = new LayersConfig(config.layers);
303306

304-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
307+
let invalid = [];
308+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
309+
310+
expect(invalid).to.have.length(0);
305311
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
306312
expect(root.name).to.be.eq('root')
307313
expect(root.type).to.be.eq('group')

tests/js-units/node/config/layersOrder.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ describe('buildLayersOrder', function () {
5252

5353
const layers = new LayersConfig(config.layers);
5454

55-
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
55+
let invalid = [];
56+
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
5657

58+
expect(invalid).to.have.length(0);
5759
const layersOrder = buildLayersOrder(config, root);
5860
expect(layersOrder).to.have.ordered.members([
5961
"points_of_interest",

tests/js-units/node/state/baselayer.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function getBaseLayersState(name) {
3030
expect(config).to.not.be.undefined
3131

3232
const layers = new LayersConfig(config.layers);
33-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
33+
let invalid = [];
34+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
35+
expect(invalid).to.have.length(0);
3436

3537
let baseLayerTreeItem = null;
3638
for (const layerTreeItem of rootCfg.getChildren()) {
@@ -158,7 +160,10 @@ describe('BaseLayersState', function () {
158160
config.layers[blName] = blGroupCfg;
159161

160162
const layers = new LayersConfig(config.layers);
161-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
163+
let invalid = [];
164+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
165+
166+
expect(invalid).to.have.length(0);
162167

163168
const blGroup = rootCfg.children[6];
164169
expect(blGroup).to.be.instanceOf(LayerTreeGroupConfig)

tests/js-units/node/state/externallayertree.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function getRootLayerTreeGroupState(name) {
3535

3636
const layers = new LayersConfig(config.layers);
3737

38-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
38+
let invalid = [];
39+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
40+
41+
expect(invalid).to.have.length(0);
3942
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
4043

4144
const layersOrder = buildLayersOrder(config, rootCfg);

tests/js-units/node/state/externalmaplayer.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function getRootMapGroupState(name) {
3434

3535
const layers = new LayersConfig(config.layers);
3636

37-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
37+
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
3838
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
39+
expect(invalid).to.have.length(0);
3940

4041
const layersOrder = buildLayersOrder(config, rootCfg);
4142

tests/js-units/node/state/layer.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ function getRootLayerGroupState(name) {
3232

3333
const layers = new LayersConfig(config.layers);
3434

35-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
35+
let invalid = [];
36+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
37+
38+
expect(invalid).to.have.length(0);
3639
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
3740

3841
const layersOrder = buildLayersOrder(config, rootCfg);
@@ -63,7 +66,10 @@ function getLayersAndGroupsCollection(name) {
6366

6467
const layers = new LayersConfig(config.layers);
6568

66-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
69+
let invalid = [];
70+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
71+
72+
expect(invalid).to.have.length(0);
6773
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
6874

6975
const layersOrder = buildLayersOrder(config, rootCfg);
@@ -1670,7 +1676,10 @@ describe('LayersAndGroupsCollection', function () {
16701676
"group-without-children"
16711677
)
16721678

1673-
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
1679+
let invalid = [];
1680+
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
1681+
1682+
expect(invalid).to.have.length(0);
16741683
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
16751684

16761685
// `group-without-children` has a layerTree config and it is a layer not a group

0 commit comments

Comments
 (0)