QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgswfsdescribefeaturetypegml.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswfsdescribefeaturetypegml.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 ***************************************************************************/
23
24#include "qgsproject.h"
26#include "qgsvectorlayer.h"
28#include "qgswfsparameters.h"
29#include "qgswfsutils.h"
30
31#include <QString>
32
33using namespace Qt::StringLiterals;
34
35using namespace QgsWfs;
36
38 : wfsParameters( wfsParams )
39{}
40
42 QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response
43) const
44{
45#ifdef HAVE_SERVER_PYTHON_PLUGINS
46 QgsAccessControl *accessControl = serverIface->accessControls();
47#endif
48 QDomDocument doc;
49 const QDomDocument *describeDocument = nullptr;
50
51#ifdef HAVE_SERVER_PYTHON_PLUGINS
52 QgsServerCacheManager *cacheManager = serverIface->cacheManager();
53 if ( cacheManager && cacheManager->getCachedDocument( &doc, project, request, accessControl ) )
54 {
55 describeDocument = &doc;
56 }
57 else //describe feature xml not in cache. Create a new one
58 {
59 doc = createDescribeFeatureTypeDocument( serverIface, project, version, request );
60
61 if ( cacheManager )
62 {
63 cacheManager->setCachedDocument( &doc, project, request, accessControl );
64 }
65 describeDocument = &doc;
66 }
67#else
68 doc = createDescribeFeatureTypeDocument( serverIface, project, version, request );
69 describeDocument = &doc;
70#endif
71 response.setHeader( "Content-Type", "text/xml; charset=utf-8" );
72 response.write( describeDocument->toByteArray() );
73}
74
75
76QDomDocument QgsWfsDescribeFeatureTypeGml::createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) const
77{
78 Q_UNUSED( version )
79
80 QDomDocument doc;
81
82#ifdef HAVE_SERVER_PYTHON_PLUGINS
83 QgsAccessControl *accessControl = serverIface->accessControls();
84#else
85 ( void ) serverIface;
86#endif
87
88 auto outputFormat = wfsParameters.outputFormat();
89
90 //xsd:schema
91 QDomElement schemaElement = doc.createElement( u"schema"_s /*xsd:schema*/ );
92 schemaElement.setAttribute( u"xmlns"_s, u"http://www.w3.org/2001/XMLSchema"_s );
93 schemaElement.setAttribute( u"xmlns:xsd"_s, u"http://www.w3.org/2001/XMLSchema"_s );
94 schemaElement.setAttribute( u"xmlns:ogc"_s, OGC_NAMESPACE );
95 schemaElement.setAttribute( u"xmlns:gml"_s, GML_NAMESPACE );
96 schemaElement.setAttribute( u"xmlns:qgs"_s, QGS_NAMESPACE );
97 schemaElement.setAttribute( u"targetNamespace"_s, QGS_NAMESPACE );
98 schemaElement.setAttribute( u"elementFormDefault"_s, u"qualified"_s );
99 schemaElement.setAttribute( u"version"_s, u"1.0"_s );
100 doc.appendChild( schemaElement );
101
102 //xsd:import
103 QDomElement importElement = doc.createElement( u"import"_s /*xsd:import*/ );
104 importElement.setAttribute( u"namespace"_s, GML_NAMESPACE );
105 if ( outputFormat == QgsWfsParameters::Format::GML2 )
106 importElement.setAttribute( u"schemaLocation"_s, u"http://schemas.opengis.net/gml/2.1.2/feature.xsd"_s );
107 else if ( outputFormat == QgsWfsParameters::Format::GML3 )
108 importElement.setAttribute( u"schemaLocation"_s, u"http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"_s );
109 schemaElement.appendChild( importElement );
110
111 QStringList typeNameList = getRequestTypeNames( request, wfsParameters );
112
113 const QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
114 for ( int i = 0; i < wfsLayerIds.size(); ++i )
115 {
116 QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) );
117 if ( !layer )
118 {
119 continue;
120 }
121
122 const QString name = layer->serverProperties()->wfsTypeName();
123
124 if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) )
125 {
126 continue;
127 }
128#ifdef HAVE_SERVER_PYTHON_PLUGINS
129 if ( accessControl && !accessControl->layerReadPermission( layer ) )
130 {
131 if ( !typeNameList.isEmpty() )
132 {
133 throw QgsSecurityAccessException( u"Feature access permission denied"_s );
134 }
135 else
136 {
137 continue;
138 }
139 }
140#endif
141 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
142 QgsVectorDataProvider *provider = vLayer->dataProvider();
143 if ( !provider )
144 {
145 continue;
146 }
147 setSchemaLayer( schemaElement, doc, const_cast<QgsVectorLayer *>( vLayer ) );
148 }
149 return doc;
150}
151
152void QgsWfsDescribeFeatureTypeGml::setSchemaLayer( QDomElement &parentElement, QDomDocument &doc, const QgsVectorLayer *layer ) const
153{
154 const QgsVectorDataProvider *provider = layer->dataProvider();
155 if ( !provider )
156 {
157 return;
158 }
159
160 const QString typeName = layer->serverProperties()->wfsTypeName();
161
162 //xsd:element
163 QDomElement elementElem = doc.createElement( u"element"_s /*xsd:element*/ );
164 elementElem.setAttribute( u"name"_s, typeName );
165 elementElem.setAttribute( u"type"_s, "qgs:" + typeName + "Type" );
166 elementElem.setAttribute( u"substitutionGroup"_s, u"gml:_Feature"_s );
167 parentElement.appendChild( elementElem );
168
169 //xsd:complexType
170 QDomElement complexTypeElem = doc.createElement( u"complexType"_s /*xsd:complexType*/ );
171 complexTypeElem.setAttribute( u"name"_s, typeName + "Type" );
172 parentElement.appendChild( complexTypeElem );
173
174 //xsd:complexType
175 QDomElement complexContentElem = doc.createElement( u"complexContent"_s /*xsd:complexContent*/ );
176 complexTypeElem.appendChild( complexContentElem );
177
178 //xsd:extension
179 QDomElement extensionElem = doc.createElement( u"extension"_s /*xsd:extension*/ );
180 extensionElem.setAttribute( u"base"_s, u"gml:AbstractFeatureType"_s );
181 complexContentElem.appendChild( extensionElem );
182
183 //xsd:sequence
184 QDomElement sequenceElem = doc.createElement( u"sequence"_s /*xsd:sequence*/ );
185 extensionElem.appendChild( sequenceElem );
186
187 //xsd:element
188 if ( layer->isSpatial() )
189 {
190 QDomElement geomElem = doc.createElement( u"element"_s /*xsd:element*/ );
191 geomElem.setAttribute( u"name"_s, u"geometry"_s );
192 geomElem.setAttribute( u"type"_s, getGmlGeometryType( layer ) );
193 geomElem.setAttribute( u"minOccurs"_s, u"0"_s );
194 geomElem.setAttribute( u"maxOccurs"_s, u"1"_s );
195 sequenceElem.appendChild( geomElem );
196 }
197
198 //Attributes
199 const QgsFields fields = layer->fields();
200 //hidden attributes for this layer
201 for ( int idx = 0; idx < fields.count(); ++idx )
202 {
203 const QgsField field = fields.at( idx );
204 //skip attribute if excluded from WFS publication
206 {
207 continue;
208 }
209
210 QString attributeName, attributeType;
211
212 // Defined in qgswfsdescribefeaturetype.h
213 getFieldAttributes( field, attributeName, attributeType );
214
215 //xsd:element
216 QDomElement attElem = doc.createElement( u"element"_s /*xsd:element*/ );
217
218 attElem.setAttribute( u"name"_s, attributeName );
219 attElem.setAttribute( u"type"_s, attributeType );
220
222 {
223 attElem.setAttribute( u"nillable"_s, u"true"_s );
224 }
225
226 sequenceElem.appendChild( attElem );
227
228 const QString alias = field.alias();
229 if ( !alias.isEmpty() )
230 {
231 attElem.setAttribute( u"alias"_s, alias );
232 }
233 }
234}
235
236QString QgsWfsDescribeFeatureTypeGml::getGmlGeometryType( const QgsVectorLayer *layer ) const
237{
238 const Qgis::WkbType wkbType = layer->wkbType();
239 switch ( wfsParameters.outputFormat() )
240 {
241 case QgsWfsParameters::Format::GML2:
242 switch ( wkbType )
243 {
247 return u"gml:PointPropertyType"_s;
248
252 return u"gml:LineStringPropertyType"_s;
253
257 return u"gml:PolygonPropertyType"_s;
258
262 return u"gml:MultiPointPropertyType"_s;
263
269 return u"gml:MultiLineStringPropertyType"_s;
270
276 return u"gml:MultiPolygonPropertyType"_s;
277
278 default:
279 return u"gml:GeometryPropertyType"_s;
280 }
281 case QgsWfsParameters::Format::GML3:
282 switch ( wkbType )
283 {
287 return u"gml:PointPropertyType"_s;
288
292 return u"gml:LineStringPropertyType"_s;
293
297 return u"gml:PolygonPropertyType"_s;
298
301 return u"gml:MultiPointPropertyType"_s;
302
308 return u"gml:MultiCurvePropertyType"_s;
309
314 return u"gml:MultiSurfacePropertyType"_s;
315
316 default:
317 return u"gml:GeometryPropertyType"_s;
318 }
319 default:
320 return u"gml:GeometryPropertyType"_s;
321 }
322}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ LineString25D
LineString25D.
Definition qgis.h:362
@ MultiPointZ
MultiPointZ.
Definition qgis.h:317
@ Point
Point.
Definition qgis.h:296
@ LineString
LineString.
Definition qgis.h:297
@ MultiPolygon25D
MultiPolygon25D.
Definition qgis.h:366
@ MultiPoint
MultiPoint.
Definition qgis.h:300
@ Polygon
Polygon.
Definition qgis.h:298
@ MultiLineString25D
MultiLineString25D.
Definition qgis.h:365
@ MultiPolygon
MultiPolygon.
Definition qgis.h:302
@ MultiSurfaceZ
MultiSurfaceZ.
Definition qgis.h:325
@ MultiLineString
MultiLineString.
Definition qgis.h:301
@ MultiPoint25D
MultiPoint25D.
Definition qgis.h:364
@ PointZ
PointZ.
Definition qgis.h:313
@ MultiLineStringZ
MultiLineStringZ.
Definition qgis.h:318
@ MultiPolygonZ
MultiPolygonZ.
Definition qgis.h:319
@ MultiCurve
MultiCurve.
Definition qgis.h:307
@ MultiCurveZ
MultiCurveZ.
Definition qgis.h:324
@ Point25D
Point25D.
Definition qgis.h:361
@ LineStringZ
LineStringZ.
Definition qgis.h:314
@ MultiSurface
MultiSurface.
Definition qgis.h:308
@ PolygonZ
PolygonZ.
Definition qgis.h:315
@ Polygon25D
Polygon25D.
Definition qgis.h:363
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
Definition qgis.h:1804
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.
@ ConstraintNotNull
Field may not be null.
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:69
QString alias
Definition qgsfield.h:66
QgsFieldConstraints constraints
Definition qgsfield.h:68
int count
Definition qgsfields.h:50
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QString wfsTypeName() const
Returns WFS typename for the layer.
Base class for all map layer types.
Definition qgsmaplayer.h:83
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
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.
Defines 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.
static QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
Defines requests passed to QgsService classes.
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...
Represents a vector layer which manages a vector based dataset.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
void writeDescribeFeatureType(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response) const
Output GML response.
QgsWfsDescribeFeatureTypeGml(const QgsWfs::QgsWfsParameters wfsParams)
Constructor.
Provides an interface to retrieve and manipulate WFS parameters received from the client.
Format outputFormat() const
Returns format.
WMS implementation.
Definition qgswfs.cpp:39
void getFieldAttributes(const QgsField &field, QString &fieldName, QString &fieldType)
Helper for returning the field type and type name.
const QString QGS_NAMESPACE
Definition qgswfsutils.h:76
QStringList getRequestTypeNames(const QgsServerRequest &request, const QgsWfsParameters &wfsParams)
Helper for returning typename list from the request.
#define GML_NAMESPACE
#define OGC_NAMESPACE