Skip to content

Commit

Permalink
Fix geosolutions-it#10812 excluding fixed list of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
MV88 committed Feb 14, 2025
1 parent c6957a2 commit cd67f8a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 4 additions & 0 deletions web/client/plugins/Print.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import { MapLibraries } from '../utils/MapTypeUtils';
* @prop {object} cfg.outputFormatOptions options for the output formats
* @prop {object[]} cfg.outputFormatOptions.allowedFormats array of allowed formats, e.g. [{"name": "PDF", "value": "pdf"}]
* @prop {object} cfg.projectionOptions options for the projections
* @prop {string[]} cfg.excludeLayersFromLegend list of layer names e.g. ["workspace:layerName"] to exclude from printed document
* @prop {object[]} cfg.projectionOptions.projections array of available projections, e.g. [{"name": "EPSG:3857", "value": "EPSG:3857"}]
* @prop {object} cfg.overlayLayersOptions options for overlay layers
* @prop {boolean} cfg.overlayLayersOptions.enabled if true a checkbox will be shown to exclude or include overlay layers to the print
Expand Down Expand Up @@ -288,6 +289,7 @@ export default {
currentLocale: PropTypes.string,
overrideOptions: PropTypes.object,
items: PropTypes.array,
excludeLayersFromLegend: PropTypes.array,
addPrintParameter: PropTypes.func,
printingService: PropTypes.object,
printMap: PropTypes.object
Expand All @@ -309,6 +311,7 @@ export default {
onPrint: () => {},
configurePrintMap: () => {},
printSpecTemplate: {},
excludeLayersFromLegend: [],
getLayoutName: getLayoutName,
getZoomForExtent: defaultGetZoomForExtent,
pdfUrl: null,
Expand Down Expand Up @@ -604,6 +607,7 @@ export default {
this.props.setPage(0);
this.props.onBeforePrint();
this.props.printingService.print({
excludeLayersFromLegend: this.props.excludeLayersFromLegend,
layers: this.getMapConfiguration()?.layers,
scales: this.props.useFixedScales ? getPrintScales(this.props.capabilities) : undefined,
bbox: this.props.map?.bbox
Expand Down
8 changes: 5 additions & 3 deletions web/client/utils/PrintUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { extractValidBaseURL } from './TileProviderUtils';
import { getTileMatrix } from './WMTSUtils';
import { guessFormat } from './TMSUtils';
import { get as getProjection } from 'ol/proj';
import { isArray, filter, find, isEmpty, toNumber, castArray, reverse } from 'lodash';
import { isArray, filter, find, isEmpty, toNumber, castArray, reverse, includes } from 'lodash';
import { getFeature } from '../api/WFS';
import { generateEnvString } from './LayerLocalizationUtils';
import { ServerTypes } from './LayersUtils';
Expand Down Expand Up @@ -277,7 +277,7 @@ export const getLayersCredits = (layers) => {
* @memberof utils.PrintUtils
*/
export const getMapfishPrintSpecification = (rawSpec, state) => {
const {params, ...baseSpec} = rawSpec;
const {params, excludeLayersFromLegend, ...baseSpec} = rawSpec;
const spec = {...baseSpec, ...params};
const printMap = state?.print?.map;
const projectedCenter = reproject(spec.center, 'EPSG:4326', spec.projection);
Expand All @@ -291,6 +291,8 @@ export const getMapfishPrintSpecification = (rawSpec, state) => {
center: projectedCenter,
scaleZoom: projectedZoom
};
let legendLayers = spec.layers.filter(layer => !includes(excludeLayersFromLegend, layer.name));
legendLayers = PrintUtils.getMapfishLayersSpecification(legendLayers, projectedSpec, state, 'legend');
return {
"units": getUnits(spec.projection),
"srs": normalizeSRS(spec.projection || 'EPSG:3857'),
Expand All @@ -311,7 +313,7 @@ export const getMapfishPrintSpecification = (rawSpec, state) => {
"rotation": !isNil(spec.rotation) ? -Number(spec.rotation) : 0 // negate the rotation value to match rotation in map preview and printed output
}
],
"legends": PrintUtils.getMapfishLayersSpecification(spec.layers, projectedSpec, state, 'legend'),
"legends": legendLayers,
"credits": getLayersCredits(spec.layers),
...params
};
Expand Down
60 changes: 60 additions & 0 deletions web/client/utils/__tests__/PrintUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,66 @@ describe('PrintUtils', () => {
expect(printSpec).toExist();
expect(printSpec.custom).toBe("customvalue");
});
it("getMapfishPrintSpecification, valid spec with legend, and excluded layer from legeng", () => {
const spec = {
projection: "EPSG:4326",
sheet: "A4",
landscape: true,
resolution: "96",
name: "Test print",
user: "user1",
scale: 500000,
printCrs: "World WGS 84 (EPSG:4326)",
description: "Test description",
includeLegend: true,
includeNotes: true,
layers: [
{
external: true,
url: "/wms",
singleTile: false,
opacity: 0.8,
name: "layer-test",
title: "layer-test",
format: "png",
style: "style1",
cql_filter: "x=1",
visibility: true,
type: "wms"
},
{
external: true,
url: "/wms",
singleTile: false,
opacity: 0.8,
name: "layer-test-exclude",
title: "layer-test",
format: "png",
style: "style1",
cql_filter: "x=1",
visibility: true,
type: "wms"
}
],
geoserverUrls: [
"/rest/geoserver",
"/rest/geoserver1",
"/rest/geoserver2"
],
center: {x: 0, y: 0, crs: "EPSG:4326"},
type: "WMS"
};
let mapFishSpec = getMapfishPrintSpecification({
...spec,
forceLabels: true,
antiAliasing: true,
legendDpi: 96,
bold: true,
excludeLayersFromLegend: ["layer-test-exclude"]
});
expect(mapFishSpec.legends.length).toBe(1);

});
it('getMapfishPrintSpecification with fixed scales', () => {
const printSpec = getMapfishPrintSpecification({
...testSpec,
Expand Down

0 comments on commit cd67f8a

Please sign in to comment.