Skip to content
Permalink
Browse files

[BUGFIX][QGIS-Server] Add all layers for fixed legend in GetPrint

If a legend is not linked to a map, it's a fixed legend and does not depend on
 layers parameter. This bugfix loads all layers needed by legend and update it.
  • Loading branch information
rldhont committed Jun 19, 2015
1 parent 35f3838 commit 7c735213ca54371f10881856ebac767dbcc878c4
Showing with 39 additions and 2 deletions.
  1. +39 −2 src/server/qgswmsprojectparser.cpp
@@ -20,6 +20,7 @@
#include "qgsconfigparserutils.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsmapserviceexception.h"
#include "qgspallabeling.h"
@@ -38,6 +39,7 @@
#include "qgscomposerscalebar.h"
#include "qgscomposershape.h"
#include "qgslayertreegroup.h"
#include "qgslayertreelayer.h"

#include <QFileInfo>
#include <QTextDocument>
@@ -442,17 +444,52 @@ QgsComposition* QgsWMSProjectParser::initComposition( const QString& composerTem
QgsComposerLegend* legend = dynamic_cast< QgsComposerLegend *>( *itemIt );
if ( legend )
{
#if 0
QgsLegendModelV2* model = legend->modelV2();
#if 0
QgsLayerTreeGroup* root = model->rootGroup();
QStringList layerIds = root->findLayerIds();
throw QgsMapServiceException( "Error", "Composer legend layerIds " + layerIds.join( " ," ) );
#endif
if ( legend->autoUpdateModel() )
{
QgsLegendModelV2* model = legend->modelV2();
model->setRootGroup( projectLayerTreeGroup() );
}
// if the legend has no map
// we will load all layers
const QgsComposerMap* map = legend->composerMap();
if ( !map )
{
QgsLayerTreeGroup* root = model->rootGroup();
QStringList layerIds = root->findLayerIds();
// foreach layer find in the layer tree
// load it if the layer id is not QgsMapLayerRegistry
foreach ( QString layerId, layerIds )
{
QgsMapLayer * layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( layer )
{
continue;
}

QgsLayerTreeLayer* nodeLayer = root->findLayer( layerId );
if ( !nodeLayer )
{
continue;
}
layer = nodeLayer->layer();
if ( !layer )
{
const QHash< QString, QDomElement > &projectLayerElements = mProjectParser->projectLayerElementsById();
QHash< QString, QDomElement >::const_iterator layerElemIt = projectLayerElements.find( layerId );
if ( layerElemIt != projectLayerElements.constEnd() )
{
layer = mProjectParser->createLayerFromElement( layerElemIt.value(), true );
}
}
QgsMapLayerRegistry::instance()->addMapLayer( layer );
}
legend->updateLegend();
}
legendList.push_back( legend );
continue;
}

6 comments on commit 7c73521

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Jun 20, 2015

Hi René-Luc, thanks for this fix. I gave it a try and does work indeed! There is one problem yet (maybe it is a consequence of the fix): if in the "LAYERS" parameter are not listed all the layers that are actually in the legend (when the legend is configured as "map=none") then the GetPrint request hangs.

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Jun 20, 2015

@rldhont

This comment has been minimized.

Copy link
Contributor Author

@rldhont rldhont replied Jun 24, 2015

Hi @gioman

I have tested with empty layers parameter and it works. I have all the fixed legend with an empty map. And it is the purpose of this fixe, it's to always have the legend even if the layer are not in the layers parameter.

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Jun 24, 2015

Hi @rldhont ok, let me check it, I may have overlooked something. cheers!

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Jun 30, 2015

@rldhont Hi, see this two services

http://193.136.216.94/lm211_tests/lizmap/www/index.php/view/

http://193.136.217.85/lm211_tests_master/lizmap/www/index.php/view/

the first uses qgis server 2.8.2 the second master. Both contain two copies of the same project, one has a layout with a legend set to map=map0 and the other set to map=none. On qgis server master when printing the project that has the layout with the legend set to map=none the pdf creation hangs if you don't have all the layers active (so if you don't pass in the url LAYER parameter the complete list of layers that are in the composer legend). Try:

http://193.136.217.85/lm211_tests_master/lizmap/www/index.php/lizmap/service/?repository=montpellier&project=prj290&SERVICE=WMS&VERSION=1.3&REQUEST=GetPrint&FORMAT=pdf&EXCEPTIONS=application/vnd.ogc.se_inimage&TRANSPARENT=true&SRS=EPSG:3763&DPI=300&TEMPLATE=test_layout_legend_map_none&map0:extent=-239795.308000505,-389632.89329657354,132713.64979690503,-9623.755140826455&map0:scale=2500000&map0:grid_interval_x=50000&map0:grid_interval_y=50000&map0:LAYERS=layer1&LAYERS=layer1

compared to:

http://193.136.217.85/lm211_tests_master/lizmap/www/index.php/lizmap/service/?repository=montpellier&project=prj290&SERVICE=WMS&VERSION=1.3&REQUEST=GetPrint&FORMAT=pdf&EXCEPTIONS=application/vnd.ogc.se_inimage&TRANSPARENT=true&SRS=EPSG:3763&DPI=300&TEMPLATE=test_layout_legend_map_none&map0:extent=-239795.308000505,-389632.89329657354,132713.64979690503,-9623.755140826455&map0:scale=2500000&map0:grid_interval_x=50000&map0:grid_interval_y=50000&map0:LAYERS=layer1,layer2&LAYERS=layer1,layer2

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Jul 1, 2015

@rldhont desperate to troubleshoot I have tested on qgis server on Windows (yikes) and as a matter of fact it works (but the request has to be done twice because the first time it crashes, only happens when map=none). Now I just need to understand why on Ubuntu server does not. Ideas?

Please sign in to comment.
You can’t perform that action at this time.