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 d4c97e4
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 37 deletions.
19 changes: 18 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._invalidLayers = null;
this._baselayers = null;
this._layersOrder = null;
this._hasMetadata = true;
Expand Down Expand Up @@ -182,11 +183,27 @@ export class Config {
*/
get layerTree() {
if (this._layerTree == null) {
this._layerTree = buildLayerTreeConfig(this._theWmsCapabilities.Capability.Layer, this.layers);
this._invalidLayers = [];
this._layerTree = buildLayerTreeConfig(
this._theWmsCapabilities.Capability.Layer,
this.layers,
this._invalidLayers,
);
}
return this._layerTree;
}

/**
* List of invalid layers, not found in the Lizmap configuration file, but found in the WMS GetCapabilities.
* @type {string[]}
*/
get invalidLayersNotFoundInCfg() {
if (this._invalidLayers == null) {
this.layerTree;
}
return this._invalidLayers;
}

/**
* 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
31 changes: 18 additions & 13 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,33 @@ 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 {string[]} invalid - List of invalid WMS layer name
* @returns {LayerTreeItemConfig[]} - The layer tree items of the WMS layer
*/
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 +441,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 {string[]} invalid - List of invalid WMS layer name
* @returns {LayerTreeGroupConfig} - The root layer tree config based on WMS capabilities
*/
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg) {
let items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg, invalid = []) {
let items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0, invalid);
return new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot);
}
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
22 changes: 16 additions & 6 deletions tests/js-units/node/state/baselayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ import { BaseLayerState, EmptyBaseLayerState, BaseLayersState } from 'assets/src
* - name +'-config.json': the Lizmap config send by lizmap web client
*
* @param {String} name - The project name
* @param {string[]} expectedInvalidLayers - Expected list of invalid layers
*
* @return {BaseLayersState}
**/
function getBaseLayersState(name) {
const capabilities = JSON.parse(readFileSync('./tests/js-units/data/'+ name +'-capabilities.json', 'utf8'));
function getBaseLayersState(name, expectedInvalidLayers = []) {
console.log(`Current test : ${name}`);
const capabilities = JSON.parse(readFileSync(`./tests/js-units/data/${name}-capabilities.json`, 'utf8'));
expect(capabilities).to.not.be.undefined
expect(capabilities.Capability).to.not.be.undefined
const config = JSON.parse(readFileSync('./tests/js-units/data/'+ name +'-config.json', 'utf8'));
const config = JSON.parse(readFileSync(`./tests/js-units/data/${name}-config.json`, 'utf8'));

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(expectedInvalidLayers.length);
expect(invalid).to.deep.eq(expectedInvalidLayers);

let baseLayerTreeItem = null;
for (const layerTreeItem of rootCfg.getChildren()) {
Expand Down Expand Up @@ -158,7 +164,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 Expand Up @@ -296,7 +305,8 @@ describe('BaseLayersState', function () {
});

it('Tiled baselayer', function () {
const baseLayers = getBaseLayersState('tiled_baselayers')
// This project has OpenStreetMap in its GetCapabilities but not in CFG file
const baseLayers = getBaseLayersState('tiled_baselayers', ['OpenStreetMap'])
expect(baseLayers.selectedBaseLayerName).to.be.eq('wms_baselayer')
expect(baseLayers.selectedBaseLayer).to.not.be.undefined
expect(baseLayers.selectedBaseLayer.name).to.be.eq('wms_baselayer')
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
4 changes: 3 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,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(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 d4c97e4

Please sign in to comment.