QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgswmsdescribelayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswmsdescribelayer.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
22#include "qgswmsdescribelayer.h"
23
24#include "qgsproject.h"
26#include "qgswmsrequest.h"
28#include "qgswmsutils.h"
29
30namespace QgsWms
31{
32
33 void writeDescribeLayer( QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response )
34 {
35 const QDomDocument doc = describeLayer( serverIface, project, request );
36 response.setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/xml; charset=utf-8" ) );
37 response.write( doc.toByteArray() );
38 }
39
40 // DescribeLayer is defined for WMS1.1.1/SLD1.0 and in WMS 1.3.0 SLD Extension
41 QDomDocument describeLayer( QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request )
42 {
43 const QgsServerRequest::Parameters parameters = request.parameters();
44
45 if ( !parameters.contains( QStringLiteral( "SLD_VERSION" ) ) )
46 {
47 throw QgsServiceException( QStringLiteral( "MissingParameterValue" ), QStringLiteral( "SLD_VERSION is mandatory for DescribeLayer operation" ), 400 );
48 }
49 if ( parameters[QStringLiteral( "SLD_VERSION" )] != QLatin1String( "1.1.0" ) )
50 {
51 throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "SLD_VERSION = %1 is not supported" ).arg( parameters[QStringLiteral( "SLD_VERSION" )] ), 400 );
52 }
53
54 if ( !parameters.contains( QStringLiteral( "LAYERS" ) ) && !parameters.contains( QStringLiteral( "LAYER" ) ) )
55 {
56 throw QgsServiceException( QStringLiteral( "MissingParameterValue" ), QStringLiteral( "LAYERS or LAYER is mandatory for DescribeLayer operation" ), 400 );
57 }
58
59 QStringList layersList;
60
61 if ( parameters.contains( QStringLiteral( "LAYERS" ) ) )
62 {
63 layersList = parameters[QStringLiteral( "LAYERS" )].split( ',', Qt::SkipEmptyParts );
64 }
65 else
66 {
67 layersList = parameters[QStringLiteral( "LAYER" )].split( ',', Qt::SkipEmptyParts );
68 }
69 if ( layersList.isEmpty() )
70 {
71 throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Layers is empty" ), 400 );
72 }
73 QDomDocument myDocument = QDomDocument();
74
75 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
76 myDocument.appendChild( header );
77
78 // Create the root element
79 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "DescribeLayerResponse" ) );
80 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/DescribeLayer.xsd" ) );
81 root.setAttribute( QStringLiteral( "xmlns:ows" ), QStringLiteral( "http://www.opengis.net/ows" ) );
82 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
83 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
84 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
85 myDocument.appendChild( root );
86
87 // store the Version element
88 QDomElement versionNode = myDocument.createElement( QStringLiteral( "Version" ) );
89 versionNode.appendChild( myDocument.createTextNode( QStringLiteral( "1.1.0" ) ) );
90 root.appendChild( versionNode );
91
92 // get the wms service url defined in project or keep the one from the
93 // request url
94 const QString wmsHrefString = serviceUrl( request, project, *serverIface->serverSettings() ).toString();
95
96 // get the wfs service url defined in project or take the same as the
97 // wms service url
98 QString wfsHrefString = QgsServerProjectUtils::wfsServiceUrl( *project, request, *serverIface->serverSettings() );
99 if ( wfsHrefString.isEmpty() )
100 {
101 wfsHrefString = wmsHrefString;
102 }
103
104 // get the wcs service url defined in project or take the same as the
105 // wms service url
106 QString wcsHrefString = QgsServerProjectUtils::wcsServiceUrl( *project, request, *serverIface->serverSettings() );
107 if ( wcsHrefString.isEmpty() )
108 {
109 wcsHrefString = wmsHrefString;
110 }
111
112 // access control
113#ifdef HAVE_SERVER_PYTHON_PLUGINS
114 QgsAccessControl *accessControl = serverIface->accessControls();
115#else
116 ( void ) serverIface;
117#endif
118 // Use layer ids
119 const bool useLayerIds = QgsServerProjectUtils::wmsUseLayerIds( *project );
120 // WMS restricted layers
121 const QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project );
122 // WFS layers
123 const QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
124 // WCS layers
125 const QStringList wcsLayerIds = QgsServerProjectUtils::wcsLayerIds( *project );
126
127 for ( QgsMapLayer *layer : project->mapLayers() )
128 {
129 QString name = layer->name();
130 if ( useLayerIds )
131 name = layer->id();
132 else if ( !layer->serverProperties()->shortName().isEmpty() )
133 name = layer->serverProperties()->shortName();
134
135 if ( !layersList.contains( name ) )
136 {
137 continue;
138 }
139
140 //unpublished layer
141 if ( restrictedLayers.contains( layer->name() ) )
142 {
143 throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
144 }
145
146#ifdef HAVE_SERVER_PYTHON_PLUGINS
147 if ( accessControl && !accessControl->layerReadPermission( layer ) )
148 {
149 throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
150 }
151#endif
152
153 // Create the NamedLayer element
154 QDomElement layerNode = myDocument.createElement( QStringLiteral( "LayerDescription" ) );
155 root.appendChild( layerNode );
156
157 // store the owsType element
158 QDomElement typeNode = myDocument.createElement( QStringLiteral( "owsType" ) );
159 // store the se:OnlineResource element
160 QDomElement oResNode = myDocument.createElement( QStringLiteral( "se:OnlineResource" ) );
161 oResNode.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
162 // store the TypeName element
163 QDomElement nameNode = myDocument.createElement( QStringLiteral( "TypeName" ) );
164 switch ( layer->type() )
165 {
167 {
168 typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wfs" ) ) );
169
170 if ( wfsLayerIds.indexOf( layer->id() ) != -1 )
171 {
172 oResNode.setAttribute( QStringLiteral( "xlink:href" ), wfsHrefString );
173 }
174
175 // store the se:FeatureTypeName element
176 QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:FeatureTypeName" ) );
177 typeNameNode.appendChild( myDocument.createTextNode( name ) );
178 nameNode.appendChild( typeNameNode );
179 break;
180 }
182 {
183 typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wcs" ) ) );
184
185 if ( wcsLayerIds.indexOf( layer->id() ) != -1 )
186 {
187 oResNode.setAttribute( QStringLiteral( "xlink:href" ), wcsHrefString );
188 }
189
190 // store the se:CoverageTypeName element
191 QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:CoverageTypeName" ) );
192 typeNameNode.appendChild( myDocument.createTextNode( name ) );
193 nameNode.appendChild( typeNameNode );
194 break;
195 }
196
204 break;
205 }
206 layerNode.appendChild( typeNode );
207 layerNode.appendChild( oResNode );
208 layerNode.appendChild( nameNode );
209 }
210
211 return myDocument;
212 }
213
214} // namespace QgsWms
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:198
@ Plugin
Plugin based layer.
Definition qgis.h:193
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:199
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:196
@ Vector
Vector layer.
Definition qgis.h:191
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:197
A helper class that centralizes restrictions given by all the access control filter plugins.
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
Base class for all map layer types.
Definition qgsmaplayer.h:80
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Defines interfaces exposed by QGIS Server and made available to plugins.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
static QString wcsServiceUrl(const QgsProject &project, const QgsServerRequest &request=QgsServerRequest(), const QgsServerSettings &settings=QgsServerSettings())
Returns the WCS service url.
static bool wmsUseLayerIds(const QgsProject &project)
Returns if layer ids are used as name in WMS.
static QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
static QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
static QString wfsServiceUrl(const QgsProject &project, const QgsServerRequest &request=QgsServerRequest(), const QgsServerSettings &settings=QgsServerSettings())
Returns the WFS service url.
static QStringList wcsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WCS.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
QMap< QString, QString > Parameters
Defines the response interface passed to QgsService.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
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...
Exception thrown when data access violates access controls.
Exception class for WMS service exceptions.
Defines request interfaces passed to WMS service.
Median cut implementation.
QDomDocument describeLayer(QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request)
DescribeLayer is defined for WMS1.1.1/SLD1.0 and in WMS 1.3.0 SLD Extension.
void writeDescribeLayer(QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response)
Output GetMap response in DXF format.
QUrl serviceUrl(const QgsServerRequest &request, const QgsProject *project, const QgsServerSettings &settings)
Returns WMS service URL.