Skip to content

Commit

Permalink
Javascript - Add API to retrieve invalid layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Feb 18, 2025
1 parent c529c94 commit 3f870bc
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 34 deletions.
17 changes: 16 additions & 1 deletion assets/src/modules/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Config {
this._options = null;
this._layers = null;
this._layerTree = null;
this._invalid_layers = null;
this._baselayers = null;
this._layersOrder = null;
this._hasMetadata = true;
Expand Down Expand Up @@ -182,11 +183,25 @@ export class Config {
*/
get layerTree() {
if (this._layerTree == null) {
this._layerTree = buildLayerTreeConfig(this._theWmsCapabilities.Capability.Layer, this.layers);
this._invalid_layers = [];
this._layerTree = buildLayerTreeConfig(
this._theWmsCapabilities.Capability.Layer, this.layers, this._invalid_layers);
}
return this._layerTree;
}

/**
* List of invalid layers, not found in the Lizmap configuration file, but found in the WMS GetCapabilities.
* Note, the getter layerTree must be called before at least one time, otherwise, a null is returned.
* @type {String[]}
*/
get invalidLayersNotFoundInCfg() {
if (this._layerTree == null) {
return null;
}
return this._invalid_layers;
}

/**
* Config base layers
* @type {BaseLayersConfig}
Expand Down
6 changes: 5 additions & 1 deletion assets/src/modules/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export class State extends EventDispatcher {
*/
get layersAndGroupsCollection() {
if (this._collection == null) {
this._collection = new LayersAndGroupsCollection(this._initialConfig.layerTree, this._initialConfig.layersOrder, this._initialConfig.options.hideGroupCheckbox);
this._collection = new LayersAndGroupsCollection(
this._initialConfig.layerTree,
this._initialConfig.layersOrder,
this._initialConfig.options.hideGroupCheckbox,
);
// Dispatch events from groups and layers
this._collection.addListener(this.dispatch.bind(this), 'group.visibility.changed');
this._collection.addListener(this.dispatch.bind(this), 'group.opacity.changed');
Expand Down
34 changes: 20 additions & 14 deletions assets/src/modules/config/LayerTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AttributionConfig } from './Attribution.js';
import { LayerConfig, LayersConfig } from './Layer.js';

/**
* Class representing a wMS layer Geographic Bounding Box
* Class representing a WMS layer Geographic Bounding Box
* @class
* @augments Extent
*/
Expand Down Expand Up @@ -403,29 +403,34 @@ export class LayerTreeGroupConfig extends LayerTreeItemConfig {
/**
* Function to build layer tree items config based on WMS capabilities
* @function
* @param {object} wmsCapaLayerGroup - the wms layer capabilities
* @param {object} wmsCapaLayerGroup - the WMS layer capabilities
* @param {LayersConfig} layersCfg - the lizmap layers config instance
* @param {number} level - the wms layer level
* @returns {LayerTreeItemConfig[]} the layer tree items of the wms layer
* @param {number} level - the WMS layer level
* @param {Array[string]} invalid - List of invalid WMS layer name

Check warning on line 410 in assets/src/modules/config/LayerTree.js

View workflow job for this annotation

GitHub Actions / ESLint 🇯‌🇸‌

Syntax error in type: Array[LayerTreeItemConfig[], String[]]
* @returns {Array[LayerTreeItemConfig[], String[]]} The layer tree items of the WMS layer, and the list of invalid layer names
*/
function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level) {
function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level, invalid) {
let items = [];

if (!wmsCapaLayerGroup.hasOwnProperty('Layer')) {
return items;
}

for(const wmsCapaLayer of wmsCapaLayerGroup.Layer) {
const wmsName = wmsCapaLayer.Name;
const cfg = layersCfg.getLayerConfigByWmsName(wmsName);
if (cfg == null) {
console.log('The WMS layer name `'+ wmsName +'` is unknown!');
invalid.push(wmsName);
continue;
}
if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length != 0) {
const groupItems = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1);

if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length !== 0) {
const groupItems = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1, invalid);
items.push(new LayerTreeGroupConfig(cfg.name, level+1, groupItems, wmsCapaLayer, cfg));
} else {
// avoid to add the baseLayers group to the map if it doesn't contain any layer.
if(wmsName.toLowerCase() != 'baselayers') {
if(wmsName.toLowerCase() !== 'baselayers') {
items.push(new LayerTreeLayerConfig(cfg.name, level+1, wmsCapaLayer, cfg));
}

Expand All @@ -437,11 +442,12 @@ function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level) {
/**
* Function to build the root layer tree config based on WMS capabilities
* @function
* @param {object} wmsCapaLayerRoot - the wms root layer capabilities
* @param {object} wmsCapaLayerRoot - the WMS root layer capabilities
* @param {LayersConfig} layersCfg - the lizmap layers config instance
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities
* @param {Array[string]} invalid - List of invalid WMS layer name
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities, and the list of invalid layer names.
*/
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg) {
let items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
return new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot);
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg, invalid = []) {
const items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
return [new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot), invalid];
}
18 changes: 12 additions & 6 deletions tests/js-units/node/config/baselayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,9 @@ describe('BaseLayersConfig', function () {
config.layers[blName] = blGroupCfg;

const layers = new LayersConfig(config.layers);
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -665,8 +666,9 @@ describe('BaseLayersConfig', function () {
expect(config).to.not.be.undefined

const layers = new LayersConfig(config.layers);
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -870,8 +872,10 @@ describe('BaseLayersConfig', function () {
});
}

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -1016,8 +1020,10 @@ describe('BaseLayersConfig', function () {
});
}

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down
10 changes: 8 additions & 2 deletions tests/js-units/node/config/layerTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ describe('buildLayerTreeConfig', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -301,7 +304,10 @@ describe('buildLayerTreeConfig', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down
4 changes: 3 additions & 1 deletion tests/js-units/node/config/layersOrder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ describe('buildLayersOrder', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
const layersOrder = buildLayersOrder(config, root);
expect(layersOrder).to.have.ordered.members([
"points_of_interest",
Expand Down
9 changes: 7 additions & 2 deletions tests/js-units/node/state/baselayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function getBaseLayersState(name) {
expect(config).to.not.be.undefined

const layers = new LayersConfig(config.layers);
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);
expect(invalid).to.have.length(0);

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

const layers = new LayersConfig(config.layers);
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);

const blGroup = rootCfg.children[6];
expect(blGroup).to.be.instanceOf(LayerTreeGroupConfig)
Expand Down
5 changes: 4 additions & 1 deletion tests/js-units/node/state/externallayertree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function getRootLayerTreeGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

const layersOrder = buildLayersOrder(config, rootCfg);
Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/state/externalmaplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ function getRootMapGroupState(name) {

const layers = new LayersConfig(config.layers);

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

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down
15 changes: 12 additions & 3 deletions tests/js-units/node/state/layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function getRootLayerGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

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

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

const layersOrder = buildLayersOrder(config, rootCfg);
Expand Down Expand Up @@ -1670,7 +1676,10 @@ describe('LayersAndGroupsCollection', function () {
"group-without-children"
)

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

// `group-without-children` has a layerTree config and it is a layer not a group
Expand Down
5 changes: 4 additions & 1 deletion tests/js-units/node/state/layerTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function getRootLayerTreeGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

const layersOrder = buildLayersOrder(config, rootCfg);
Expand Down
5 changes: 4 additions & 1 deletion tests/js-units/node/state/maplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function getRootMapGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
let invalid = [];
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers, invalid);

expect(invalid).to.have.length(0);;
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)

const layersOrder = buildLayersOrder(config, rootCfg);
Expand Down

0 comments on commit 3f870bc

Please sign in to comment.