QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgswms.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswms.cpp
3  -------------------------
4  begin : December 20 , 2016
5  copyright : (C) 2007 by Marco Hugentobler ( parts from qgswmshandler)
6  (C) 2014 by Alessandro Pasotti ( parts from qgswmshandler)
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 
22 #include "qgsmodule.h"
23 #include "qgsdxfwriter.h"
24 #include "qgswmsserviceexception.h"
25 #include "qgswmsgetcapabilities.h"
26 #include "qgswmsgetmap.h"
27 #include "qgswmsgetstyles.h"
28 #include "qgswmsgetcontext.h"
30 #include "qgswmsgetprint.h"
31 #include "qgswmsgetfeatureinfo.h"
32 #include "qgswmsdescribelayer.h"
34 #include "qgswmsparameters.h"
35 
36 #define QSTR_COMPARE( str, lit )\
37  (str.compare( QLatin1String( lit ), Qt::CaseInsensitive ) == 0)
38 
39 namespace QgsWms
40 {
41 
48  class Service: public QgsService
49  {
50  public:
51 
57  Service( const QString &version, QgsServerInterface *serverIface )
58  : mVersion( version )
59  , mServerIface( serverIface )
60  {}
61 
62  QString name() const override { return QStringLiteral( "WMS" ); }
63  QString version() const override { return mVersion; }
64 
65  void executeRequest( const QgsServerRequest &request, QgsServerResponse &response,
66  const QgsProject *project ) override
67  {
68  const QgsWmsParameters parameters( QUrlQuery( request.url() ) );
69 
70  QString version = parameters.version();
71  if ( version.isEmpty() )
72  {
73  // WMTVER needs to be supported by WMS 1.1.1 for backwards
74  // compatibility with WMS 1.0.0
75  version = parameters.wmtver();
76  }
77 
78  // Set the default version
79  if ( version.isEmpty() || !parameters.versionIsValid( version ) )
80  {
81  version = mVersion;
82  }
83 
84  // Get the request
85  const QString req = parameters.request();
86  if ( req.isEmpty() )
87  {
89  QStringLiteral( "Please add or check the value of the REQUEST parameter" ), 501 );
90  }
91 
92  if ( ( mVersion.compare( QLatin1String( "1.1.1" ) ) == 0 \
93  && QSTR_COMPARE( req, "capabilities" ) )
94  || QSTR_COMPARE( req, "GetCapabilities" ) )
95  {
96  writeGetCapabilities( mServerIface, project, version, request, response, false );
97  }
98  else if ( QSTR_COMPARE( req, "GetProjectSettings" ) )
99  {
100  //getProjectSettings extends WMS 1.3.0 capabilities
101  version = QStringLiteral( "1.3.0" );
102  writeGetCapabilities( mServerIface, project, version, request, response, true );
103  }
104  else if ( QSTR_COMPARE( req, "GetMap" ) )
105  {
106  QString format = parameters.formatAsString();
107  if QSTR_COMPARE( format, "application/dxf" )
108  {
109  writeAsDxf( mServerIface, project, version, request, response );
110  }
111  else
112  {
113  writeGetMap( mServerIface, project, version, request, response );
114  }
115  }
116  else if ( QSTR_COMPARE( req, "GetFeatureInfo" ) )
117  {
118  writeGetFeatureInfo( mServerIface, project, version, request, response );
119  }
120  else if ( QSTR_COMPARE( req, "GetContext" ) )
121  {
122  writeGetContext( mServerIface, project, version, request, response );
123  }
124  else if ( QSTR_COMPARE( req, "GetSchemaExtension" ) )
125  {
126  writeGetSchemaExtension( response );
127  }
128  else if ( QSTR_COMPARE( req, "GetStyle" ) )
129  {
130  writeGetStyle( mServerIface, project, version, request, response );
131  }
132  else if ( QSTR_COMPARE( req, "GetStyles" ) )
133  {
134  writeGetStyles( mServerIface, project, version, request, response );
135  }
136  else if ( QSTR_COMPARE( req, "DescribeLayer" ) )
137  {
138  writeDescribeLayer( mServerIface, project, version, request, response );
139  }
140  else if ( QSTR_COMPARE( req, "GetLegendGraphic" ) || QSTR_COMPARE( req, "GetLegendGraphics" ) )
141  {
142  writeGetLegendGraphics( mServerIface, project, version, request, response );
143  }
144  else if ( QSTR_COMPARE( req, "GetPrint" ) )
145  {
146  if ( mServerIface->serverSettings() && mServerIface->serverSettings()->getPrintDisabled() )
147  {
148  // GetPrint has been disabled
149  QgsDebugMsg( QStringLiteral( "WMS GetPrint request called, but it has been disabled." ) );
151  QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
152  }
153  writeGetPrint( mServerIface, project, version, request, response );
154  }
155  else
156  {
157  // Operation not supported
159  QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
160  }
161  }
162 
163  private:
164  QString mVersion;
165  QgsServerInterface *mServerIface = nullptr;
166  };
167 } // namespace QgsWms
168 
176 {
177  public:
178  void registerSelf( QgsServiceRegistry &registry, QgsServerInterface *serverIface ) override
179  {
180  QgsDebugMsg( QStringLiteral( "WMSModule::registerSelf called" ) );
181  registry.registerService( new QgsWms::Service( "1.3.0", serverIface ) );
182  }
183 };
184 
185 
186 // Entry points
188 {
189  static QgsWmsModule sModule;
190  return &sModule;
191 }
193 {
194  // Nothing to do
195 }
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
bool getPrintDisabled() const
Returns true if WMS GetPrint request is disabled and the project's reading flag QgsProject::ReadFlag:...
Class defining the service module interface for QGIS server services.
QgsServiceRegistry Class defining the registry manager for QGIS server services.
void registerService(QgsService *service)
Register a service by its name and version.
QgsService Class defining interfaces for QGIS server services.
Definition: qgsservice.h:40
Module specialized for WMS service.
Definition: qgswms.cpp:176
void registerSelf(QgsServiceRegistry &registry, QgsServerInterface *serverIface) override
Asks the module to register all provided services.
Definition: qgswms.cpp:178
Exception class for WMS service exceptions.
Provides an interface to retrieve and manipulate WMS parameters received from the client.
QString formatAsString() const
Returns FORMAT parameter as a string.
bool versionIsValid(const QString version) const
Returns true if version is valid, false otherwise.
QString wmtver() const
Returns WMTVER parameter or an empty string if not defined.
OGC web service specialized for WMS.
Definition: qgswms.cpp:49
Service(const QString &version, QgsServerInterface *serverIface)
Constructor for WMS service.
Definition: qgswms.cpp:57
void executeRequest(const QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project) override
Execute the requests and set result in QgsServerRequest.
Definition: qgswms.cpp:65
QString version() const override
Definition: qgswms.cpp:63
QString name() const override
Definition: qgswms.cpp:62
Median cut implementation.
void writeDescribeLayer(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
void writeGetStyles(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetStyles response.
void writeGetCapabilities(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response, bool projectSettings)
Output GetCapabilities response.
void writeAsDxf(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
void writeGetStyle(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetStyle response.
void writeGetSchemaExtension(QgsServerResponse &response)
Output GetSchemaExtension response.
void writeGetFeatureInfo(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetFeatureInfo response.
void writeGetMap(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
void writeGetPrint(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetPrint response.
void writeGetContext(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetContext response.
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
#define QGISEXTERN
Definition: qgis.h:816
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
#define QSTR_COMPARE(str, lit)
Definition: qgswms.cpp:36
QGISEXTERN void QGS_ServiceModule_Exit(QgsServiceModule *)
Definition: qgswms.cpp:192
QGISEXTERN QgsServiceModule * QGS_ServiceModule_Init()
Definition: qgswms.cpp:187