QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #ifdef HAVE_SERVER_PYTHON_PLUGINS
43  QgsAccessControl *accessControl = serverIface->accessControls();
44  QgsServerCacheManager *cacheManager = serverIface->cacheManager();
45  if ( cacheManager )
46  {
47  ImageOutputFormat outputFormat = parseImageFormat( format );
48  QString saveFormat;
49  QString contentType;
50  switch ( outputFormat )
51  {
52  case PNG:
53  case PNG8:
54  case PNG16:
55  case PNG1:
56  contentType = "image/png";
57  saveFormat = "PNG";
58  break;
59  case JPEG:
60  contentType = "image/jpeg";
61  saveFormat = "JPEG";
62  break;
63  default:
64  throw QgsServiceException( "InvalidFormat",
65  QString( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
66  break;
67  }
68 
69  QImage image;
70  QByteArray content = cacheManager->getCachedImage( project, request, accessControl );
71  if ( !content.isEmpty() && image.loadFromData( content ) )
72  {
73  response.setHeader( QStringLiteral( "Content-Type" ), contentType );
74  image.save( response.io(), qPrintable( saveFormat ) );
75  return;
76  }
77  }
78 #endif
79 
80  QgsRenderer renderer( serverIface, project, wmsParameters );
81 
82  std::unique_ptr<QImage> result( renderer.getLegendGraphics() );
83 
84  if ( result )
85  {
86  writeImage( response, *result, format, renderer.getImageQuality() );
87 #ifdef HAVE_SERVER_PYTHON_PLUGINS
88  if ( cacheManager )
89  {
90  QByteArray content = response.data();
91  if ( !content.isEmpty() )
92  cacheManager->setCachedImage( &content, project, request, accessControl );
93  }
94 #endif
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.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.
Exception class for WMS service exceptions.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
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:40
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
int getImageQuality() const
Returns the image quality to use for getMap request.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
Definition: qgswmsutils.cpp:75
QMap< QString, QString > Parameters