QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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"
20 #include "qgsserverprojectutils.h"
21 #include "qgswcsgetcapabilities.h"
22 
23 #include "qgsproject.h"
24 #include "qgsrasterlayer.h"
25 
26 namespace QgsWcs
27 {
28 
32  void writeGetCapabilities( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
33  const QgsServerRequest &request, QgsServerResponse &response )
34  {
35 #ifdef HAVE_SERVER_PYTHON_PLUGINS
36  QgsAccessControl *accessControl = serverIface->accessControls();
37 #endif
38  QDomDocument doc;
39  const QDomDocument *capabilitiesDocument = nullptr;
40 
41 #ifdef HAVE_SERVER_PYTHON_PLUGINS
42  QgsServerCacheManager *cacheManager = serverIface->cacheManager();
43  if ( cacheManager && cacheManager->getCachedDocument( &doc, project, request, accessControl ) )
44  {
45  capabilitiesDocument = &doc;
46  }
47  else //capabilities xml not in cache. Create a new one
48  {
49  doc = createGetCapabilitiesDocument( serverIface, project, version, request );
50 
51  if ( cacheManager )
52  {
53  cacheManager->setCachedDocument( &doc, project, request, accessControl );
54  }
55  capabilitiesDocument = &doc;
56  }
57 #else
58  doc = createGetCapabilitiesDocument( serverIface, project, version, request );
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,
66  const QgsServerRequest &request )
67  {
68  Q_UNUSED( version )
69 
70  QDomDocument doc;
71 
72  //wcs:WCS_Capabilities element
73  QDomElement wcsCapabilitiesElement = doc.createElement( QStringLiteral( "WCS_Capabilities" )/*wcs:WCS_Capabilities*/ );
74  wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns" ), WCS_NAMESPACE );
75  wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
76  wcsCapabilitiesElement.setAttribute( QStringLiteral( "xsi:schemaLocation" ), WCS_NAMESPACE + " http://schemas.opengis.net/wcs/1.0.0/wcsCapabilities.xsd" );
77  wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:gml" ), GML_NAMESPACE );
78  wcsCapabilitiesElement.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
79  wcsCapabilitiesElement.setAttribute( QStringLiteral( "version" ), implementationVersion() );
80  wcsCapabilitiesElement.setAttribute( QStringLiteral( "updateSequence" ), QStringLiteral( "0" ) );
81  doc.appendChild( wcsCapabilitiesElement );
82 
83  //INSERT Service
84  wcsCapabilitiesElement.appendChild( getServiceElement( doc, project ) );
85 
86  //wcs:Capability element
87  QDomElement capabilityElement = doc.createElement( QStringLiteral( "Capability" )/*wcs:Capability*/ );
88  wcsCapabilitiesElement.appendChild( capabilityElement );
89 
90  //wcs:Request element
91  QDomElement requestElement = doc.createElement( QStringLiteral( "Request" )/*wcs:Request*/ );
92  capabilityElement.appendChild( requestElement );
93 
94  //wcs:GetCapabilities
95  QDomElement getCapabilitiesElement = doc.createElement( QStringLiteral( "GetCapabilities" )/*wcs:GetCapabilities*/ );
96  requestElement.appendChild( getCapabilitiesElement );
97 
98  QDomElement dcpTypeElement = doc.createElement( QStringLiteral( "DCPType" )/*wcs:DCPType*/ );
99  getCapabilitiesElement.appendChild( dcpTypeElement );
100  QDomElement httpElement = doc.createElement( QStringLiteral( "HTTP" )/*wcs:HTTP*/ );
101  dcpTypeElement.appendChild( httpElement );
102 
103  //Prepare url
104  QString hrefString = serviceUrl( request, project );
105 
106  QDomElement getElement = doc.createElement( QStringLiteral( "Get" )/*wcs:Get*/ );
107  httpElement.appendChild( getElement );
108  QDomElement onlineResourceElement = doc.createElement( QStringLiteral( "OnlineResource" )/*wcs:OnlineResource*/ );
109  onlineResourceElement.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
110  onlineResourceElement.setAttribute( QStringLiteral( "xlink:href" ), hrefString );
111  getElement.appendChild( onlineResourceElement );
112 
113  QDomElement getCapabilitiesDhcTypePostElement = dcpTypeElement.cloneNode().toElement();//this is the same as for 'GetCapabilities'
114  getCapabilitiesDhcTypePostElement.firstChild().firstChild().toElement().setTagName( QStringLiteral( "Post" ) );
115  getCapabilitiesElement.appendChild( getCapabilitiesDhcTypePostElement );
116 
117  QDomElement describeCoverageElement = getCapabilitiesElement.cloneNode().toElement();//this is the same as 'GetCapabilities'
118  describeCoverageElement.setTagName( QStringLiteral( "DescribeCoverage" ) );
119  requestElement.appendChild( describeCoverageElement );
120 
121  QDomElement getCoverageElement = getCapabilitiesElement.cloneNode().toElement();//this is the same as 'GetCapabilities'
122  getCoverageElement.setTagName( QStringLiteral( "GetCoverage" ) );
123  requestElement.appendChild( getCoverageElement );
124 
125  //INSERT ContentMetadata
126  wcsCapabilitiesElement.appendChild( getContentMetadataElement( doc, serverIface, project ) );
127 
128  return doc;
129 
130  }
131 
132  QDomElement getServiceElement( QDomDocument &doc, const QgsProject *project )
133  {
134  //Service element
135  QDomElement serviceElem = doc.createElement( QStringLiteral( "Service" ) );
136 
137  //Service name
138  QDomElement nameElem = doc.createElement( QStringLiteral( "name" ) );
139  QDomText nameText = doc.createTextNode( "WCS" );
140  nameElem.appendChild( nameText );
141  serviceElem.appendChild( nameElem );
142 
143  QString title = QgsServerProjectUtils::owsServiceTitle( *project );
144  if ( !title.isEmpty() )
145  {
146  QDomElement titleElem = doc.createElement( QStringLiteral( "label" ) );
147  QDomText titleText = doc.createTextNode( title );
148  titleElem.appendChild( titleText );
149  serviceElem.appendChild( titleElem );
150  }
151 
152  QString abstract = QgsServerProjectUtils::owsServiceAbstract( *project );
153  if ( !abstract.isEmpty() )
154  {
155  QDomElement abstractElem = doc.createElement( QStringLiteral( "description" ) );
156  QDomText abstractText = doc.createCDATASection( abstract );
157  abstractElem.appendChild( abstractText );
158  serviceElem.appendChild( abstractElem );
159  }
160 
161  QStringList keywords = QgsServerProjectUtils::owsServiceKeywords( *project );
162  if ( !keywords.isEmpty() )
163  {
164  QDomElement keywordsElem = doc.createElement( QStringLiteral( "keywords" ) );
165  for ( int i = 0; i < keywords.size(); ++i )
166  {
167  QDomElement keywordElem = doc.createElement( QStringLiteral( "keyword" ) );
168  QDomText keywordText = doc.createTextNode( keywords.at( i ) );
169  keywordElem.appendChild( keywordText );
170  keywordsElem.appendChild( keywordElem );
171  }
172  serviceElem.appendChild( keywordsElem );
173  }
174 
175 
176  QString contactPerson = QgsServerProjectUtils::owsServiceContactPerson( *project );
177  QString contactOrganization = QgsServerProjectUtils::owsServiceContactOrganization( *project );
178  QString contactPosition = QgsServerProjectUtils::owsServiceContactPosition( *project );
179  QString contactMail = QgsServerProjectUtils::owsServiceContactMail( *project );
180  QString contactPhone = QgsServerProjectUtils::owsServiceContactPhone( *project );
181  QString onlineResource = QgsServerProjectUtils::owsServiceOnlineResource( *project );
182  if ( !contactPerson.isEmpty() ||
183  !contactOrganization.isEmpty() ||
184  !contactPosition.isEmpty() ||
185  !contactMail.isEmpty() ||
186  !contactPhone.isEmpty() ||
187  !onlineResource.isEmpty() )
188  {
189  QDomElement responsiblePartyElem = doc.createElement( QStringLiteral( "responsibleParty" ) );
190  if ( !contactPerson.isEmpty() )
191  {
192  QDomElement contactPersonElem = doc.createElement( QStringLiteral( "individualName" ) );
193  QDomText contactPersonText = doc.createTextNode( contactPerson );
194  contactPersonElem.appendChild( contactPersonText );
195  responsiblePartyElem.appendChild( contactPersonElem );
196  }
197  if ( !contactOrganization.isEmpty() )
198  {
199  QDomElement contactOrganizationElem = doc.createElement( QStringLiteral( "organisationName" ) );
200  QDomText contactOrganizationText = doc.createTextNode( contactOrganization );
201  contactOrganizationElem.appendChild( contactOrganizationText );
202  responsiblePartyElem.appendChild( contactOrganizationElem );
203  }
204  if ( !contactPosition.isEmpty() )
205  {
206  QDomElement contactPositionElem = doc.createElement( QStringLiteral( "positionName" ) );
207  QDomText contactPositionText = doc.createTextNode( contactPosition );
208  contactPositionElem.appendChild( contactPositionText );
209  responsiblePartyElem.appendChild( contactPositionElem );
210  }
211  if ( !contactMail.isEmpty() ||
212  !contactPhone.isEmpty() ||
213  !onlineResource.isEmpty() )
214  {
215  QDomElement contactInfoElem = doc.createElement( QStringLiteral( "contactInfo" ) );
216  if ( !contactMail.isEmpty() )
217  {
218  QDomElement contactAddressElem = doc.createElement( QStringLiteral( "address" ) );
219  QDomElement contactAddressMailElem = doc.createElement( QStringLiteral( "electronicMailAddress" ) );
220  QDomText contactAddressMailText = doc.createTextNode( contactMail );
221  contactAddressMailElem.appendChild( contactAddressMailText );
222  contactAddressElem.appendChild( contactAddressMailElem );
223  contactInfoElem.appendChild( contactAddressElem );
224  }
225  if ( !contactPhone.isEmpty() )
226  {
227  QDomElement contactPhoneElem = doc.createElement( QStringLiteral( "phone" ) );
228  QDomElement contactVoiceElem = doc.createElement( QStringLiteral( "voice" ) );
229  QDomText contactVoiceText = doc.createTextNode( contactPhone );
230  contactVoiceElem.appendChild( contactVoiceText );
231  contactPhoneElem.appendChild( contactVoiceElem );
232  contactInfoElem.appendChild( contactPhoneElem );
233  }
234  if ( !onlineResource.isEmpty() )
235  {
236  QDomElement onlineResourceElem = doc.createElement( QStringLiteral( "onlineResource" ) );
237  onlineResourceElem.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
238  onlineResourceElem.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
239  onlineResourceElem.setAttribute( QStringLiteral( "xlink:href" ), onlineResource );
240  contactInfoElem.appendChild( onlineResourceElem );
241  }
242  responsiblePartyElem.appendChild( contactInfoElem );
243  }
244  serviceElem.appendChild( responsiblePartyElem );
245  }
246 
247  QDomElement feesElem = doc.createElement( QStringLiteral( "fees" ) );
248  QDomText feesText = doc.createTextNode( QStringLiteral( "None" ) ); // default value if fees are unknown
249  QString fees = QgsServerProjectUtils::owsServiceFees( *project );
250  if ( !fees.isEmpty() )
251  {
252  feesText = doc.createTextNode( fees );
253  }
254  feesElem.appendChild( feesText );
255  serviceElem.appendChild( feesElem );
256 
257  QDomElement accessConstraintsElem = doc.createElement( QStringLiteral( "accessConstraints" ) );
258  QDomText accessConstraintsText = doc.createTextNode( QStringLiteral( "None" ) ); // default value if access constraints are unknown
259  QString accessConstraints = QgsServerProjectUtils::owsServiceAccessConstraints( *project );
260  if ( !accessConstraints.isEmpty() )
261  {
262  accessConstraintsText = doc.createTextNode( accessConstraints );
263  }
264  accessConstraintsElem.appendChild( accessConstraintsText );
265  serviceElem.appendChild( accessConstraintsElem );
266 
267  //End
268  return serviceElem;
269  }
270 
271  QDomElement getContentMetadataElement( QDomDocument &doc, QgsServerInterface *serverIface, const QgsProject *project )
272  {
273 #ifdef HAVE_SERVER_PYTHON_PLUGINS
274  QgsAccessControl *accessControl = serverIface->accessControls();
275 #else
276  ( void )serverIface;
277 #endif
278  /*
279  * Adding layer list in ContentMetadata
280  */
281  QDomElement contentMetadataElement = doc.createElement( QStringLiteral( "ContentMetadata" )/*wcs:ContentMetadata*/ );
282 
283  QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );
284  for ( int i = 0; i < wcsLayersId.size(); ++i )
285  {
286  QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) );
287  if ( !layer )
288  {
289  continue;
290  }
291  if ( layer->type() != QgsMapLayerType::RasterLayer )
292  {
293  continue;
294  }
295 #ifdef HAVE_SERVER_PYTHON_PLUGINS
296  if ( !accessControl->layerReadPermission( layer ) )
297  {
298  continue;
299  }
300 #endif
301 
302  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
303  QDomElement layerElem = getCoverageOffering( doc, const_cast<QgsRasterLayer *>( rLayer ), project, true );
304 
305  contentMetadataElement.appendChild( layerElem );
306  }
307 
308  //End
309  return contentMetadataElement;
310  }
311 
312 } // namespace QgsWcs
313 
314 
315 
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...
Base class for all map layer types.
Definition: qgsmaplayer.h:78
QgsMapLayerType type() const
Returns the type of the layer.
const QString WCS_NAMESPACE
Definition: qgswcsutils.h:62
SERVER_EXPORT QString owsServiceContactPosition(const QgsProject &project)
Returns the owsService contact position defined in project.
SERVER_EXPORT QString owsServiceContactPerson(const QgsProject &project)
Returns the owsService contact person defined in project.
SERVER_EXPORT QStringList owsServiceKeywords(const QgsProject &project)
Returns the owsService keywords defined in project.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
SERVER_EXPORT QString owsServiceAbstract(const QgsProject &project)
Returns the owsService abstract defined in project.
QString serviceUrl(const QgsServerRequest &request, const QgsProject *project)
Service URL string.
QDomDocument createGetCapabilitiesDocument(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request)
Create get capabilities document.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device...
const QString GML_NAMESPACE
Definition: qgswcsutils.h:63
SERVER_EXPORT QStringList wcsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WCS.
A helper class that centralizes caches accesses given by all the server cache filter plugins...
Reads and writes project states.
Definition: qgsproject.h:89
SERVER_EXPORT QString owsServiceFees(const QgsProject &project)
Returns the owsService fees defined in project.
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.
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 owsServiceContactOrganization(const QgsProject &project)
Returns the owsService contact organization defined in project.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QDomElement getServiceElement(QDomDocument &doc, const QgsProject *project)
Create Service element for get capabilities document.
QString implementationVersion()
Returns the highest version supported by this implementation.
Definition: qgswcsutils.cpp:30
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
QDomElement getCoverageOffering(QDomDocument &doc, const QgsRasterLayer *layer, const QgsProject *project, bool brief)
CoverageOffering or CoverageOfferingBrief element.
Definition: qgswcsutils.cpp:35
bool setCachedDocument(const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the document in cache like capabilities.
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...
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
SERVER_EXPORT QString owsServiceContactMail(const QgsProject &project)
Returns the owsService contact mail defined in project.
QDomElement getContentMetadataElement(QDomDocument &doc, QgsServerInterface *serverIface, const QgsProject *project)
Create ContentMetadata element for get capabilities document.
WCS implementation.
Definition: qgswcs.cpp:29
void writeGetCapabilities(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output WCS GetCapabilities response.
SERVER_EXPORT QString owsServiceTitle(const QgsProject &project)
Returns the owsService title defined in project.
SERVER_EXPORT QString owsServiceContactPhone(const QgsProject &project)
Returns the owsService contact phone defined in project.