Skip to content

Commit

Permalink
Fix print layers when opacity background layer is a layer group
Browse files Browse the repository at this point in the history
  • Loading branch information
llienher committed Jul 19, 2018
1 parent c76c96f commit 33bbc8c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
31 changes: 23 additions & 8 deletions src/map/LayerHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -287,7 +287,14 @@ 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);
};


Expand All @@ -296,17 +303,25 @@ exports.prototype.getFlatLayers = function(layer) {
* of others groups.
* @param {ol.layer.Base} layer The base layer, mostly a group of layers.
* @param {Array.<ol.layer.Base>} array An array to add layers.
* @param {number|undefined} computedOpacity Opacity inherited from ancestor layer groups.
* @return {Array.<ol.layer.Layer>} 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);
}
}
Expand All @@ -326,7 +341,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) {
Expand Down Expand Up @@ -440,7 +455,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();
Expand Down
27 changes: 21 additions & 6 deletions src/print/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<MapFishPrintLayer>} 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;
}
Expand Down Expand Up @@ -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_,
});
Expand Down Expand Up @@ -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',
Expand All @@ -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());
};


Expand All @@ -399,6 +399,21 @@ exports.prototype.getWmtsUrl_ = function(source) {
return exports.getAbsoluteUrl_(urls[0]);
};

/**
* Return an opacity value for the specified layer.
* Usage: When we flatten a group (getFlatLayers() method), we get only the child layers.
* If opacity is defined on the group, this value is lost.
* Inherited opacity is a custom 'back-up' value that contains the parent element opacity.
* @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.
Expand Down

0 comments on commit 33bbc8c

Please sign in to comment.