From 90c73e9aa0e9834ed1398bd1b728b9e488dce9df Mon Sep 17 00:00:00 2001 From: Laurent Lienher Date: Wed, 18 Jul 2018 16:46:14 +0200 Subject: [PATCH] Fix print layers when opacity background layer is a layer group --- src/map/LayerHelper.js | 36 +++++++++++++++++++++++++++--------- src/print/Service.js | 24 ++++++++++++++++++------ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/map/LayerHelper.js b/src/map/LayerHelper.js index dce1bed41f41..dd52eb5728bf 100644 --- a/src/map/LayerHelper.js +++ b/src/map/LayerHelper.js @@ -4,7 +4,7 @@ import googAsserts from 'goog/asserts.js'; import * as olArray from 'ol/array.js'; import olFormatWMTSCapabilities from 'ol/format/WMTSCapabilities.js'; -import olLayerGroup from 'ol/layer/Group.js'; +import OlLayerGroup from 'ol/layer/Group.js'; import olLayerImage from 'ol/layer/Image.js'; import olLayerTile from 'ol/layer/Tile.js'; import * as olObj from 'ol/obj.js'; @@ -241,7 +241,7 @@ exports.prototype.createWMTSLayerFromCapabilititesObj = function( * @export */ exports.prototype.createBasicGroup = function(opt_layers) { - const group = new olLayerGroup(); + const group = new OlLayerGroup(); if (opt_layers) { group.setLayers(opt_layers); } @@ -287,26 +287,44 @@ exports.prototype.getGroupFromMap = function(map, groupName) { * @export */ exports.prototype.getFlatLayers = function(layer) { - return this.getFlatLayers_(layer, []); + if (layer instanceof OlLayerGroup) { + const sublayers = layer.getLayers().getArray(); + const hasGroupLayer = sublayers.some(sublayer => sublayer instanceof OlLayerGroup); + if (!hasGroupLayer) { + return sublayers.slice(); + } + } + return this.getFlatLayers_(layer, [], undefined); }; /** * Get an array of all layers in a group. The group can contain multiple levels - * of others groups. + * of others groups. When we flatten a group, we get the child layers. + * If opacity is defined on the group, this value is lost. + * Computed opacity is a custom 'back-up' value that contains + * the calculated value of all ancestors and the given layer. * @param {ol.layer.Base} layer The base layer, mostly a group of layers. * @param {Array.} array An array to add layers. + * @param {number|undefined} computedOpacity Opacity inherited from ancestor layer groups. * @return {Array.} Layers. * @private */ -exports.prototype.getFlatLayers_ = function(layer, array) { - if (layer instanceof olLayerGroup) { +exports.prototype.getFlatLayers_ = function(layer, array, computedOpacity) { + const opacity = layer.getOpacity(); + if (computedOpacity !== undefined) { + computedOpacity *= opacity; + } else { + computedOpacity = opacity; + } + if (layer instanceof OlLayerGroup) { const sublayers = layer.getLayers(); sublayers.forEach((l) => { - this.getFlatLayers_(l, array); + this.getFlatLayers_(l, array, computedOpacity); }); } else { if (array.indexOf(layer) < 0) { + layer.set('inheritedOpacity', computedOpacity, true); array.push(layer); } } @@ -326,7 +344,7 @@ exports.prototype.getFlatLayers_ = function(layer, array) { exports.prototype.getLayerByName = function(layerName, layers) { let found = null; layers.some((layer) => { - if (layer instanceof olLayerGroup) { + if (layer instanceof OlLayerGroup) { const sublayers = layer.getLayers().getArray(); found = this.getLayerByName(layerName, sublayers); } else if (layer.get('layerNodeName') === layerName) { @@ -440,7 +458,7 @@ exports.prototype.refreshWMSLayer = function(layer) { * @param {number} ZIndex The ZIndex for children element. */ exports.prototype.setZIndexToFirstLevelChildren = function(element, ZIndex) { - if (!(element instanceof olLayerGroup)) { + if (!(element instanceof OlLayerGroup)) { return; } const innerGroupLayers = element.getLayers(); diff --git a/src/print/Service.js b/src/print/Service.js index 822306645729..bd2043549c1d 100644 --- a/src/print/Service.js +++ b/src/print/Service.js @@ -238,19 +238,19 @@ exports.prototype.encodeImageWmsLayer_ = function(arr, layer) { const url = source.getUrl(); if (url !== undefined) { this.encodeWmsLayer_( - arr, layer.getOpacity(), url, source.getParams()); + arr, layer, url, source.getParams()); } }; /** * @param {Array.} arr Array. - * @param {number} opacity Opacity of the layer. + * @param {ol.layer.Image} layer The layer. * @param {string} url Url of the WMS server. * @param {Object} params Url parameters * @private */ -exports.prototype.encodeWmsLayer_ = function(arr, opacity, url, params) { +exports.prototype.encodeWmsLayer_ = function(arr, layer, url, params) { if (url.startsWith('//')) { url = window.location.protocol + url; } @@ -280,7 +280,7 @@ exports.prototype.encodeWmsLayer_ = function(arr, opacity, url, params) { customParams: customParams, serverType: params['SERVERTYPE'], type: 'wms', - opacity: opacity, + opacity: this.getOpacityOrInherited_(layer), version: params['VERSION'], useNativeAngle: this.printNativeAngle_, }); @@ -360,7 +360,7 @@ exports.prototype.encodeTileWmtsLayer_ = function(arr, layer) { layer: source.getLayer(), matrices: matrices, matrixSet: source.getMatrixSet(), - opacity: layer.getOpacity(), + opacity: this.getOpacityOrInherited_(layer), requestEncoding: source.getRequestEncoding(), style: source.getStyle(), type: 'WMTS', @@ -383,7 +383,7 @@ exports.prototype.encodeTileWmsLayer_ = function(arr, layer) { googAsserts.assertInstanceof(source, olSourceTileWMS); this.encodeWmsLayer_( - arr, layer.getOpacity(), source.getUrls()[0], source.getParams()); + arr, layer, source.getUrls()[0], source.getParams()); }; @@ -399,6 +399,18 @@ exports.prototype.getWmtsUrl_ = function(source) { return exports.getAbsoluteUrl_(urls[0]); }; +/** + * Return an opacity value for the specified layer. + * @param {ol.layer.Base} layer Layer. + * @returns {number} opacity Opacity value. + * @private + */ +exports.prototype.getOpacityOrInherited_ = function(layer) { + if (layer.get('inheritedOpacity') !== undefined) { + return layer.get('inheritedOpacity'); + } + return layer.getOpacity(); +}; /** * Send a create report request to the MapFish Print service.