Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image URL with QGIS server, allows to have hi DPI #4833

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
sbrunner marked this conversation as resolved.
Show resolved Hide resolved
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) {
sbrunner marked this conversation as resolved.
Show resolved Hide resolved
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