QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgswcsdescribecoverage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswcsdescribecoverage.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 "qgswcsdescribecoverage.h"
22 
23 #include "qgsproject.h"
24 #include "qgsmaplayer.h"
25 #include "qgsrasterlayer.h"
26 
27 namespace QgsWcs
28 {
29 
33  void writeDescribeCoverage( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
34  const QgsServerRequest &request, QgsServerResponse &response )
35  {
36 #ifdef HAVE_SERVER_PYTHON_PLUGINS
37  QgsAccessControl *accessControl = serverIface->accessControls();
38 #endif
39  QDomDocument doc;
40  const QDomDocument *describeDocument = nullptr;
41 
42 #ifdef HAVE_SERVER_PYTHON_PLUGINS
43  QgsServerCacheManager *cacheManager = serverIface->cacheManager();
44  if ( cacheManager && cacheManager->getCachedDocument( &doc, project, request, accessControl ) )
45  {
46  describeDocument = &doc;
47  }
48  else //describe feature xml not in cache. Create a new one
49  {
50  doc = createDescribeCoverageDocument( serverIface, project, version, request );
51 
52  if ( cacheManager )
53  {
54  cacheManager->setCachedDocument( &doc, project, request, accessControl );
55  }
56  describeDocument = &doc;
57  }
58 #else
59  doc = createDescribeCoverageDocument( serverIface, project, version, request );
60 #endif
61  response.setHeader( "Content-Type", "text/xml; charset=utf-8" );
62  response.write( describeDocument->toByteArray() );
63  }
64 
65 
66  QDomDocument createDescribeCoverageDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
67  const QgsServerRequest &request )
68  {
69  Q_UNUSED( version )
70 
71  QDomDocument doc;
72 
73  QgsServerRequest::Parameters parameters = request.parameters();
74 
75 #ifdef HAVE_SERVER_PYTHON_PLUGINS
76  QgsAccessControl *accessControl = serverIface->accessControls();
77 #else
78  ( void )serverIface;
79 #endif
80 
81  //wcs:WCS_Capabilities element
82  QDomElement coveDescElement = doc.createElement( QStringLiteral( "CoverageDescription" )/*wcs:CoverageDescription*/ );
83  coveDescElement.setAttribute( QStringLiteral( "xmlns" ), WCS_NAMESPACE );
84  coveDescElement.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
85  coveDescElement.setAttribute( QStringLiteral( "xsi:schemaLocation" ), WCS_NAMESPACE + " http://schemas.opengis.net/wcs/1.0.0/describeCoverage.xsd" );
86  coveDescElement.setAttribute( QStringLiteral( "xmlns:gml" ), GML_NAMESPACE );
87  coveDescElement.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
88  coveDescElement.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
89  coveDescElement.setAttribute( QStringLiteral( "updateSequence" ), QStringLiteral( "0" ) );
90  doc.appendChild( coveDescElement );
91 
92  //defining coverage name
93  QString coveNames;
94  //read COVERAGE
95  QMap<QString, QString>::const_iterator cove_name_it = parameters.constFind( QStringLiteral( "COVERAGE" ) );
96  if ( cove_name_it != parameters.constEnd() )
97  {
98  coveNames = cove_name_it.value();
99  }
100  if ( coveNames.isEmpty() )
101  {
102  QMap<QString, QString>::const_iterator cove_name_it = parameters.constFind( QStringLiteral( "IDENTIFIER" ) );
103  if ( cove_name_it != parameters.constEnd() )
104  {
105  coveNames = cove_name_it.value();
106  }
107  }
108 
109  QStringList coveNameList;
110  if ( !coveNames.isEmpty() )
111  {
112  coveNameList = coveNames.split( ',' );
113  for ( int i = 0; i < coveNameList.size(); ++i )
114  {
115  coveNameList.replace( i, coveNameList.at( i ).trimmed() );
116  }
117  }
118 
119  QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );
120  for ( int i = 0; i < wcsLayersId.size(); ++i )
121  {
122  QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) );
123  if ( !layer )
124  {
125  continue;
126  }
127  if ( layer->type() != QgsMapLayerType::RasterLayer )
128  {
129  continue;
130  }
131 #ifdef HAVE_SERVER_PYTHON_PLUGINS
132  if ( !accessControl->layerReadPermission( layer ) )
133  {
134  continue;
135  }
136 #endif
137  QString name = layer->name();
138  if ( !layer->shortName().isEmpty() )
139  name = layer->shortName();
140  name = name.replace( ' ', '_' );
141 
142  if ( coveNameList.size() == 0 || coveNameList.contains( name ) )
143  {
144  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
145  coveDescElement.appendChild( getCoverageOffering( doc, const_cast<QgsRasterLayer *>( rLayer ), project ) );
146  }
147  }
148  return doc;
149  }
150 
151 } // namespace QgsWcs
152 
153 
154 
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
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:263
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void writeDescribeCoverage(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output WCS DescribeCoverage response.
QDomDocument createDescribeCoverageDocument(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request)
Create describe coverage 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
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
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
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.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
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.
QString name
Definition: qgsmaplayer.h:82
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
WCS implementation.
Definition: qgswcs.cpp:29
QMap< QString, QString > Parameters