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 print layers when opacity background layer is a layer group #4041

Merged
merged 1 commit into from
Jul 19, 2018
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
36 changes: 27 additions & 9 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,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.<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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use an optional arguments for "computedOpacity" instead of calling this function with "undefined" at line 297 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion with @gberaudo, we prefer that it is explicit that the value is called as 'undefined' instead of skipping the last argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check in our code if there is a way to indicate (document) that this opacity and should be used ?

array.push(layer);
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 18 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,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.
Expand Down