QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgswmsutils.h"
23 #include "qgswmsrenderer.h"
24 
25 #include <QImage>
26 
27 namespace QgsWms
28 {
29 
30  void writeGetLegendGraphics( QgsServerInterface *serverIface, const QgsProject *project,
31  const QString &version, const QgsServerRequest &request,
32  QgsServerResponse &response )
33  {
34  Q_UNUSED( version );
35 
36  QgsServerRequest::Parameters params = request.parameters();
37  QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );
38 
39  QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
40 
41  // Get cached image
42  QgsAccessControl *accessControl = nullptr;
43  QgsServerCacheManager *cacheManager = nullptr;
44 #ifdef HAVE_SERVER_PYTHON_PLUGINS
45  accessControl = serverIface->accessControls();
46  cacheManager = serverIface->cacheManager();
47 #endif
48  if ( cacheManager )
49  {
50  ImageOutputFormat outputFormat = parseImageFormat( format );
51  QString saveFormat;
52  QString contentType;
53  switch ( outputFormat )
54  {
55  case PNG:
56  case PNG8:
57  case PNG16:
58  case PNG1:
59  contentType = "image/png";
60  saveFormat = "PNG";
61  break;
62  case JPEG:
63  contentType = "image/jpeg";
64  saveFormat = "JPEG";
65  break;
66  default:
67  throw QgsServiceException( "InvalidFormat",
68  QString( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
69  break;
70  }
71 
72  QImage image;
73  QByteArray content = cacheManager->getCachedImage( project, request, accessControl );
74  if ( !content.isEmpty() && image.loadFromData( content ) )
75  {
76  response.setHeader( QStringLiteral( "Content-Type" ), contentType );
77  image.save( response.io(), qPrintable( saveFormat ) );
78  return;
79  }
80  }
81 
82  QgsRenderer renderer( serverIface, project, wmsParameters );
83 
84  std::unique_ptr<QImage> result( renderer.getLegendGraphics() );
85 
86  if ( result )
87  {
88  writeImage( response, *result, format, renderer.getImageQuality() );
89  if ( cacheManager )
90  {
91  QByteArray content = response.data();
92  if ( !content.isEmpty() )
93  cacheManager->setCachedImage( &content, project, request, accessControl );
94  }
95  }
96  else
97  {
98  throw QgsServiceException( QStringLiteral( "UnknownError" ),
99  QStringLiteral( "Failed to compute GetLegendGraphics image" ) );
100  }
101  }
102 
103 
104 } // namespace QgsWms
105 
106 
107 
108 
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...
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
int getImageQuality() const
Returns the image quality to use for getMap request.
Exception class for WMS service exceptions.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
Provides an interface to retrieve and manipulate WMS parameters received from the client...
QImage * getLegendGraphics()
Returns the map legend as an image (or a null pointer in case of error).
A helper class that centralizes caches accesses given by all the server cache filter plugins...
Reads and writes project states.
Definition: qgsproject.h:89
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
Median cut implementation.
virtual QByteArray data() const =0
Gets the data written so far.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
Map renderer for WMS requests.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
A helper class that centralizes restrictions given by all the access control filter plugins...
ImageOutputFormat
Supported image output format.
Definition: qgswmsutils.h:39
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
Definition: qgswmsutils.cpp:75
QMap< QString, QString > Parameters
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.