Skip to content

Commit

Permalink
SERVER: fix getpring atlas with DD follow theme
Browse files Browse the repository at this point in the history
Fix #54475 when an atlas map has a data-defined
follow theme with an expression which depends on
the atlas feature.
  • Loading branch information
elpaso authored and nyalldawson committed Sep 29, 2023
1 parent ff9c1b5 commit cf75df9
Show file tree
Hide file tree
Showing 4 changed files with 2,437 additions and 2,523 deletions.
18 changes: 16 additions & 2 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -454,15 +454,19 @@ namespace QgsWms
}

filterString.append( " )" );

}

atlas->setFilterFeatures( true );

QString errorString;
atlas->setFilterExpression( filterString, errorString );

if ( !errorString.isEmpty() )
{
throw QgsException( QStringLiteral( "An error occurred during the Atlas print: %1" ).arg( errorString ) );
}

}
}

Expand Down Expand Up @@ -667,7 +671,7 @@ namespace QgsWms
return tempOutputFile.readAll();
}

bool QgsRenderer::configurePrintLayout( QgsPrintLayout *c, const QgsMapSettings &mapSettings, bool atlasPrint )
bool QgsRenderer::configurePrintLayout( QgsPrintLayout *c, const QgsMapSettings &mapSettings, QgsLayoutAtlas *atlas )
{

c->renderContext().setSelectionColor( mapSettings.selectionColor() );
Expand All @@ -689,7 +693,7 @@ namespace QgsWms
cMapParams.mLayers = mWmsParameters.composerMapParameters( -1 ).mLayers;
}

if ( !atlasPrint || !map->atlasDriven() ) //No need to extent, scal, rotation set with atlas feature
if ( !atlas || !map->atlasDriven() ) //No need to extent, scale, rotation set with atlas feature
{
//map extent is mandatory
if ( !cMapParams.mHasExtent )
Expand Down Expand Up @@ -727,6 +731,7 @@ namespace QgsWms

if ( !map->keepLayerSet() )
{

QList<QgsMapLayer *> layerSet;

for ( const auto &layer : std::as_const( cMapParams.mLayers ) )
Expand Down Expand Up @@ -774,6 +779,15 @@ namespace QgsWms
QMap<QString, QString> layersStyle;
if ( map->followVisibilityPreset() )
{

if ( atlas )
{
// Possibly triggers a refresh of the DD visibility preset (theme) name
// see issue GH #54475
atlas->updateFeatures();
atlas->first();
}

const QString presetName = map->followVisibilityPresetName();
if ( layerSet.isEmpty() )
{
Expand Down
5 changes: 3 additions & 2 deletions src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -20,6 +20,7 @@
#ifndef QGSWMSRENDERER_H
#define QGSWMSRENDERER_H

#include "qgslayoutatlas.h"
#include "qgsserversettings.h"
#include "qgswmsparameters.h"
#include "qgswmsrendercontext.h"
Expand Down Expand Up @@ -324,10 +325,10 @@ namespace QgsWms
* Configures the print layout for the GetPrint request
*\param c the print layout
*\param mapSettings the map settings
*\param atlasPrint true if atlas is used for printing
*\param atlas atlas used for printing, maybe NULL
*\returns true in case of success
*/
bool configurePrintLayout( QgsPrintLayout *c, const QgsMapSettings &mapSettings, bool atlasPrint = false );
bool configurePrintLayout( QgsPrintLayout *c, const QgsMapSettings &mapSettings, QgsLayoutAtlas *atlas );

void removeTemporaryLayers();

Expand Down
46 changes: 46 additions & 0 deletions tests/src/python/test_qgsserver_wms_getprint_maptheme.py
Expand Up @@ -308,6 +308,52 @@ def test_wms_getprint_maptheme_highlight(self):
image = QImage.fromData(response.body(), "PNG")
self._assertBlue(image.pixelColor(100, 100))

def test_wms_getprint_atlas_dd_theme_(self):
"""Test a template with atlas DD theme"""

project = self.project

# No LAYERS specified
params = {
'SERVICE': 'WMS',
'VERSION': '1.3.0',
'REQUEST': 'GetPrint',
'TEMPLATE': 'data_defined_theme',
'FORMAT': 'png',
'CRS': 'EPSG:4326',
'DPI': '72',
}

def _test_red():
params['ATLAS_PK'] = '2'

response = QgsBufferServerResponse()
request = QgsBufferServerRequest('?' + '&'.join(["%s=%s" % i for i in params.items()]))
self.server.handleRequest(request, response, project)

image = QImage.fromData(response.body(), "PNG")
# Expected: white and red
self._assertRed(image.pixelColor(325, 184))
self._assertWhite(image.pixelColor(685, 150))

def _test_green():
params['ATLAS_PK'] = '4'

response = QgsBufferServerResponse()
request = QgsBufferServerRequest('?' + '&'.join(["%s=%s" % i for i in params.items()]))
self.server.handleRequest(request, response, project)

image = QImage.fromData(response.body(), "PNG")
# Expected: green and white
self._assertGreen(image.pixelColor(325, 184))
self._assertWhite(image.pixelColor(685, 150))

# Alternate test to make sure nothing is cached
_test_red()
_test_green()
_test_red()
_test_green()


if __name__ == '__main__':
unittest.main()

0 comments on commit cf75df9

Please sign in to comment.