QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgswmsgetlegendgraphics.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswmsgetlegendgraphics.cpp
3  -------------------------
4  begin : December 20 , 2016
5  copyright : (C) 2007 by Marco Hugentobler (original code)
6  (C) 2014 by Alessandro Pasotti (original code)
7  (C) 2016 by David Marteau
8  email : marco dot hugentobler at karto dot baug dot ethz dot ch
9  a dot pasotti at itopen dot it
10  david dot marteau at 3liz dot com
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 #include "qgslayertree.h"
22 #include "qgslegendrenderer.h"
23 #include "qgsvectorlayer.h"
25 #include "qgssymbollayerutils.h"
26 #include "qgsmaplayerlegend.h"
27 
28 #include "qgswmsutils.h"
29 #include "qgswmsserviceexception.h"
31 #include "qgswmsrenderer.h"
32 
33 #include <QImage>
34 #include <QJsonObject>
35 #include <QJsonDocument>
36 
37 namespace QgsWms
38 {
39  void writeGetLegendGraphics( QgsServerInterface *serverIface, const QgsProject *project,
40  const QString &, const QgsServerRequest &request,
41  QgsServerResponse &response )
42  {
43  // get parameters from query
44  QgsWmsParameters parameters( QUrlQuery( request.url() ) );
45 
46  // check parameters validity
47  // FIXME fail with png + mode
48  checkParameters( parameters );
49 
50  // init render context
51  QgsWmsRenderContext context( project, serverIface );
54  context.setParameters( parameters );
55 
56  // get the requested output format
57  QgsWmsParameters::Format format = parameters.format();
58 
59  // parameters.format() returns NONE if the requested format is image/png with a
60  // mode (e.g. image/png;mode=16bit), so in that case we use parseImageFormat to
61  // give the requested format another chance
62 
63  QString imageSaveFormat;
64  QString imageContentType;
65  if ( format == QgsWmsParameters::Format::PNG )
66  {
67  imageContentType = "image/png";
68  imageSaveFormat = "PNG";
69  }
70  else if ( format == QgsWmsParameters::Format::JPG )
71  {
72  imageContentType = "image/jpeg";
73  imageSaveFormat = "JPEG";
74  }
75  else if ( format == QgsWmsParameters::Format::NONE )
76  {
77  switch ( parseImageFormat( parameters.formatAsString() ) )
78  {
79  case PNG:
80  case PNG8:
81  case PNG16:
82  case PNG1:
84  imageContentType = "image/png";
85  imageSaveFormat = "PNG";
86  break;
87  default:
88  break;
89  }
90  }
91 
92  if ( format == QgsWmsParameters::Format::NONE )
93  {
95  QStringLiteral( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( parameters.formatAsString() ) );
96  }
97 
98  // Get cached image
99 #ifdef HAVE_SERVER_PYTHON_PLUGINS
100  QgsAccessControl *accessControl = serverIface->accessControls();
101  QgsServerCacheManager *cacheManager = serverIface->cacheManager();
102  if ( cacheManager && !imageSaveFormat.isEmpty() )
103  {
104  QImage image;
105  QByteArray content = cacheManager->getCachedImage( project, request, accessControl );
106  if ( !content.isEmpty() && image.loadFromData( content ) )
107  {
108  response.setHeader( QStringLiteral( "Content-Type" ), imageContentType );
109  image.save( response.io(), qPrintable( imageSaveFormat ) );
110  return;
111  }
112  }
113 #endif
114  QgsRenderer renderer( context );
115 
116  // retrieve legend settings and model
117  std::unique_ptr<QgsLayerTree> tree( layerTree( context ) );
118  std::unique_ptr<QgsLayerTreeModel> model( legendModel( context, *tree.get() ) );
119 
120  // rendering
121  if ( format == QgsWmsParameters::Format::JSON )
122  {
123  QJsonObject result;
124  if ( !parameters.rule().isEmpty() )
125  {
127  QStringLiteral( "RULE cannot be used with JSON format" ) );
128  }
129  else
130  {
131  result = renderer.getLegendGraphicsAsJson( *model.get() );
132  }
133  tree->clear();
134  response.setHeader( QStringLiteral( "Content-Type" ), parameters.formatAsString() );
135  QJsonDocument doc( result );
136  response.write( doc.toJson( QJsonDocument::Compact ) );
137  }
138  else
139  {
140  std::unique_ptr<QImage> result;
141  if ( !parameters.rule().isEmpty() )
142  {
143  QgsLayerTreeModelLegendNode *node = legendNode( parameters.rule(), *model.get() );
144  if ( ! node )
145  {
146  throw QgsException( QStringLiteral( "Could not get a legend node for the requested RULE" ) );
147  }
148  result.reset( renderer.getLegendGraphics( *node ) );
149  }
150  else
151  {
152  result.reset( renderer.getLegendGraphics( *model.get() ) );
153  }
154  tree->clear();
155  if ( result )
156  {
157  writeImage( response, *result, parameters.formatAsString(), context.imageQuality() );
158 #ifdef HAVE_SERVER_PYTHON_PLUGINS
159  if ( cacheManager )
160  {
161  QByteArray content = response.data();
162  if ( !content.isEmpty() )
163  cacheManager->setCachedImage( &content, project, request, accessControl );
164  }
165 #endif
166  }
167  else
168  {
169  throw QgsException( QStringLiteral( "Failed to compute GetLegendGraphics image" ) );
170  }
171  }
172  }
173 
174  void checkParameters( QgsWmsParameters &parameters )
175  {
176  if ( parameters.allLayersNickname().isEmpty() )
177  {
179  parameters[QgsWmsParameter::LAYERS] );
180  }
181 
182  if ( parameters.format() == QgsWmsParameters::Format::NONE )
183  {
185  parameters[QgsWmsParameter::FORMAT] );
186  }
187 
188  if ( ! parameters.bbox().isEmpty() && !parameters.rule().isEmpty() )
189  {
191  QStringLiteral( "BBOX parameter cannot be combined with RULE." ) );
192  }
193 
194  if ( ! parameters.bbox().isEmpty() && parameters.bboxAsRectangle().isEmpty() )
195  {
197  parameters[QgsWmsParameter::BBOX] );
198  }
199  // If we have a contextual legend (BBOX is set)
200  // make sure (SRC)WIDTH and (SRC)HEIGHT are set, default to 800px width
201  // height is calculated from that value, respecting the aspect
202  if ( ! parameters.bbox().isEmpty() )
203  {
204  // Calculate ratio from bbox
205  QgsRectangle bbox { parameters.bboxAsRectangle() };
206  QString crs = parameters.crs();
207  if ( crs.compare( QStringLiteral( "CRS:84" ), Qt::CaseInsensitive ) == 0 )
208  {
209  bbox.invert();
210  }
212  if ( parameters.versionAsNumber() >= QgsProjectVersion( 1, 3, 0 ) &&
214  {
215  bbox.invert();
216  }
217  const double ratio { bbox.width() / bbox.height() };
218  int defaultHeight { static_cast<int>( 800 / ratio ) };
219  if ( parameters.width().isEmpty() && parameters.srcWidth().isEmpty() )
220  {
221  parameters.set( QgsWmsParameter::SRCWIDTH, 800 );
222  }
223  if ( parameters.height().isEmpty() && parameters.srcHeight().isEmpty() )
224  {
225  parameters.set( QgsWmsParameter::SRCHEIGHT, defaultHeight );
226  }
227  }
228  }
229 
231  {
232 
233  const QgsWmsParameters parameters = context.parameters();
234  std::unique_ptr<QgsLayerTreeModel> model( new QgsLayerTreeModel( &tree ) );
235  std::unique_ptr<QgsMapSettings> mapSettings;
236 
237  if ( context.scaleDenominator() > 0 )
238  {
239  model->setLegendFilterByScale( context.scaleDenominator() );
240  }
241 
242  // content based legend
243  if ( ! parameters.bbox().isEmpty() )
244  {
245  mapSettings = qgis::make_unique<QgsMapSettings>();
246  mapSettings->setOutputSize( context.mapSize() );
247  // Inverted axis?
248  QgsRectangle bbox { parameters.bboxAsRectangle() };
249  QString crs = parameters.crs();
250  if ( crs.compare( QStringLiteral( "CRS:84" ), Qt::CaseInsensitive ) == 0 )
251  {
252  bbox.invert();
253  }
255  if ( parameters.versionAsNumber() >= QgsProjectVersion( 1, 3, 0 ) &&
257  {
258  bbox.invert();
259  }
260  mapSettings->setDestinationCrs( outputCrs );
261  mapSettings->setExtent( bbox );
262  QgsRenderer renderer( context );
263  QList<QgsMapLayer *> layers = context.layersToRender();
264  renderer.configureLayers( layers, mapSettings.get() );
265  mapSettings->setLayers( context.layersToRender() );
266  model->setLegendFilterByMap( mapSettings.get() );
267  }
268 
269  // if legend is not based on rendering rules
270  if ( parameters.rule().isEmpty() )
271  {
272  QList<QgsLayerTreeNode *> children = tree.children();
273  QString ruleLabel = parameters.ruleLabel();
274  for ( QgsLayerTreeNode *node : children )
275  {
276  if ( ! QgsLayerTree::isLayer( node ) )
277  continue;
278 
279  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
280 
281  // layer titles - hidden or not
283  // rule item titles
284  if ( !parameters.ruleLabelAsBool() )
285  {
286  for ( QgsLayerTreeModelLegendNode *legendNode : model->layerLegendNodes( nodeLayer ) )
287  {
288  // empty string = no override, so let's use one space
289  legendNode->setUserLabel( QStringLiteral( " " ) );
290  }
291  }
292  else if ( ruleLabel.compare( QStringLiteral( "AUTO" ), Qt::CaseInsensitive ) == 0 )
293  {
294  for ( QgsLayerTreeModelLegendNode *legendNode : model->layerLegendNodes( nodeLayer ) )
295  {
296  //clearing label for single symbol
299  }
300  }
301  }
302  }
303 
304  return model.release();
305  }
306 
308  {
309  std::unique_ptr<QgsLayerTree> tree( new QgsLayerTree() );
310 
311  QList<QgsVectorLayerFeatureCounter *> counters;
312  for ( QgsMapLayer *ml : context.layersToRender() )
313  {
314  QgsLayerTreeLayer *lt = tree->addLayer( ml );
315  lt->setUseLayerName( false ); // do not modify underlying layer
316 
317  // name
318  if ( !ml->title().isEmpty() )
319  lt->setName( ml->title() );
320 
321  // show feature count
322  const bool showFeatureCount = context.parameters().showFeatureCountAsBool();
323  const QString property = QStringLiteral( "showFeatureCount" );
324  lt->setCustomProperty( property, showFeatureCount );
325 
326  if ( ml->type() != QgsMapLayerType::VectorLayer || !showFeatureCount )
327  continue;
328 
329  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
331  if ( !counter )
332  continue;
333 
334  counters.append( counter );
335  }
336 
337  for ( QgsVectorLayerFeatureCounter *counter : counters )
338  {
339  counter->waitForFinished();
340  }
341 
342  return tree.release();
343  }
344 
346  {
347  for ( QgsLayerTreeLayer *layer : model.rootGroup()->findLayers() )
348  {
349  for ( QgsLayerTreeModelLegendNode *node : model.layerLegendNodes( layer ) )
350  {
351  if ( node->data( Qt::DisplayRole ).toString().compare( rule ) == 0 )
352  return node;
353  }
354  }
355  return nullptr;
356  }
357 } // namespace QgsWms
A helper class that centralizes restrictions given by all the access control filter plugins.
This class represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
Defines a QGIS exception class.
Definition: qgsexception.h:35
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
void setName(const QString &n) override
Sets the layer's name.
void setUseLayerName(bool use=true)
Uses the layer's name if use is true, or the name manually set if false.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual void setEmbeddedInParent(bool embedded)
virtual void setUserLabel(const QString &userLabel)
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
This class is a base class for nodes in a layer tree.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:33
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsLegendStyle::Style style)
Sets the style of a node.
@ Subgroup
Legend subgroup title.
@ Hidden
Special style, item is hidden including margins around.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
A class to describe the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
A rectangle specified with double values.
Definition: qgsrectangle.h:42
bool isEmpty() const
Returns true if the rectangle is empty.
Definition: qgsrectangle.h:437
A helper class that centralizes caches accesses given by all the server cache filter plugins.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual QByteArray data() const =0
Gets the data written so far.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual QIODevice * io()=0
Returns the underlying QIODevice.
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
Counts the features in a QgsVectorLayer in task.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
Exception thrown in case of malformed request.
Map renderer for WMS requests.
void configureLayers(QList< QgsMapLayer * > &layers, QgsMapSettings *settings=nullptr)
Configures layers for rendering optionally considering the map settings.
QJsonObject getLegendGraphicsAsJson(QgsLayerTreeModel &model)
Returns the map legend as a JSON object.
QImage * getLegendGraphics(QgsLayerTreeModel &model)
Returns the map legend as an image (or nullptr in case of error).
Provides an interface to retrieve and manipulate WMS parameters received from the client.
QString rule() const
Returns RULE parameter or an empty string if none is defined.
QStringList allLayersNickname() const
Returns nickname of layers found in LAYER and LAYERS parameters.
QString formatAsString() const
Returns FORMAT parameter as a string.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
QString ruleLabel() const
Returns RULELABEL parameter or an empty string if none is defined.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
void set(QgsWmsParameter::Name name, const QVariant &value)
Sets a parameter value thanks to its name.
QString srcHeight() const
Returns SRCHEIGHT parameter or an empty string if not defined.
bool showFeatureCountAsBool() const
Returns SHOWFEATURECOUNT as a bool.
QString bbox() const
Returns BBOX if defined or an empty string.
Format format() const
Returns format.
bool ruleLabelAsBool() const
Returns RULELABEL as a bool.
QString srcWidth() const
Returns SRCWIDTH parameter or an empty string if not defined.
QString height() const
Returns HEIGHT parameter or an empty string if not defined.
QString crs() const
Returns CRS or an empty string if none is defined.
bool layerTitleAsBool() const
Returns LAYERTITLE as a bool or its default value if not defined.
Format
Output format for the response.
QString width() const
Returns WIDTH parameter or an empty string if not defined.
Rendering context for the WMS renderer.
QSize mapSize(bool aspectRatio=true) const
Returns the size (in pixels) of the map to render, according to width and height WMS parameters as we...
void setParameters(const QgsWmsParameters &parameters)
Sets WMS parameters.
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
QgsWmsParameters parameters() const
Returns WMS parameters.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
Median cut implementation.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
Definition: qgswmsutils.cpp:70
void checkParameters(QgsWmsParameters &parameters)
checkParameters checks request parameters and sets SRCHEIGHT and SRCWIDTH to default values in case B...
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs