Skip to content

Commit

Permalink
Fix image URL with QGIS server, allows to have hi DPI
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Apr 17, 2019
1 parent da09205 commit 33bf641
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
79 changes: 58 additions & 21 deletions contribs/gmf/src/print/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1233,40 +1233,51 @@ 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) {
const url = this.ngeoLayerHelper_.getWMTSLegendURL(layer);
if (url) {
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 {
const source = /** @type import("ol/source/ImageWMS.js").default */ (layer.getSource());
// 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,
} : {}));
}
});
Expand All @@ -1283,25 +1294,51 @@ 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 = 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;
}
let dist = Number.MAX_VALUE;
if (legendImage) {
dist = Math.abs(Math.log(72 / dpi));
}
return legendImage;
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,
};
}


Expand Down
2 changes: 2 additions & 0 deletions contribs/gmf/src/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>} [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.
Expand Down

0 comments on commit 33bf641

Please sign in to comment.