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 legend on off #4184

Merged
merged 3 commits into from
Feb 12, 2024
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
46 changes: 29 additions & 17 deletions assets/src/components/Print.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default class Print extends HTMLElement {
const deltaY = (this._maskHeight * this._printScale) / 2 / INCHES_PER_METER / DOTS_PER_INCH;
let extent = [center[0] - deltaX, center[1] - deltaY, center[0] + deltaX, center[1] + deltaY];
const mapProjection = mainLizmap.config.options.projection.ref;
const projectProjection = mainLizmap.config.options.qgisProjectProjection.ref;
const projectProjection = mainLizmap.config.options.qgisProjectProjection.ref ? mainLizmap.config.options.qgisProjectProjection.ref : mapProjection;

if(projectProjection != mapProjection){
extent = transformExtent(extent, mapProjection, projectProjection);
Expand All @@ -208,7 +208,7 @@ export default class Print extends HTMLElement {
VERSION: '1.3.0',
FORMAT: this._printFormat,
TRANSPARENT: true,
SRS: projectProjection,
CRS: projectProjection,
DPI: this._printDPI,
TEMPLATE: this._printTemplates[this._printTemplate].title
};
Expand Down Expand Up @@ -253,45 +253,57 @@ export default class Print extends HTMLElement {
orderedVisibleLayers[layer.layerOrder] = layer;
}
});

// Selection and filter
const filter = [];
const selection = [];
const legendOn = [];
const legendOff = [];
for (const layerIndex of Object.keys(orderedVisibleLayers).reverse()) {
const layer = orderedVisibleLayers[layerIndex];
const layerWmsParams = layer.wmsParameters;
// Add layer to the list of printed layers
printLayers.push(layer.wmsName);
printLayers.push(layerWmsParams['LAYERS']);

// Optionally add layer style if needed (same order as layers )
styleLayers.push(layer.wmsSelectedStyleName);
styleLayers.push(layerWmsParams['STYLES']);

// Handle qgis layer opacity otherwise client value override it
if (layer.layerConfig?.opacity) {
opacityLayers.push(parseInt(255 * layer.opacity * layer.layerConfig.opacity));
} else {
opacityLayers.push(parseInt(255 * layer.opacity));
}
if ('FILTERTOKEN' in layerWmsParams) {
filter.push(layerWmsParams['FILTERTOKEN']);
}
if ('SELECTIONTOKEN' in layerWmsParams) {
selection.push(layerWmsParams['SELECTIONTOKEN']);
}
if ('LEGEND_ON' in layerWmsParams) {
legendOn.push(layerWmsParams['LEGEND_ON']);
}
if ('LEGEND_OFF' in layerWmsParams) {
legendOff.push(layerWmsParams['LEGEND_OFF']);
}
}

wmsParams[this._mainMapID + ':LAYERS'] = printLayers.join(',');
wmsParams[this._mainMapID + ':STYLES'] = styleLayers.join(',');
wmsParams[this._mainMapID + ':OPACITIES'] = opacityLayers.join(',');

// Selection and filter
const filter = [];
const selection = [];
for (const layerConfig of Object.values(mainLizmap.config.layers)) {
const filtertoken = layerConfig?.request_params?.filtertoken;
const selectiontoken = layerConfig?.request_params?.selectiontoken;
if (filtertoken) {
filter.push(filtertoken);
}
if (selectiontoken) {
selection.push(selectiontoken);
}
}
if (filter.length) {
wmsParams.FILTERTOKEN = filter.join(';');
}
if (selection.length) {
wmsParams.SELECTIONTOKEN = selection.join(';');
}
if (legendOn.length) {
wmsParams.LEGEND_ON = legendOn.join(';');
}
if (legendOff.length) {
wmsParams.LEGEND_OFF = legendOff.join(';');
}

