From c0000e745e87a2ffc9f16af29cad7578d5622651 Mon Sep 17 00:00:00 2001 From: simonseyock Date: Mon, 8 Jul 2019 15:14:06 +0200 Subject: [PATCH] make resolution in getGetLegendGraphicUrl optional --- src/ol/source/ImageWMS.js | 19 +++++++++++-------- src/ol/source/TileWMS.js | 22 ++++++++++++---------- test/spec/ol/source/imagewms.test.js | 10 +++++++++- test/spec/ol/source/tilewms.test.js | 8 ++++++++ 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/ol/source/ImageWMS.js b/src/ol/source/ImageWMS.js index 60e2fcaafc..3b85bc289b 100644 --- a/src/ol/source/ImageWMS.js +++ b/src/ol/source/ImageWMS.js @@ -194,7 +194,8 @@ class ImageWMS extends ImageSource { /** * Return the GetLegendGraphic URL for the passed resolution. * Return `undefined` if the GetLegendGraphic URL cannot be constructed. - * @param {number} resolution Resolution. + * @param {!number} resolution Resolution. If set to undefined `SCALE` + * will not be calculated. * @param {!Object} params GetLegendGraphic params. Default `FORMAT` is * `image/png`. `VERSION` should not be specified here. * @return {string|undefined} GetLegendGraphic URL. @@ -207,19 +208,21 @@ class ImageWMS extends ImageSource { return undefined; } - const mpu = this.getProjection() ? this.getProjection().getMetersPerUnit() : 1; - const dpi = 25.4 / 0.28; - const inchesPerMeter = 39.37; - const scale = resolution * mpu * inchesPerMeter * dpi; - const baseParams = { 'SERVICE': 'WMS', 'VERSION': DEFAULT_WMS_VERSION, 'REQUEST': 'GetLegendGraphic', 'FORMAT': 'image/png', - 'LAYER': layers, - 'SCALE': scale + 'LAYER': layers }; + + if (resolution !== undefined) { + const mpu = this.getProjection() ? this.getProjection().getMetersPerUnit() : 1; + const dpi = 25.4 / 0.28; + const inchesPerMeter = 39.37; + baseParams['SCALE'] = resolution * mpu * inchesPerMeter * dpi; + } + assign(baseParams, params); return appendParams(/** @type {string} */ (this.url_), baseParams); diff --git a/src/ol/source/TileWMS.js b/src/ol/source/TileWMS.js index 838b5ed3bb..66c29a2bfc 100644 --- a/src/ol/source/TileWMS.js +++ b/src/ol/source/TileWMS.js @@ -8,7 +8,7 @@ import {assert} from '../asserts.js'; import {buffer, createEmpty} from '../extent.js'; import {assign} from '../obj.js'; import {modulo} from '../math.js'; -import {get as getProjection, transform, transformExtent, METERS_PER_UNIT} from '../proj.js'; +import {get as getProjection, transform, transformExtent} from '../proj.js'; import {calculateSourceResolution} from '../reproj.js'; import {toSize, buffer as bufferSize, scale as scaleSize} from '../size.js'; import TileImage from './TileImage.js'; @@ -214,7 +214,8 @@ class TileWMS extends TileImage { /** * Return the GetLegendGraphic URL for the passed resolution. * Return `undefined` if the GetLegendGraphic URL cannot be constructed. - * @param {number} resolution Resolution. + * @param {!number} resolution Resolution. If set to undefined `SCALE` + * will not be calculated. * @param {!Object} params GetLegendGraphic params. Default `FORMAT` is * `image/png`. `VERSION` should not be specified here. * @return {string|undefined} GetLegendGraphic URL. @@ -227,20 +228,21 @@ class TileWMS extends TileImage { return undefined; } - const units = this.getProjection() && this.getProjection().getUnits(); - const dpi = 25.4 / 0.28; - const mpu = METERS_PER_UNIT[units || 'm']; - const inchesPerMeter = 39.37; - const scale = resolution * mpu * inchesPerMeter * dpi; - const baseParams = { 'SERVICE': 'WMS', 'VERSION': DEFAULT_WMS_VERSION, 'REQUEST': 'GetLegendGraphic', 'FORMAT': 'image/png', - 'LAYER': this.params_['LAYERS'], - 'SCALE': scale + 'LAYER': layers }; + + if (resolution !== undefined) { + const mpu = this.getProjection() ? this.getProjection().getMetersPerUnit() : 1; + const dpi = 25.4 / 0.28; + const inchesPerMeter = 39.37; + baseParams['SCALE'] = resolution * mpu * inchesPerMeter * dpi; + } + assign(baseParams, params); return appendParams(/** @type {string} */ (this.urls[0]), baseParams); diff --git a/test/spec/ol/source/imagewms.test.js b/test/spec/ol/source/imagewms.test.js index e69c1e9aaf..44f0a95dab 100644 --- a/test/spec/ol/source/imagewms.test.js +++ b/test/spec/ol/source/imagewms.test.js @@ -332,7 +332,7 @@ describe('ol.source.ImageWMS', function() { describe('#getGetLegendGraphicUrl', function() { - it('returns the getLegenGraphic url as expected', function() { + it('returns the getLegendGraphic url as expected', function() { const source = new ImageWMS(options); const url = source.getGetLegendGraphicUrl(resolution); const uri = new URL(url); @@ -348,6 +348,14 @@ describe('ol.source.ImageWMS', function() { expect(queryData.get('SCALE')).to.be('357.14214285714274'); }); + it('does not include SCALE if no resolution was provided', function() { + const source = new ImageWMS(options); + const url = source.getGetLegendGraphicUrl(); + const uri = new URL(url); + const queryData = uri.searchParams; + expect(queryData.get('SCALE')).to.be(null); + }); + it('adds additional params as expected', function() { const source = new ImageWMS(options); const url = source.getGetLegendGraphicUrl(resolution, { diff --git a/test/spec/ol/source/tilewms.test.js b/test/spec/ol/source/tilewms.test.js index 3d24e7deaa..4ff73bdf46 100644 --- a/test/spec/ol/source/tilewms.test.js +++ b/test/spec/ol/source/tilewms.test.js @@ -317,6 +317,14 @@ describe('ol.source.TileWMS', function() { expect(queryData.get('SCALE')).to.be('357.14214285714274'); }); + it('does not include SCALE if no resolution was provided', function() { + const source = new TileWMS(options); + const url = source.getGetLegendGraphicUrl(); + const uri = new URL(url); + const queryData = uri.searchParams; + expect(queryData.get('SCALE')).to.be(null); + }); + it('adds additional params as expected', function() { const source = new TileWMS(options); const url = source.getGetLegendGraphicUrl(0.1, {