QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgswcsgetcapabilities.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswcsgecapabilities.cpp
3 -------------------------
4 begin : January 16 , 2017
5 copyright : (C) 2013 by René-Luc D'Hont ( parts from qgswcsserver )
6 (C) 2017 by David Marteau
7 email : rldhont at 3liz dot com
8 david dot marteau at 3liz dot com
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19#include "qgswcsutils.h"
22
23#include "qgsproject.h"
24#include "qgsrasterlayer.h"
25
26namespace QgsWcs
27{
28
32 void writeGetCapabilities( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response )
33 {
34#ifdef HAVE_SERVER_PYTHON_PLUGINS
35 QgsAccessControl *accessControl = serverIface->accessControls();
36#endif
37 QDomDocument doc;
38 const QDomDocument *capabilitiesDocument = nullptr;
39
40#ifdef HAVE_SERVER_PYTHON_PLUGINS
41 QgsServerCacheManager *cacheManager = serverIface->cacheManager();
42 if ( cacheManager && cacheManager->getCachedDocument( &doc, project, request, accessControl ) )
43 {
44 capabilitiesDocument = &doc;
45 }
46 else //capabilities xml not in cache. Create a new one
47 {
48 doc = createGetCapabilitiesDocument( serverIface, project, version, request );
49
50 if ( cacheManager )
51 {
52 cacheManager->setCachedDocument( &doc, project, request, accessControl );
53 }
54 capabilitiesDocument = &doc;
55 }
56#else
57 doc = createGetCapabilitiesDocument( serverIface, project, version, request );
58 capabilitiesDocument = &doc;
59#endif
60 response.setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/xml; charset=utf-8" ) );
61 response.write( capabilitiesDocument->toByteArray() );
62 }
63
64
65 QDomDocument createGetCapabilitiesDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request )
66 {
67 Q_UNUSED( version )
68
69 QDomDocument doc;
70
71 //wcs:WCS_Capabilities element
72 QDomElement wcsCapabilitiesElement = doc.createElement( QStringLiteral( "WCS_Capabilities" ) /*wcs:WCS_Capabilities*/ );
73 wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns" ), WCS_NAMESPACE );
74 wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
75 wcsCapabilitiesElement.setAttribute( QStringLiteral( "xsi:schemaLocation" ), WCS_NAMESPACE + " http://schemas.opengis.net/wcs/1.0.0/wcsCapabilities.xsd" );
76 wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:gml" ), GML_NAMESPACE );
77 wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
78 wcsCapabilitiesElement.setAttribute( QStringLiteral( "version" ), implementationVersion() );
79 wcsCapabilitiesElement.setAttribute( QStringLiteral( "updateSequence" ), QStringLiteral( "0" ) );
80 doc.appendChild( wcsCapabilitiesElement );
81
82 //INSERT Service
83 wcsCapabilitiesElement.appendChild( getServiceElement( doc, project ) );
84
85 //wcs:Capability element
86 QDomElement capabilityElement = doc.createElement( QStringLiteral( "Capability" ) /*wcs:Capability*/ );
87 wcsCapabilitiesElement.appendChild( capabilityElement );
88
89 //wcs:Request element
90 QDomElement requestElement = doc.createElement( QStringLiteral( "Request" ) /*wcs:Request*/ );
91 capabilityElement.appendChild( requestElement );
92
93 //wcs:GetCapabilities
94 QDomElement getCapabilitiesElement = doc.createElement( QStringLiteral( "GetCapabilities" ) /*wcs:GetCapabilities*/ );
95 requestElement.appendChild( getCapabilitiesElement );
96
97 QDomElement dcpTypeElement = doc.createElement( QStringLiteral( "DCPType" ) /*wcs:DCPType*/ );
98 getCapabilitiesElement.appendChild( dcpTypeElement );
99 QDomElement httpElement = doc.createElement( QStringLiteral( "HTTP" ) /*wcs:HTTP*/ );
100 dcpTypeElement.appendChild( httpElement );
101
102 //Prepare url
103 const QString hrefString = serviceUrl( request, project, *serverIface->serverSettings() );
104
105 QDomElement getElement = doc.createElement( QStringLiteral( "Get" ) /*wcs:Get*/ );
106 httpElement.appendChild( getElement );
107 QDomElement onlineResourceElement = doc.createElement( QStringLiteral( "OnlineResource" ) /*wcs:OnlineResource*/ );
108 onlineResourceElement.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
109 onlineResourceElement.setAttribute( QStringLiteral( "xlink:href" ), hrefString );
110 getElement.appendChild( onlineResourceElement );
111
112 const QDomElement getCapabilitiesDhcTypePostElement = dcpTypeElement.cloneNode().toElement(); //this is the same as for 'GetCapabilities'
113 getCapabilitiesDhcTypePostElement.firstChild().firstChild().toElement().setTagName( QStringLiteral( "Post" ) );
114 getCapabilitiesElement.appendChild( getCapabilitiesDhcTypePostElement );
115
116 QDomElement describeCoverageElement = getCapabilitiesElement.cloneNode().toElement(); //this is the same as 'GetCapabilities'
117 describeCoverageElement.setTagName( QStringLiteral( "DescribeCoverage" ) );
118 requestElement.appendChild( describeCoverageElement );
119
120 QDomElement getCoverageElement = getCapabilitiesElement.cloneNode().toElement(); //this is the same as 'GetCapabilities'
121 getCoverageElement.setTagName( QStringLiteral( "GetCoverage" ) );
122 requestElement.appendChild( getCoverageElement );
123
124 //INSERT ContentMetadata
125 wcsCapabilitiesElement.appendChild( getContentMetadataElement( doc, serverIface, project ) );
126
127 return doc;
128 }
129
130 QDomElement getServiceElement( QDomDocument &doc, const QgsProject *project )
131 {
132 //Service element
133 QDomElement serviceElem = doc.createElement( QStringLiteral( "Service" ) );
134
135 //Service name
136 QDomElement nameElem = doc.createElement( QStringLiteral( "name" ) );
137 const QDomText nameText = doc.createTextNode( "WCS" );
138 nameElem.appendChild( nameText );
139 serviceElem.appendChild( nameElem );
140
141 const QString title = QgsServerProjectUtils::owsServiceTitle( *project );
142 QDomElement titleElem = doc.createElement( QStringLiteral( "label" ) );
143 const QDomText titleText = doc.createTextNode( title );
144 titleElem.appendChild( titleText );
145 serviceElem.appendChild( titleElem );
146
147 const QString abstract = QgsServerProjectUtils::owsServiceAbstract( *project );
148 if ( !abstract.isEmpty() )
149 {
150 QDomElement abstractElem = doc.createElement( QStringLiteral( "description" ) );
151 const QDomText abstractText = doc.createCDATASection( abstract );
152 abstractElem.appendChild( abstractText );
153 serviceElem.appendChild( abstractElem );
154 }
155
156 const QStringList keywords = QgsServerProjectUtils::owsServiceKeywords( *project );
157 if ( !keywords.isEmpty() )
158 {
159 QDomElement keywordsElem = doc.createElement( QStringLiteral( "keywords" ) );
160 for ( int i = 0; i < keywords.size(); ++i )
161 {
162 QDomElement keywordElem = doc.createElement( QStringLiteral( "keyword" ) );
163 const QDomText keywordText = doc.createTextNode( keywords.at( i ) );
164 keywordElem.appendChild( keywordText );
165 keywordsElem.appendChild( keywordElem );
166 }
167 serviceElem.appendChild( keywordsElem );
168 }
169
170
171 const QString contactPerson = QgsServerProjectUtils::owsServiceContactPerson( *project );
172 const QString contactOrganization = QgsServerProjectUtils::owsServiceContactOrganization( *project );
173 const QString contactPosition = QgsServerProjectUtils::owsServiceContactPosition( *project );
174 const QString contactMail = QgsServerProjectUtils::owsServiceContactMail( *project );
175 const QString contactPhone = QgsServerProjectUtils::owsServiceContactPhone( *project );
176 const QString onlineResource = QgsServerProjectUtils::owsServiceOnlineResource( *project );
177 if ( !contactPerson.isEmpty() || !contactOrganization.isEmpty() || !contactPosition.isEmpty() || !contactMail.isEmpty() || !contactPhone.isEmpty() || !onlineResource.isEmpty() )
178 {
179 QDomElement responsiblePartyElem = doc.createElement( QStringLiteral( "responsibleParty" ) );
180 if ( !contactPerson.isEmpty() )
181 {
182 QDomElement contactPersonElem = doc.createElement( QStringLiteral( "individualName" ) );
183 const QDomText contactPersonText = doc.createTextNode( contactPerson );
184 contactPersonElem.appendChild( contactPersonText );
185 responsiblePartyElem.appendChild( contactPersonElem );
186 }
187 if ( !contactOrganization.isEmpty() )
188 {
189 QDomElement contactOrganizationElem = doc.createElement( QStringLiteral( "organisationName" ) );
190 const QDomText contactOrganizationText = doc.createTextNode( contactOrganization );
191 contactOrganizationElem.appendChild( contactOrganizationText );
192 responsiblePartyElem.appendChild( contactOrganizationElem );
193 }
194 if ( !contactPosition.isEmpty() )
195 {
196 QDomElement contactPositionElem = doc.createElement( QStringLiteral( "positionName" ) );
197 const QDomText contactPositionText = doc.createTextNode( contactPosition );
198 contactPositionElem.appendChild( contactPositionText );
199 responsiblePartyElem.appendChild( contactPositionElem );
200 }
201 if ( !contactMail.isEmpty() || !contactPhone.isEmpty() || !onlineResource.isEmpty() )
202 {
203 QDomElement contactInfoElem = doc.createElement( QStringLiteral( "contactInfo" ) );
204 if ( !contactMail.isEmpty() )
205 {
206 QDomElement contactAddressElem = doc.createElement( QStringLiteral( "address" ) );
207 QDomElement contactAddressMailElem = doc.createElement( QStringLiteral( "electronicMailAddress" ) );
208 const QDomText contactAddressMailText = doc.createTextNode( contactMail );
209 contactAddressMailElem.appendChild( contactAddressMailText );
210 contactAddressElem.appendChild( contactAddressMailElem );
211 contactInfoElem.appendChild( contactAddressElem );
212 }
213 if ( !contactPhone.isEmpty() )
214 {
215 QDomElement contactPhoneElem = doc.createElement( QStringLiteral( "phone" ) );
216 QDomElement contactVoiceElem = doc.createElement( QStringLiteral( "voice" ) );
217 const QDomText contactVoiceText = doc.createTextNode( contactPhone );
218 contactVoiceElem.appendChild( contactVoiceText );
219 contactPhoneElem.appendChild( contactVoiceElem );
220 contactInfoElem.appendChild( contactPhoneElem );
221 }
222 if ( !onlineResource.isEmpty() )
223 {
224 QDomElement onlineResourceElem = doc.createElement( QStringLiteral( "onlineResource" ) );
225 onlineResourceElem.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
226 onlineResourceElem.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
227 onlineResourceElem.setAttribute( QStringLiteral( "xlink:href" ), onlineResource );
228 contactInfoElem.appendChild( onlineResourceElem );
229 }
230 responsiblePartyElem.appendChild( contactInfoElem );
231 }
232 serviceElem.appendChild( responsiblePartyElem );
233 }
234
235 QDomElement feesElem = doc.createElement( QStringLiteral( "fees" ) );
236 QDomText feesText = doc.createTextNode( QStringLiteral( "None" ) ); // default value if fees are unknown
237 const QString fees = QgsServerProjectUtils::owsServiceFees( *project );
238 if ( !fees.isEmpty() )
239 {
240 feesText = doc.createTextNode( fees );
241 }
242 feesElem.appendChild( feesText );
243 serviceElem.appendChild( feesElem );
244
245 QDomElement accessConstraintsElem = doc.createElement( QStringLiteral( "accessConstraints" ) );
246 QDomText accessConstraintsText = doc.createTextNode( QStringLiteral( "None" ) ); // default value if access constraints are unknown
247 const QString accessConstraints = QgsServerProjectUtils::owsServiceAccessConstraints( *project );
248 if ( !accessConstraints.isEmpty() )
249 {
250 accessConstraintsText = doc.createTextNode( accessConstraints );
251 }
252 accessConstraintsElem.appendChild( accessConstraintsText );
253 serviceElem.appendChild( accessConstraintsElem );
254
255 //End
256 return serviceElem;
257 }
258
259 QDomElement getContentMetadataElement( QDomDocument &doc, QgsServerInterface *serverIface, const QgsProject *project )
260 {
261#ifdef HAVE_SERVER_PYTHON_PLUGINS
262 QgsAccessControl *accessControl = serverIface->accessControls();
263#else
264 ( void ) serverIface;
265#endif
266 /*
267 * Adding layer list in ContentMetadata
268 */
269 QDomElement contentMetadataElement = doc.createElement( QStringLiteral( "ContentMetadata" ) /*wcs:ContentMetadata*/ );
270
271 const QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );
272 for ( int i = 0; i < wcsLayersId.size(); ++i )
273 {
274 QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) );
275 if ( !layer )
276 {
277 continue;
278 }
279 if ( layer->type() != Qgis::LayerType::Raster )
280 {
281 continue;
282 }
283#ifdef HAVE_SERVER_PYTHON_PLUGINS
284 if ( !accessControl->layerReadPermission( layer ) )
285 {
286 continue;
287 }
288#endif
289
290 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
291 const QDomElement layerElem = getCoverageOffering( doc, const_cast<QgsRasterLayer *>( rLayer ), project, true );
292
293 contentMetadataElement.appendChild( layerElem );
294 }
295
296 //End
297 return contentMetadataElement;
298 }
299
300} // namespace QgsWcs
@ Raster
Raster layer.
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:76
Qgis::LayerType type
Definition qgsmaplayer.h:86
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Represents a raster layer.
A helper class that centralizes caches accesses given by all the server cache filter plugins.
bool setCachedDocument(const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the document in cache like capabilities.
bool getCachedDocument(QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached document (or 0 if document not in cache) like capabilities.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
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...
SERVER_EXPORT QString owsServiceAccessConstraints(const QgsProject &project)
Returns the owsService access constraints defined in project.
SERVER_EXPORT QString owsServiceOnlineResource(const QgsProject &project)
Returns the owsService online resource defined in project.
SERVER_EXPORT QString owsServiceFees(const QgsProject &project)
Returns the owsService fees defined in project.
SERVER_EXPORT QStringList owsServiceKeywords(const QgsProject &project)
Returns the owsService keywords defined in project.
SERVER_EXPORT QString owsServiceContactPosition(const QgsProject &project)
Returns the owsService contact position defined in project.
SERVER_EXPORT QString owsServiceContactOrganization(const QgsProject &project)
Returns the owsService contact organization defined in project.
SERVER_EXPORT QString owsServiceTitle(const QgsProject &project)
Returns the owsService title defined in project.
SERVER_EXPORT QString owsServiceContactMail(const QgsProject &project)
Returns the owsService contact mail defined in project.
SERVER_EXPORT QString owsServiceAbstract(const QgsProject &project)
Returns the owsService abstract defined in project.
SERVER_EXPORT QStringList wcsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WCS.
SERVER_EXPORT QString owsServiceContactPhone(const QgsProject &project)
Returns the owsService contact phone defined in project.
SERVER_EXPORT QString owsServiceContactPerson(const QgsProject &project)
Returns the owsService contact person defined in project.
WCS implementation.
Definition qgswcs.cpp:30
QDomDocument createGetCapabilitiesDocument(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request)
Create get capabilities document.
QDomElement getCoverageOffering(QDomDocument &doc, const QgsRasterLayer *layer, const QgsProject *project, bool brief)
CoverageOffering or CoverageOfferingBrief element.
QString implementationVersion()
Returns the highest version supported by this implementation.
const QString WCS_NAMESPACE
Definition qgswcsutils.h:63
const QString GML_NAMESPACE
Definition qgswcsutils.h:64
QString serviceUrl(const QgsServerRequest &request, const QgsProject *project, const QgsServerSettings &settings)
Service URL string.
QDomElement getServiceElement(QDomDocument &doc, const QgsProject *project)
Create Service element for get capabilities document.
QDomElement getContentMetadataElement(QDomDocument &doc, QgsServerInterface *serverIface, const QgsProject *project)
Create ContentMetadata element for get capabilities document.
void writeGetCapabilities(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output WCS GetCapabilities response.