QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgswfsdescribefeaturetype.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswfsdescribefeaturetype.cpp
3  -------------------------
4  begin : December 20 , 2016
5  copyright : (C) 2007 by Marco Hugentobler (original code)
6  (C) 2012 by RenĂ©-Luc D'Hont (original code)
7  (C) 2014 by Alessandro Pasotti (original code)
8  (C) 2017 by David Marteau
9  email : marco dot hugentobler at karto dot baug dot ethz dot ch
10  a dot pasotti at itopen dot it
11  david dot marteau at 3liz dot com
12  ***************************************************************************/
13 
14 /***************************************************************************
15  * *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published by *
18  * the Free Software Foundation; either version 2 of the License, or *
19  * (at your option) any later version. *
20  * *
21  ***************************************************************************/
22 #include "qgswfsutils.h"
23 #include "qgsserverprojectutils.h"
25 #include "qgswfsparameters.h"
26 
27 #include "qgsproject.h"
28 #include "qgsvectorlayer.h"
30 
31 namespace QgsWfs
32 {
33 
34  void writeDescribeFeatureType( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
35  const QgsServerRequest &request, QgsServerResponse &response )
36  {
37 #ifdef HAVE_SERVER_PYTHON_PLUGINS
38  QgsAccessControl *accessControl = serverIface->accessControls();
39 #endif
40  QDomDocument doc;
41  const QDomDocument *describeDocument = nullptr;
42 
43 #ifdef HAVE_SERVER_PYTHON_PLUGINS
44  QgsServerCacheManager *cacheManager = serverIface->cacheManager();
45  if ( cacheManager && cacheManager->getCachedDocument( &doc, project, request, accessControl ) )
46  {
47  describeDocument = &doc;
48  }
49  else //describe feature xml not in cache. Create a new one
50  {
51  doc = createDescribeFeatureTypeDocument( serverIface, project, version, request );
52 
53  if ( cacheManager )
54  {
55  cacheManager->setCachedDocument( &doc, project, request, accessControl );
56  }
57  describeDocument = &doc;
58  }
59 #else
60  doc = createDescribeFeatureTypeDocument( serverIface, project, version, request );
61 #endif
62  response.setHeader( "Content-Type", "text/xml; charset=utf-8" );
63  response.write( describeDocument->toByteArray() );
64  }
65 
66 
67  QDomDocument createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
68  const QgsServerRequest &request )
69  {
70  Q_UNUSED( version )
71 
72  QDomDocument doc;
73 
74  QgsServerRequest::Parameters parameters = request.parameters();
75  QgsWfsParameters wfsParameters( QUrlQuery( request.url() ) );
76  QgsWfsParameters::Format oFormat = wfsParameters.outputFormat();
77 
78  // test oFormat
79  if ( oFormat == QgsWfsParameters::Format::NONE )
80  throw QgsBadRequestException( QStringLiteral( "Invalid WFS Parameter" ),
81  QStringLiteral( "OUTPUTFORMAT %1 is not supported" ).arg( wfsParameters.outputFormatAsString() ) );
82 
83 #ifdef HAVE_SERVER_PYTHON_PLUGINS
84  QgsAccessControl *accessControl = serverIface->accessControls();
85 #else
86  ( void )serverIface;
87 #endif
88 
89  //xsd:schema
90  QDomElement schemaElement = doc.createElement( QStringLiteral( "schema" )/*xsd:schema*/ );
91  schemaElement.setAttribute( QStringLiteral( "xmlns" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema" ) );
92  schemaElement.setAttribute( QStringLiteral( "xmlns:xsd" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema" ) );
93  schemaElement.setAttribute( QStringLiteral( "xmlns:ogc" ), OGC_NAMESPACE );
94  schemaElement.setAttribute( QStringLiteral( "xmlns:gml" ), GML_NAMESPACE );
95  schemaElement.setAttribute( QStringLiteral( "xmlns:qgs" ), QGS_NAMESPACE );
96  schemaElement.setAttribute( QStringLiteral( "targetNamespace" ), QGS_NAMESPACE );
97  schemaElement.setAttribute( QStringLiteral( "elementFormDefault" ), QStringLiteral( "qualified" ) );
98  schemaElement.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
99  doc.appendChild( schemaElement );
100 
101  //xsd:import
102  QDomElement importElement = doc.createElement( QStringLiteral( "import" )/*xsd:import*/ );
103  importElement.setAttribute( QStringLiteral( "namespace" ), GML_NAMESPACE );
104  if ( oFormat == QgsWfsParameters::Format::GML2 )
105  importElement.setAttribute( QStringLiteral( "schemaLocation" ), QStringLiteral( "http://schemas.opengis.net/gml/2.1.2/feature.xsd" ) );
106  else if ( oFormat == QgsWfsParameters::Format::GML3 )
107  importElement.setAttribute( QStringLiteral( "schemaLocation" ), QStringLiteral( "http://schemas.opengis.net/gml/3.1.1/base/gml.xsd" ) );
108  schemaElement.appendChild( importElement );
109 
110  QStringList typeNameList;
111  QDomDocument queryDoc;
112  QString errorMsg;
113  if ( queryDoc.setContent( parameters.value( QStringLiteral( "REQUEST_BODY" ) ), true, &errorMsg ) )
114  {
115  //read doc
116  QDomElement queryDocElem = queryDoc.documentElement();
117  QDomNodeList docChildNodes = queryDocElem.childNodes();
118  if ( docChildNodes.size() )
119  {
120  for ( int i = 0; i < docChildNodes.size(); i++ )
121  {
122  QDomElement docChildElem = docChildNodes.at( i ).toElement();
123  if ( docChildElem.tagName() == QLatin1String( "TypeName" ) )
124  {
125  QString typeName = docChildElem.text().trimmed();
126  if ( typeName.contains( ':' ) )
127  typeNameList << typeName.section( ':', 1, 1 );
128  else
129  typeNameList << typeName;
130  }
131  }
132  }
133  }
134  else
135  {
136  typeNameList = wfsParameters.typeNames();
137  }
138 
139  QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
140  for ( int i = 0; i < wfsLayerIds.size(); ++i )
141  {
142  QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) );
143  if ( !layer )
144  {
145  continue;
146  }
147  if ( layer->type() != QgsMapLayerType::VectorLayer )
148  {
149  continue;
150  }
151 
152  QString name = layerTypeName( layer );
153 
154  if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) )
155  {
156  continue;
157  }
158 #ifdef HAVE_SERVER_PYTHON_PLUGINS
159  if ( accessControl && !accessControl->layerReadPermission( layer ) )
160  {
161  if ( !typeNameList.isEmpty() )
162  {
163  throw QgsSecurityAccessException( QStringLiteral( "Feature access permission denied" ) );
164  }
165  else
166  {
167  continue;
168  }
169  }
170 #endif
171  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
172  QgsVectorDataProvider *provider = vLayer->dataProvider();
173  if ( !provider )
174  {
175  continue;
176  }
177  setSchemaLayer( schemaElement, doc, const_cast<QgsVectorLayer *>( vLayer ) );
178  }
179  return doc;
180  }
181 
182  void setSchemaLayer( QDomElement &parentElement, QDomDocument &doc, const QgsVectorLayer *layer )
183  {
184  const QgsVectorDataProvider *provider = layer->dataProvider();
185  if ( !provider )
186  {
187  return;
188  }
189 
190  QString typeName = layerTypeName( layer );
191 
192  //xsd:element
193  QDomElement elementElem = doc.createElement( QStringLiteral( "element" )/*xsd:element*/ );
194  elementElem.setAttribute( QStringLiteral( "name" ), typeName );
195  elementElem.setAttribute( QStringLiteral( "type" ), "qgs:" + typeName + "Type" );
196  elementElem.setAttribute( QStringLiteral( "substitutionGroup" ), QStringLiteral( "gml:_Feature" ) );
197  parentElement.appendChild( elementElem );
198 
199  //xsd:complexType
200  QDomElement complexTypeElem = doc.createElement( QStringLiteral( "complexType" )/*xsd:complexType*/ );
201  complexTypeElem.setAttribute( QStringLiteral( "name" ), typeName + "Type" );
202  parentElement.appendChild( complexTypeElem );
203 
204  //xsd:complexType
205  QDomElement complexContentElem = doc.createElement( QStringLiteral( "complexContent" )/*xsd:complexContent*/ );
206  complexTypeElem.appendChild( complexContentElem );
207 
208  //xsd:extension
209  QDomElement extensionElem = doc.createElement( QStringLiteral( "extension" )/*xsd:extension*/ );
210  extensionElem.setAttribute( QStringLiteral( "base" ), QStringLiteral( "gml:AbstractFeatureType" ) );
211  complexContentElem.appendChild( extensionElem );
212 
213  //xsd:sequence
214  QDomElement sequenceElem = doc.createElement( QStringLiteral( "sequence" )/*xsd:sequence*/ );
215  extensionElem.appendChild( sequenceElem );
216 
217  //xsd:element
218  if ( layer->isSpatial() )
219  {
220  QDomElement geomElem = doc.createElement( QStringLiteral( "element" )/*xsd:element*/ );
221  geomElem.setAttribute( QStringLiteral( "name" ), QStringLiteral( "geometry" ) );
222 
223  QgsWkbTypes::Type wkbType = layer->wkbType();
224  switch ( wkbType )
225  {
227  case QgsWkbTypes::Point:
228  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:PointPropertyType" ) );
229  break;
232  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:LineStringPropertyType" ) );
233  break;
236  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:PolygonPropertyType" ) );
237  break;
240  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:MultiPointPropertyType" ) );
241  break;
244  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:MultiLineStringPropertyType" ) );
245  break;
248  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:MultiPolygonPropertyType" ) );
249  break;
250  default:
251  geomElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "gml:GeometryPropertyType" ) );
252  break;
253  }
254  geomElem.setAttribute( QStringLiteral( "minOccurs" ), QStringLiteral( "0" ) );
255  geomElem.setAttribute( QStringLiteral( "maxOccurs" ), QStringLiteral( "1" ) );
256  sequenceElem.appendChild( geomElem );
257 
258  //Attributes
259  QgsFields fields = layer->fields();
260  //hidden attributes for this layer
261  const QSet<QString> &layerExcludedAttributes = layer->excludeAttributesWfs();
262  for ( int idx = 0; idx < fields.count(); ++idx )
263  {
264  const QgsField field = fields.at( idx );
265  QString attributeName = field.name();
266  //skip attribute if excluded from WFS publication
267  if ( layerExcludedAttributes.contains( attributeName ) )
268  {
269  continue;
270  }
271 
272  //xsd:element
273  QDomElement attElem = doc.createElement( QStringLiteral( "element" )/*xsd:element*/ );
274  attElem.setAttribute( QStringLiteral( "name" ), attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
275  QVariant::Type attributeType = field.type();
276  if ( attributeType == QVariant::Int )
277  {
278  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
279  }
280  else if ( attributeType == QVariant::UInt )
281  {
282  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unsignedInt" ) );
283  }
284  else if ( attributeType == QVariant::LongLong )
285  {
286  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "long" ) );
287  }
288  else if ( attributeType == QVariant::ULongLong )
289  {
290  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unsignedLong" ) );
291  }
292  else if ( attributeType == QVariant::Double )
293  {
294  if ( field.length() > 0 && field.precision() == 0 )
295  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "integer" ) );
296  else
297  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "decimal" ) );
298  }
299  else if ( attributeType == QVariant::Bool )
300  {
301  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "boolean" ) );
302  }
303  else if ( attributeType == QVariant::Date )
304  {
305  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
306  }
307  else if ( attributeType == QVariant::Time )
308  {
309  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
310  }
311  else if ( attributeType == QVariant::DateTime )
312  {
313  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
314  }
315  else
316  {
317  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "string" ) );
318  }
319 
320  const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
321  if ( setup.type() == QStringLiteral( "DateTime" ) )
322  {
323  QgsDateTimeFieldFormatter fieldFormatter;
324  const QVariantMap config = setup.config();
325  const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( field.type() ) ).toString();
326  if ( fieldFormat == QStringLiteral( "yyyy-MM-dd" ) )
327  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
328  else if ( fieldFormat == QStringLiteral( "HH:mm:ss" ) )
329  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
330  else
331  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
332  }
333  else if ( setup.type() == QStringLiteral( "Range" ) )
334  {
335  const QVariantMap config = setup.config();
336  if ( config.contains( QStringLiteral( "Precision" ) ) )
337  {
338  // if precision in range config is not the same as the attributePrec
339  // we need to update type
340  bool ok;
341  int configPrec( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
342  if ( ok && configPrec != field.precision() )
343  {
344  if ( configPrec == 0 )
345  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "integer" ) );
346  else
347  attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "decimal" ) );
348  }
349  }
350  }
351 
352  if ( !( field.constraints().constraints() & QgsFieldConstraints::Constraint::ConstraintNotNull ) )
353  {
354  attElem.setAttribute( QStringLiteral( "nillable" ), QStringLiteral( "true" ) );
355  }
356 
357  sequenceElem.appendChild( attElem );
358 
359  QString alias = field.alias();
360  if ( !alias.isEmpty() )
361  {
362  attElem.setAttribute( QStringLiteral( "alias" ), alias );
363  }
364  }
365  }
366  }
367 
368 } // namespace QgsWfs
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
QVariantMap config() const
QgsMapLayerType type() const
Returns the type of the layer.
QString name
Definition: qgsfield.h:58
int precision
Definition: qgsfield.h:55
void setSchemaLayer(QDomElement &parentElement, QDomDocument &doc, const QgsVectorLayer *layer)
const QString QGS_NAMESPACE
Definition: qgswfsutils.h:69
QString alias
Definition: qgsfield.h:59
QSet< QString > excludeAttributesWfs() const
A set of attributes that are not advertised in WFS requests with QGIS server.
Format outputFormat() const
Returns format.
QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QString outputFormatAsString() const
Returns OUTPUTFORMAT parameter as a string.
void writeDescribeFeatureType(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output WFS GetCapabilities response.
Container of fields for a vector layer.
Definition: qgsfields.h:42
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition: qgsfield.cpp:410
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device...
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QString layerTypeName(const QgsMapLayer *layer)
Returns typename from vector layer.
Definition: qgswfsutils.cpp:71
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
int length
Definition: qgsfield.h:54
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsFields fields() const FINAL
Returns the list of fields of this layer.
const QString & typeName
WMS implementation.
Definition: qgswfs.cpp:35
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
const QString GML_NAMESPACE
Definition: qgswfsutils.h:67
A helper class that centralizes caches accesses given by all the server cache filter plugins...
Reads and writes project states.
Definition: qgsproject.h:89
Format
Output format for the response.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
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.
const QString OGC_NAMESPACE
Definition: qgswfsutils.h:68
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
const QRegExp cleanTagNameRegExp("(?![\\\-]).")
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
QgsFieldConstraints constraints
Definition: qgsfield.h:61
QDomDocument createDescribeFeatureTypeDocument(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request)
Create get capabilities document.
Holder for the widget type and its configuration for a field.
SERVER_EXPORT QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
Field formatter for a date time field.
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.
Exception thrown in case of malformed request.
A helper class that centralizes restrictions given by all the access control filter plugins...
QStringList typeNames() const
Returns TYPENAME parameter as list.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Exception thrown when data access violates access controls.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer&#39;s data provider, it may be nullptr.
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
Provides an interface to retrieve and manipulate WFS parameters received from the client...
QVariant::Type type
Definition: qgsfield.h:56
QMap< QString, QString > Parameters