QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  bool allowMethod( QgsServerRequest::Method method ) const override
66  {
67  return method == QgsServerRequest::GetMethod;
68  }
69 
70  void executeRequest( const QgsServerRequest &request, QgsServerResponse &response,
71  const QgsProject *project ) override
72  {
73  const QgsWmsParameters parameters( QUrlQuery( request.url() ) );
74 
75  QString version = parameters.version();
76  if ( version.isEmpty() )
77  {
78  // WMTVER needs to be supported by WMS 1.1.1 for backwards
79  // compatibility with WMS 1.0.0
80  version = parameters.wmtver();
81  }
82 
83  // Set the default version
84  if ( version.isEmpty() || !parameters.versionIsValid( version ) )
85  {
86  version = mVersion;
87  }
88 
89  // Get the request
90  const QString req = parameters.request();
91  if ( req.isEmpty() )
92  {
94  QStringLiteral( "Please check the value of the REQUEST parameter" ), 501 );
95  }
96 
97  if ( ( mVersion.compare( QLatin1String( "1.1.1" ) ) == 0 \
98  && QSTR_COMPARE( req, "capabilities" ) )
99  || QSTR_COMPARE( req, "GetCapabilities" ) )
100  {
101  writeGetCapabilities( mServerIface, project, version, request, response, false );
102  }
103  else if ( QSTR_COMPARE( req, "GetProjectSettings" ) )
104  {
105  //getProjectSettings extends WMS 1.3.0 capabilities
106  version = QStringLiteral( "1.3.0" );
107  writeGetCapabilities( mServerIface, project, version, request, response, true );
108  }
109  else if ( QSTR_COMPARE( req, "GetMap" ) )
110  {
111  QString format = parameters.formatAsString();
112  if QSTR_COMPARE( format, "application/dxf" )
113  {
114  writeAsDxf( mServerIface, project, version, request, response );
115  }
116  else
117  {
118  writeGetMap( mServerIface, project, version, request, response );
119  }
120  }
121  else if ( QSTR_COMPARE( req, "GetFeatureInfo" ) )
122  {
123  writeGetFeatureInfo( mServerIface, project, version, request, response );
124  }
125  else if ( QSTR_COMPARE( req, "GetContext" ) )
126  {
127  writeGetContext( mServerIface, project, version, request, response );
128  }
129  else if ( QSTR_COMPARE( req, "GetSchemaExtension" ) )
130  {
131  writeGetSchemaExtension( mServerIface, version, request, response );
132  }
133  else if ( QSTR_COMPARE( req, "GetStyle" ) )
134  {
135  writeGetStyle( mServerIface, project, version, request, response );
136  }
137  else if ( QSTR_COMPARE( req, "GetStyles" ) )
138  {
139  writeGetStyles( mServerIface, project, version, request, response );
140  }
141  else if ( QSTR_COMPARE( req, "DescribeLayer" ) )
142  {
143  writeDescribeLayer( mServerIface, project, version, request, response );
144  }
145  else if ( QSTR_COMPARE( req, "GetLegendGraphic" ) || QSTR_COMPARE( req, "GetLegendGraphics" ) )
146  {
147  writeGetLegendGraphics( mServerIface, project, version, request, response );
148  }
149  else if ( QSTR_COMPARE( req, "GetPrint" ) )
150  {
151  writeGetPrint( mServerIface, project, version, request, response );
152  }
153  else
154  {
155  // Operation not supported
157  QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
158  }
159  }
160 
161  private:
162  QString mVersion;
163  QgsServerInterface *mServerIface = nullptr;
164  };
165 } // namespace QgsWms
166 
174 {
175  public:
176  void registerSelf( QgsServiceRegistry &registry, QgsServerInterface *serverIface ) override
177  {
178  QgsDebugMsg( QStringLiteral( "WMSModule::registerSelf called" ) );
179  registry.registerService( new QgsWms::Service( "1.3.0", serverIface ) );
180  }
181 };
182 
183 
184 // Entry points
186 {
187  static QgsWmsModule sModule;
188  return &sModule;
189 }
191 {
192  // Nothing to do
193 }
bool versionIsValid(const QString version) const
Returns true if version is valid, false otherwise.
void writeGetStyles(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetStyles response.
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
Module specialized for WMS service.
Definition: qgswms.cpp:173
void writeAsDxf(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void writeGetMap(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
Class defining the service module interface for QGIS server services.
QString version() const override
Definition: qgswms.cpp:63
Service(const QString &version, QgsServerInterface *serverIface)
Constructor for WMS service.
Definition: qgswms.cpp:57
void writeGetFeatureInfo(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetFeatureInfo response.
Method
HTTP Method (or equivalent) used for the request.
Exception class for WMS service exceptions.
void writeGetCapabilities(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response, bool projectSettings)
Output GetCapabilities response.
void executeRequest(const QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project) override
Execute the requests and set result in QgsServerRequest.
Definition: qgswms.cpp:70
QGISEXTERN void QGS_ServiceModule_Exit(QgsServiceModule *)
Definition: qgswms.cpp:190
Provides an interface to retrieve and manipulate WMS parameters received from the client...
#define QSTR_COMPARE(str, lit)
Definition: qgswms.cpp:36
void writeGetContext(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetContext response.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
OGC web service specialized for WMS.
Definition: qgswms.cpp:48
bool allowMethod(QgsServerRequest::Method method) const override
Returns true if the given method is supported for that service.
Definition: qgswms.cpp:65
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
void registerService(QgsService *service)
Register a service by its name and version.
void writeGetPrint(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetPrint response.
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
void writeGetStyle(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetStyle response.
Median cut implementation.
void registerSelf(QgsServiceRegistry &registry, QgsServerInterface *serverIface) override
Asks the module to register all provided services.
Definition: qgswms.cpp:176
QgsService Class defining interfaces for QGIS server services.
Definition: qgsservice.h:39
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
QgsServiceRegistry Class defining the registry manager for QGIS server services.
void writeGetSchemaExtension(QgsServerInterface *serverIface, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetSchemaExtension response.
#define QGISEXTERN
Definition: qgis.h:668
QString name() const override
Definition: qgswms.cpp:62
QString formatAsString() const
Returns FORMAT parameter as a string.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
QString wmtver() const
Returns WMTVER parameter or an empty string if not defined.
void writeDescribeLayer(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
QGISEXTERN QgsServiceModule * QGS_ServiceModule_Init()
Definition: qgswms.cpp:185