From 492522ca8bfa2cc81c12cb68faa05e54fb46a65f Mon Sep 17 00:00:00 2001 From: Lorenzo Natali Date: Mon, 5 Oct 2020 11:27:42 +0200 Subject: [PATCH 1/2] #5292: Fixed opacity issues in printing --- web/client/utils/PrintUtils.js | 20 ++++++------ web/client/utils/__tests__/PrintUtils-test.js | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/web/client/utils/PrintUtils.js b/web/client/utils/PrintUtils.js index bd8d0fac22..d41fb3eed8 100644 --- a/web/client/utils/PrintUtils.js +++ b/web/client/utils/PrintUtils.js @@ -243,7 +243,7 @@ const PrintUtils = { wms: { map: (layer, spec) => ({ "baseURL": PrintUtils.normalizeUrl(layer.url) + '?', - "opacity": layer.opacity || 1.0, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), "singleTile": false, "type": "WMS", "layers": [ @@ -300,7 +300,7 @@ const PrintUtils = { map: (layer, spec) => ({ type: 'Vector', name: layer.name, - "opacity": layer.opacity || 1.0, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), styleProperty: "ms_style", styles: { 1: PrintUtils.toOpenLayers2Style(layer, layer.style), @@ -323,7 +323,7 @@ const PrintUtils = { map: (layer) => ({ type: 'Vector', name: layer.name, - "opacity": layer.opacity || 1.0, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), styleProperty: "ms_style", styles: { 1: PrintUtils.toOpenLayers2Style(layer, layer.style), @@ -341,9 +341,9 @@ const PrintUtils = { ) }, osm: { - map: () => ({ + map: (layer = {}) => ({ "baseURL": "http://a.tile.openstreetmap.org/", - "opacity": 1, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), "singleTile": false, "type": "OSM", "maxExtent": [ @@ -381,9 +381,9 @@ const PrintUtils = { }) }, mapquest: { - map: () => ({ + map: (layer = {}) => ({ "baseURL": "http://otile1.mqcdn.com/tiles/1.0.0/map/", - "opacity": 1, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), "singleTile": false, "type": "OSM", "maxExtent": [ @@ -458,7 +458,7 @@ const PrintUtils = { "style": layer.style, "name": layer.name, "requestEncoding": layer.requestEncoding === "RESTful" ? "REST" : layer.requestEncoding, - "opacity": layer.opacity || layer.opacity === 0 ? 0 : 1.0, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), "version": layer.version || "1.0.0" }; } @@ -485,7 +485,7 @@ const PrintUtils = { path_format: pathFormat, "type": 'xyz', "extension": validURL.split('.').pop() || "png", - "opacity": layer.opacity || layer.opacity === 0 ? 0 : 1.0, + "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), "tileSize": [256, 256], "maxExtent": [-20037508.3392, -20037508.3392, 20037508.3392, 20037508.3392], "resolutions": MapUtils.getResolutions() @@ -501,7 +501,7 @@ const PrintUtils = { const layerName = layer.tileMapUrl.split(layer.tileMapService + "/")[1]; return { type: 'tms', - opacity: layer.opacity || layer.opacity === 0 ? 0 : 1.0, + opacity: layer.opacity || (layer.opacity === 0 ? 0 : 1.0), layer: layerName, // baseURL for mapfish print required to remove the version baseURL: layer.tileMapService.substring(0, layer.tileMapService.lastIndexOf("/1.0.0")), diff --git a/web/client/utils/__tests__/PrintUtils-test.js b/web/client/utils/__tests__/PrintUtils-test.js index 8e8d5783d5..ceb92dd12e 100644 --- a/web/client/utils/__tests__/PrintUtils-test.js +++ b/web/client/utils/__tests__/PrintUtils-test.js @@ -457,6 +457,38 @@ describe('PrintUtils', () => { expect(rgb).toBe("rgb(255, 255, 255)"); }); describe('specCreators', () => { + describe('opacity', () => { + const testBase = { + wms: layer, + wmts: KVP1, + vector: vectorLayer, + tms: TMS110_1, + tileprovider: BasemapAT, + osm: { + "group": "background", + "source": "osm", + "name": "mapnik", + "title": "Open Street Map", + "type": "osm", + "visibility": true, + "singleTile": false, + "dimensions": [], + "id": "mapnik__0", + "loading": false, + "loadingError": false + } + }; + it('check opacity for all layers to be 1 for undefined, therwise its value', () => { + Object.keys(PrintUtils.specCreators).map( k => { + const fun = PrintUtils.specCreators[k].map; + // 0 must remain + expect(fun({ ...(testBase[k] || {}), opacity: 0 }, { projection: "EPSG:900913" }).opacity).toEqual(0); + expect(fun({ ...(testBase[k] || {}), opacity: 0.5 }, { projection: "EPSG:900913" }).opacity).toEqual(0.5); + expect(fun({ ...(testBase[k] || {}), opacity: undefined }, { projection: "EPSG:900913" }).opacity).toEqual(1); + + } ); + }); + }); describe('WMTS', () => { const checkMatrixIds = (layerSpec, tileMatrixSet) => layerSpec.matrixIds.map((mid, index) => { const tileMatrixEntry = tileMatrixSet.TileMatrix[index]; From b02bccc9936bb1d444b2b16c94de95632b3f1f6c Mon Sep 17 00:00:00 2001 From: Lorenzo Natali Date: Tue, 6 Oct 2020 09:38:54 +0200 Subject: [PATCH 2/2] Unified opacity exract system in printutils --- web/client/utils/PrintUtils.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/web/client/utils/PrintUtils.js b/web/client/utils/PrintUtils.js index d41fb3eed8..a67e0299c8 100644 --- a/web/client/utils/PrintUtils.js +++ b/web/client/utils/PrintUtils.js @@ -37,6 +37,14 @@ const getGeomType = function(layer) { const isAnnotationLayer = (layer) => { return layer.id === "annotations" || layer.name === "Measurements"; }; + +/** + * Extracts the correct opacity from layer. if Undefined, the opacity is `1`. + * @ignore + * @param {object} layer the MapStore layer + */ +const getOpacity = layer => layer.opacity || (layer.opacity === 0 ? 0 : 1.0); + /** * Utilities for Print * @memberof utils @@ -243,7 +251,7 @@ const PrintUtils = { wms: { map: (layer, spec) => ({ "baseURL": PrintUtils.normalizeUrl(layer.url) + '?', - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), "singleTile": false, "type": "WMS", "layers": [ @@ -300,7 +308,7 @@ const PrintUtils = { map: (layer, spec) => ({ type: 'Vector', name: layer.name, - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), styleProperty: "ms_style", styles: { 1: PrintUtils.toOpenLayers2Style(layer, layer.style), @@ -323,7 +331,7 @@ const PrintUtils = { map: (layer) => ({ type: 'Vector', name: layer.name, - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), styleProperty: "ms_style", styles: { 1: PrintUtils.toOpenLayers2Style(layer, layer.style), @@ -343,7 +351,7 @@ const PrintUtils = { osm: { map: (layer = {}) => ({ "baseURL": "http://a.tile.openstreetmap.org/", - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), "singleTile": false, "type": "OSM", "maxExtent": [ @@ -383,7 +391,7 @@ const PrintUtils = { mapquest: { map: (layer = {}) => ({ "baseURL": "http://otile1.mqcdn.com/tiles/1.0.0/map/", - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), "singleTile": false, "type": "OSM", "maxExtent": [ @@ -458,7 +466,7 @@ const PrintUtils = { "style": layer.style, "name": layer.name, "requestEncoding": layer.requestEncoding === "RESTful" ? "REST" : layer.requestEncoding, - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), "version": layer.version || "1.0.0" }; } @@ -485,7 +493,7 @@ const PrintUtils = { path_format: pathFormat, "type": 'xyz', "extension": validURL.split('.').pop() || "png", - "opacity": layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + "opacity": getOpacity(layer), "tileSize": [256, 256], "maxExtent": [-20037508.3392, -20037508.3392, 20037508.3392, 20037508.3392], "resolutions": MapUtils.getResolutions() @@ -501,7 +509,7 @@ const PrintUtils = { const layerName = layer.tileMapUrl.split(layer.tileMapService + "/")[1]; return { type: 'tms', - opacity: layer.opacity || (layer.opacity === 0 ? 0 : 1.0), + opacity: getOpacity(layer), layer: layerName, // baseURL for mapfish print required to remove the version baseURL: layer.tileMapService.substring(0, layer.tileMapService.lastIndexOf("/1.0.0")),