diff --git a/src/ol/source/ImageWMS.js b/src/ol/source/ImageWMS.js index 6a1beac78b..a32b9b061e 100644 --- a/src/ol/source/ImageWMS.js +++ b/src/ol/source/ImageWMS.js @@ -197,15 +197,15 @@ class ImageWMS extends ImageSource { * * @param {number} [resolution] Resolution. If set to undefined, `SCALE` * will not be calculated and included in URL. - * @param {Object} [params] GetLegendGraphic params. Default `FORMAT` is - * `image/png`. `VERSION` should not be specified here. + * @param {Object} [params] GetLegendGraphic params. If `LAYER` is set, the + * request is generated for this wms layer, else it will try to use the + * configured wms layer. Default `FORMAT` is `image/png`. + * `VERSION` should not be specified here. * @return {string|undefined} GetLegendGraphic URL. * @api */ getLegendUrl(resolution, params) { - const layers = this.params_.LAYERS; - const isSingleLayer = !Array.isArray(layers) || this.params_['LAYERS'].length === 1; - if (this.url_ === undefined || !isSingleLayer) { + if (this.url_ === undefined) { return undefined; } @@ -213,10 +213,18 @@ class ImageWMS extends ImageSource { 'SERVICE': 'WMS', 'VERSION': DEFAULT_WMS_VERSION, 'REQUEST': 'GetLegendGraphic', - 'FORMAT': 'image/png', - 'LAYER': layers + 'FORMAT': 'image/png' }; + if (params === undefined || params['LAYER'] === undefined) { + const layers = this.params_.LAYERS; + const isSingleLayer = !Array.isArray(layers) || layers.length === 1; + if (!isSingleLayer) { + return undefined; + } + baseParams['LAYER'] = layers; + } + if (resolution !== undefined) { const mpu = this.getProjection() ? this.getProjection().getMetersPerUnit() : 1; const dpi = 25.4 / 0.28; diff --git a/src/ol/source/TileWMS.js b/src/ol/source/TileWMS.js index bbe833592a..ee39d13e1d 100644 --- a/src/ol/source/TileWMS.js +++ b/src/ol/source/TileWMS.js @@ -219,15 +219,15 @@ class TileWMS extends TileImage { * * @param {number} [resolution] Resolution. If set to undefined, `SCALE` * will not be calculated and included in URL. - * @param {Object} [params] GetLegendGraphic params. Default `FORMAT` is - * `image/png`. `VERSION` should not be specified here. + * @param {Object} [params] GetLegendGraphic params. If `LAYER` is set, the + * request is generated for this wms layer, else it will try to use the + * configured wms layer. Default `FORMAT` is `image/png`. + * `VERSION` should not be specified here. * @return {string|undefined} GetLegendGraphic URL. * @api */ getLegendUrl(resolution, params) { - const layers = this.params_.LAYERS; - const isSingleLayer = !Array.isArray(layers) || this.params_['LAYERS'].length === 1; - if (this.urls[0] === undefined || !isSingleLayer) { + if (this.urls[0] === undefined) { return undefined; } @@ -235,10 +235,18 @@ class TileWMS extends TileImage { 'SERVICE': 'WMS', 'VERSION': DEFAULT_WMS_VERSION, 'REQUEST': 'GetLegendGraphic', - 'FORMAT': 'image/png', - 'LAYER': layers + 'FORMAT': 'image/png' }; + if (params === undefined || params['LAYER'] === undefined) { + const layers = this.params_.LAYERS; + const isSingleLayer = !Array.isArray(layers) || layers.length === 1; + if (!isSingleLayer) { + return undefined; + } + baseParams['LAYER'] = layers; + } + if (resolution !== undefined) { const mpu = this.getProjection() ? this.getProjection().getMetersPerUnit() : 1; const dpi = 25.4 / 0.28; diff --git a/test/spec/ol/source/imagewms.test.js b/test/spec/ol/source/imagewms.test.js index 1c410aaea7..94e4881dfb 100644 --- a/test/spec/ol/source/imagewms.test.js +++ b/test/spec/ol/source/imagewms.test.js @@ -368,7 +368,8 @@ describe('ol.source.ImageWMS', function() { WIDTH: 'WIDTH_VALUE', HEIGHT: 'HEIGHT_VALUE', EXCEPTIONS: 'EXCEPTIONS_VALUE', - LANGUAGE: 'LANGUAGE_VALUE' + LANGUAGE: 'LANGUAGE_VALUE', + LAYER: 'LAYER_VALUE' }); const uri = new URL(url); expect(uri.protocol).to.be('http:'); @@ -376,7 +377,7 @@ describe('ol.source.ImageWMS', function() { expect(uri.pathname).to.be('/wms'); const queryData = uri.searchParams; expect(queryData.get('FORMAT')).to.be('FORMAT_VALUE'); - expect(queryData.get('LAYER')).to.be('layer'); + expect(queryData.get('LAYER')).to.be('LAYER_VALUE'); expect(queryData.get('REQUEST')).to.be('GetLegendGraphic'); expect(queryData.get('SERVICE')).to.be('WMS'); expect(queryData.get('VERSION')).to.be('1.3.0'); diff --git a/test/spec/ol/source/tilewms.test.js b/test/spec/ol/source/tilewms.test.js index 48fb8eeb0e..20978a9dc7 100644 --- a/test/spec/ol/source/tilewms.test.js +++ b/test/spec/ol/source/tilewms.test.js @@ -337,7 +337,8 @@ describe('ol.source.TileWMS', function() { WIDTH: 'WIDTH_VALUE', HEIGHT: 'HEIGHT_VALUE', EXCEPTIONS: 'EXCEPTIONS_VALUE', - LANGUAGE: 'LANGUAGE_VALUE' + LANGUAGE: 'LANGUAGE_VALUE', + LAYER: 'LAYER_VALUE' }); const uri = new URL(url); expect(uri.protocol).to.be('http:'); @@ -345,7 +346,7 @@ describe('ol.source.TileWMS', function() { expect(uri.pathname).to.be('/wms'); const queryData = uri.searchParams; expect(queryData.get('FORMAT')).to.be('FORMAT_VALUE'); - expect(queryData.get('LAYER')).to.be('layer'); + expect(queryData.get('LAYER')).to.be('LAYER_VALUE'); expect(queryData.get('REQUEST')).to.be('GetLegendGraphic'); expect(queryData.get('SERVICE')).to.be('WMS'); expect(queryData.get('VERSION')).to.be('1.3.0');