Introduces getGetLegendGraphic method

Adds the `getGetLegendGraphic` method to `ImageWMS` and `TileWMS` source.
Also adds an example and corresponding tests.
This commit is contained in:
Kai Volland
2019-04-04 12:06:03 +02:00
committed by simonseyock
parent 7e3ed92ba6
commit f493f01bd0
6 changed files with 247 additions and 2 deletions

View File

@@ -191,6 +191,40 @@ class ImageWMS extends ImageSource {
1, sourceProjectionObj || projectionObj, baseParams);
}
/**
* Return the GetLegendGraphic URL for the passed resolution.
* Return `undefined` if the GetLegendGraphic URL cannot be constructed.
* @param {number} resolution Resolution.
* @param {!Object} params GetLegendGraphic params. Default `FORMAT` is
* `image/png`. `VERSION` should not be specified here.
* @return {string|undefined} GetLegendGraphic URL.
* @api
*/
getGetLegendGraphicUrl(resolution, params) {
const layers = this.params_.LAYERS;
const isSingleLayer = !Array.isArray(layers) || this.params_['LAYERS'].length === 1;
if (this.url_ === undefined || !isSingleLayer) {
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
};
assign(baseParams, params);
return appendParams(/** @type {string} */ (this.url_), baseParams);
}
/**
* Get the user-provided params, i.e. those passed to the constructor through
* the "params" option, and possibly updated using the updateParams method.