From da4385e24c6b9626ffbcd92f3ccc9a946f611f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Tue, 16 Apr 2019 15:12:52 +0200 Subject: [PATCH] Fix image URL with QGIS server, allows to have hi DPI --- contribs/gmf/src/print/component.js | 80 +++++++++++++++++++++-------- contribs/gmf/src/themes.js | 2 + 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/contribs/gmf/src/print/component.js b/contribs/gmf/src/print/component.js index 99701599cb1b..ddec55c38c41 100644 --- a/contribs/gmf/src/print/component.js +++ b/contribs/gmf/src/print/component.js @@ -1233,15 +1233,18 @@ class Controller { // For WMTS layers. if (layer instanceof olLayerTile) { const layerName = `${layer.get('layerNodeName')}`; - let icons = this.getMetadataLegendImage_(layerName); - if (!icons) { - icons = this.ngeoLayerHelper_.getWMTSLegendURL(layer); + let icon_dpi = this.getMetadataLegendImage_(layerName, dpi); + if (!icon_dpi) { + icon_dpi = { + 'url': this.ngeoLayerHelper_.getWMTSLegendURL(layer), + 'dpi': 72 + }; } // Don't add classes without legend url. - if (icons) { + if (icon_dpi) { classes.push({ 'name': gettextCatalog.getString(layerName), - 'icons': [icons] + 'icons': [icon_dpi.url] }); } } else { @@ -1249,24 +1252,29 @@ class Controller { // For each name in a WMS layer. const layerNames = source.getParams()['LAYERS'].split(','); layerNames.forEach((name) => { - let icons = this.getMetadataLegendImage_(name); - if (!icons) { - icons = this.ngeoLayerHelper_.getWMSLegendURL(source.getUrl(), name, - scale, undefined, undefined, undefined, source.serverType_, dpi, - this.gmfLegendOptions_.useBbox ? bbox : undefined, - this.map.getView().getProjection().getCode(), - this.gmfLegendOptions_.params[source.serverType_] - ); + let icon_dpi = this.getMetadataLegendImage_(name, dpi); + const type = icon_dpi ? 'image' : source.serverType_; + if (!icon_dpi) { + icon_dpi = { + 'url': this.ngeoLayerHelper_.getWMSLegendURL(source.getUrl(), name, + scale, undefined, undefined, undefined, source.serverType_, dpi, + this.gmfLegendOptions_.useBbox ? bbox : undefined, + this.map.getView().getProjection().getCode(), + this.gmfLegendOptions_.params[source.serverType_] + ), + 'dpi': type === 'qgis' ? dpi : 72, + }; } + // Don't add classes without legend url or from layers without any // active name. - if (icons && name.length !== 0) { + if (icon_dpi && name.length !== 0) { classes.push(Object.assign({ - 'name': this.gmfLegendOptions_.label[source.serverType_] === false ? '' : + 'name': this.gmfLegendOptions_.label[type] === false ? '' : gettextCatalog.getString(name), - 'icons': [icons] - }, source.serverType_ === 'qgis' ? { - 'dpi': dpi, + 'icons': [icon_dpi.url] + }, icon_dpi.dpi != 72 ? { + 'dpi': icon_dpi.dpi, } : {})); } }); @@ -1283,25 +1291,55 @@ class Controller { return legend['classes'].length > 0 ? legend : null; } + /** + * @typedef {Object} LegendURLDPI + * @property {string} url The URL + * @property {number} dpi The DPI + */ /** * Return the metadata legendImage of a layer from the found corresponding node * or undefined. * @param {string} layerName a layer name. - * @return {string|undefined} The legendImage or undefined. + * @param {number} [dpi=72] the image DPI. + * @return {LegendURLDPI|undefined} The legendImage with selected DPI or undefined. * @private */ - getMetadataLegendImage_(layerName) { + getMetadataLegendImage_(layerName, dpi) { + if (dpi == undefined) { + dpi = 72; + } + const groupNode = findGroupByLayerNodeName(this.currentThemes_, layerName); let node; if (groupNode && groupNode.children) { node = findObjectByName(groupNode.children, layerName); } let legendImage; + let hiDPILegendImages; if (node && node.metadata) { legendImage = node.metadata.legendImage; + hiDPILegendImages = node.metadata.hiDPILegendImages; } - return legendImage; + let dist = Number.MAX_VALUE; + if (legendImage) { + dist = Math.abs(Math.log(72 / dpi)); + } + if (hiDPILegendImages) { + for (const str_dpi in hiDPILegendImages) { + const new_dpi = parseFloat(str_dpi); + const new_dist = Math.abs(Math.log(new_dpi / dpi)); + if (new_dist < dist) { + dist = new_dist; + dpi = new_dpi; + legendImage = hiDPILegendImages[str_dpi]; + } + } + } + return { + 'url': legendImage, + 'dpi': dpi, + }; } diff --git a/contribs/gmf/src/themes.js b/contribs/gmf/src/themes.js index 1874c5210fc6..746e731d28ef 100644 --- a/contribs/gmf/src/themes.js +++ b/contribs/gmf/src/themes.js @@ -204,6 +204,8 @@ * @property {boolean} [legend=false] Display the legend of this layers. For WMS and WMTS layers. * @property {string} [legendImage] The URL to the image used as a legend in the layer tree. For WMS and * WMTS layers. + * @property {Object} [hiDPILegendImages] The URLs to the hi DPI images used as a legend + * in the layer tree. For WMS and WMTS layers. * @property {string} [legendRule] The WMS 'RULE' parameter used to display the icon in the layer tree. * "Short version" of the 'iconURL' metadata for WMS layers. For WMS layers. * @property {number} [maxResolution] The max resolution where the layer is visible. For WMS layers.