// If user has made a draw, print it with redlining
const formatWKT = new WKT();
Expand Down
104 changes: 89 additions & 15 deletions tests/end2end/playwright/print.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ test.describe('Print', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A2154')
expect(postData).toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3AEXTENT=759249.549002605%2C6271892.11637865%2C781949.549002605%2C6286892.11637865')
expect(postData).toMatch(/map0%3AEXTENT=759249.\d+%2C6271892.\d+%2C781949.\d+%2C6286892.\d+/)
expect(postData).toContain('map0%3ASCALE=100000')
expect(postData).toContain('map0%3ALAYERS=OpenStreetMap%2Cquartiers%2Csousquartiers')
expect(postData).toContain('map0%3ASTYLES=default%2Cd%C3%A9faut%2Cd%C3%A9faut')
Expand All @@ -72,10 +72,10 @@ test.describe('Print', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=jpeg')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A2154')
expect(postData).toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=200')
expect(postData).toContain('TEMPLATE=print_map')
expect(postData).toContain('map0%3AEXTENT=765699.549002605%2C6271792.11637865%2C775499.549002605%2C6286992.11637865')
expect(postData).toMatch(/map0%3AEXTENT=765699.\d+%2C6271792.\d+%2C775499.\d+%2C6286992.\d+/)
expect(postData).toContain('map0%3ASCALE=100000')
expect(postData).toContain('map0%3ALAYERS=OpenStreetMap%2Cquartiers%2Csousquartiers')
expect(postData).toContain('map0%3ASTYLES=default%2Cd%C3%A9faut%2Cd%C3%A9faut')
Expand All @@ -92,15 +92,15 @@ test.describe('Print', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A2154')
expect(postData).toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3AEXTENT=759249.549002605%2C6271892.11637865%2C781949.549002605%2C6286892.11637865')
expect(postData).toMatch(/map0%3AEXTENT=759249.\d+%2C6271892.\d+%2C781949.\d+%2C6286892.\d+/)
expect(postData).toContain('map0%3ASCALE=100000')
expect(postData).toContain('map0%3ALAYERS=OpenStreetMap%2Cquartiers%2Csousquartiers')
expect(postData).toContain('map0%3ASTYLES=default%2Cd%C3%A9faut%2Cd%C3%A9faut')
expect(postData).toContain('map0%3AOPACITIES=255%2C255%2C255')
expect(postData).toContain('map0%3AHIGHLIGHT_GEOM=CIRCULARSTRING(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20772265.9549028379%206279008.469778025%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20775229.7665963736%206281972.281471561%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20778193.5782899094%206279008.469778025%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20775229.7665963736%206276044.6580844885%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20772265.9549028379%206279008.469778025)')
expect(postData).toMatch(/map0%3AHIGHLIGHT_GEOM=CIRCULARSTRING\(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20772265.\d+%206279008.\d+%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20775229.\d+%206281972.\d+%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20778193.\d+%206279008.\d+%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20775229.\d+%206276044.\d+%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20772265.\d+%206279008.\d+\)/)
expect(postData).toContain('map0%3AHIGHLIGHT_SYMBOL=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%20%20%20%20%3CStyledLayerDescriptor%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20version%3D%221.1.0%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.1.0%2FStyledLayerDescriptor.xsd%22%20xmlns%3Ase%3D%22http%3A%2F%2Fwww.opengis.net%2Fse%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CUserStyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFeatureTypeStyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CRule%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CPolygonSymbolizer%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CStroke%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSvgParameter%20name%3D%22stroke%22%3E%23ff0000%3C%2FSvgParameter%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSvgParameter%20name%3D%22stroke-opacity%22%3E1%3C%2FSvgParameter%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSvgParameter%20name%3D%22stroke-width%22%3E2%3C%2FSvgParameter%3E%0A%20%20%20%20%20%20%20%20%3C%2FStroke%3E%0A%20%20%20%20%20%20%20%20%3CFill%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSvgParameter%20name%3D%22fill%22%3E%23ff0000%3C%2FSvgParameter%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSvgParameter%20name%3D%22fill-opacity%22%3E0.2%3C%2FSvgParameter%3E%0A%20%20%20%20%20%20%20%20%3C%2FFill%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FPolygonSymbolizer%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FRule%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FFeatureTypeStyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FUserStyle%3E%0A%20%20%20%20%20%20%20%20%3C%2FStyledLayerDescriptor%3E')
expect(postData).toContain('simple_label=simple%20label');
// Disabled because of the migration when project is saved with QGIS >= 3.32
Expand All @@ -109,8 +109,8 @@ test.describe('Print', () => {

await page.locator('#button-draw').click();
await page.locator('#draw').click();
await page.locator('.digitizing-buttons').click();
await page.locator('.digitizing-circle > svg').click();
await page.locator('#draw .digitizing-buttons').click();
await page.locator('#draw .digitizing-circle > svg').click();
await page.locator('#newOlMap').click({
position: {
x: 610,
Expand All @@ -128,6 +128,80 @@ test.describe('Print', () => {
await page.locator('#print-scale').selectOption('100000');
await page.locator('#print-launch').click();
});

test('Print requests with selection', async ({ page }) => {
// Select a feature
await page.locator('#button-attributeLayers').click();
await page.getByRole('button', { name: 'Detail' }).click();
await page.locator('lizmap-feature-toolbar:nth-child(1) > div:nth-child(1) > button:nth-child(1)').first().click();
await page.locator('#bottom-dock-window-buttons .btn-bottomdock-clear').click();

page.on('request', request => {
if(request.method() === "POST") {
const postData = request.postData();
if (postData != null && postData.includes('GetPrint')){
expect(postData).toContain('SERVICE=WMS')
expect(postData).toContain('REQUEST=GetPrint')
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3ASCALE=100000')
expect(postData).toContain('map0%3ALAYERS=OpenStreetMap%2Cquartiers%2Csousquartiers')
expect(postData).toContain('map0%3ASTYLES=default%2Cd%C3%A9faut%2Cd%C3%A9faut')
expect(postData).toContain('map0%3AOPACITIES=255%2C255%2C255');
expect(postData).toContain('simple_label=simple%20label');
expect(postData).toContain('SELECTIONTOKEN=');
}
}
});

await page.locator('#print-launch').click();
});

test('Print requests with filter', async ({ page }) => {
// Select a feature
await page.locator('#button-attributeLayers').click();
await page.getByRole('button', { name: 'Detail' }).click();
await page.locator('lizmap-feature-toolbar:nth-child(1) > div:nth-child(1) > button:nth-child(1)').first().click();
await page.locator('#bottom-dock-window-buttons .btn-bottomdock-clear').click();

// Filter selected feature
await page.locator('#button-attributeLayers').click();
const responseMatchGetFilterTokenFunc = function (response) {
return (response.request().method() == 'POST' && response.request().postData().match(/GetFilterToken/i));
};
await page.locator('.btn-filter-attributeTable').click();
let getFilterTokenPromise = page.waitForResponse(responseMatchGetFilterTokenFunc);
await getFilterTokenPromise;

page.on('request', request => {
if(request.method() === "POST") {
const postData = request.postData();
if (postData != null && postData.includes('GetPrint')){
expect(postData).toContain('SERVICE=WMS')
expect(postData).toContain('REQUEST=GetPrint')
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3ASCALE=100000')
expect(postData).toContain('map0%3ALAYERS=OpenStreetMap%2Cquartiers%2Csousquartiers')
expect(postData).toContain('map0%3ASTYLES=default%2Cd%C3%A9faut%2Cd%C3%A9faut')
expect(postData).toContain('map0%3AOPACITIES=255%2C255%2C255');
expect(postData).toContain('simple_label=simple%20label');
expect(postData).toContain('FILTERTOKEN=');
}
}
});

await page.locator('#bottom-dock-window-buttons .btn-bottomdock-clear').click();
await page.locator('#print-launch').click();
});
});

test.describe('Print in popup', () => {
Expand Down Expand Up @@ -175,7 +249,7 @@ test.describe('Print in popup', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).not.toContain('SRS=EPSG%3A2154')
expect(postData).not.toContain('CRS=EPSG%3A2154')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=atlas_quartiers')
expect(postData).not.toContain('LAYERS=quartiers')
Expand Down Expand Up @@ -338,7 +412,7 @@ test.describe('Print 3857', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A3857')
expect(postData).toContain('CRS=EPSG%3A3857')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3AEXTENT=423093.00655000005%2C5399873.567900001%2C439487.85455000005%2C5410707.167900001')
Expand All @@ -362,7 +436,7 @@ test.describe('Print 3857', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=jpeg')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A3857')
expect(postData).toContain('CRS=EPSG%3A3857')
expect(postData).toContain('DPI=200')
expect(postData).toContain('TEMPLATE=print_map')
expect(postData).toContain('map0%3AEXTENT=427751.45455%2C5399801.343900001%2C434829.4065500001%2C5410779.391900001')
Expand All @@ -382,7 +456,7 @@ test.describe('Print 3857', () => {
expect(postData).toContain('VERSION=1.3.0')
expect(postData).toContain('FORMAT=pdf')
expect(postData).toContain('TRANSPARENT=true')
expect(postData).toContain('SRS=EPSG%3A3857')
expect(postData).toContain('CRS=EPSG%3A3857')
expect(postData).toContain('DPI=100')
expect(postData).toContain('TEMPLATE=print_labels')
expect(postData).toContain('map0%3AEXTENT=423093.00655000005%2C5399873.567900001%2C439487.85455000005%2C5410707.167900001')
Expand All @@ -399,8 +473,8 @@ test.describe('Print 3857', () => {

await page.locator('#button-draw').click();
await page.locator('#draw').click();
await page.locator('.digitizing-buttons').click();
await page.locator('.digitizing-circle > svg').click();
await page.locator('#draw .digitizing-buttons').click();
await page.locator('#draw .digitizing-circle > svg').click();
await page.locator('#newOlMap').click({
position: {
x: 610,
Expand Down
Loading
Loading