QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgswfsdescribefeaturetypejson.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswfsdescribefeaturetypegeojson.cpp
3 ------------------------------------
4 begin : December 09 , 2022
5 copyright : (C) 2022 by David Marteau
6 email : david dot marteau at 3liz dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18
20#include "qgsjsonutils.h"
21#include "qgsproject.h"
23#include "qgsvectorlayer.h"
25#include "qgswfsparameters.h"
26#include "qgswfsutils.h"
27
28#include <QJsonArray>
29#include <QJsonDocument>
30#include <QJsonObject>
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 const QJsonDocument doc( createDescribeFeatureTypeDocument( serverIface, project, version, request ) );
46
47 response.setHeader( "Content-Type", "application/vnd.geo+json; charset=utf-8" );
48 response.write( doc.toJson( QJsonDocument::Compact ) );
49}
50
51
52QJsonObject QgsWfsDescribeFeatureTypeJson::createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) const
53{
54 Q_UNUSED( version )
55
56
57#ifdef HAVE_SERVER_PYTHON_PLUGINS
58 QgsAccessControl *accessControl = serverIface->accessControls();
59#else
60 ( void ) serverIface;
61#endif
62
63 QJsonObject json;
64 json[u"elementFormDefault"_s] = u"qualified"_s;
65 json[u"targetNamespace"_s] = QGS_NAMESPACE;
66 json[u"targetPrefix"_s] = u"qgs"_s;
67
68 QJsonArray featureTypes;
69
70 QStringList typeNameList = getRequestTypeNames( request, wfsParameters );
71
72 const QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
73 for ( int i = 0; i < wfsLayerIds.size(); ++i )
74 {
75 QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) );
76 if ( !layer )
77 {
78 continue;
79 }
80
81 const QString name = layer->serverProperties()->wfsTypeName();
82
83 if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) )
84 {
85 continue;
86 }
87#ifdef HAVE_SERVER_PYTHON_PLUGINS
88 if ( accessControl && !accessControl->layerReadPermission( layer ) )
89 {
90 if ( !typeNameList.isEmpty() )
91 {
92 throw QgsSecurityAccessException( u"Feature access permission denied"_s );
93 }
94 else
95 {
96 continue;
97 }
98 }
99#endif
100 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
101 QgsVectorDataProvider *provider = vLayer->dataProvider();
102 if ( !provider )
103 {
104 continue;
105 }
106
107 featureTypes.append( schemaLayerToJson( const_cast<QgsVectorLayer *>( vLayer ) ) );
108 }
109
110 json[u"featureTypes"_s] = featureTypes;
111 return json;
112}
113
114QJsonObject QgsWfsDescribeFeatureTypeJson::schemaLayerToJson( const QgsVectorLayer *layer ) const
115{
116 const QString typeName = layer->serverProperties()->wfsTypeName();
117
118 QJsonObject json;
119 QJsonArray properties;
120
121 json[u"typeName"_s] = typeName;
122
123 if ( layer->isSpatial() )
124 {
125 QString geomType, geomLocalType;
126 getGeometryType( layer, geomType, geomLocalType );
127
128 QJsonObject property;
129 property[u"name"_s] = u"geometry"_s;
130 property[u"minOccurs"_s] = u"0"_s;
131 property[u"maxOccurs"_s] = u"1"_s;
132 property[u"type"_s] = geomType;
133
134
135 if ( !geomLocalType.isEmpty() )
136 {
137 property[u"localType"_s] = geomLocalType;
138 }
139 properties.append( property );
140 }
141
142 //Attributes
143 const QgsFields fields = layer->fields();
144 //hidden attributes for this layer
145 for ( int idx = 0; idx < fields.count(); ++idx )
146 {
147 const QgsField field = fields.at( idx );
148 //skip attribute if excluded from WFS publication
150 {
151 continue;
152 }
153
154 QString attributeName, attributeType;
155
156 // Defined in qgswfsdescribefeaturetype.h
157 getFieldAttributes( field, attributeName, attributeType );
158
159 QJsonObject property;
160 property[u"name"_s] = attributeName;
161 property[u"type"_s] = attributeType;
162 property[u"localType"_s] = attributeType;
163
165 {
166 property[u"nillable"_s] = "true";
167 }
168
169 const QString alias = field.alias();
170 if ( !alias.isEmpty() )
171 {
172 property[u"alias"_s] = alias;
173 }
174
175 properties.append( property );
176 }
177
178 json[u"properties"_s] = properties;
179 return json;
180}
181
182void QgsWfsDescribeFeatureTypeJson::getGeometryType( const QgsVectorLayer *layer, QString &geomType, QString &geomLocalType ) const
183{
184 const Qgis::WkbType wkbType = layer->wkbType();
185 switch ( wkbType )
186 {
189 geomType = u"gml:PointPropertyType"_s;
190 geomLocalType = u"Point"_s;
191 break;
192
195 geomType = u"gml:LineStringPropertyType"_s;
196 geomLocalType = u"LineString"_s;
197 break;
198
201 geomType = u"gml:PolygonPropertyType"_s;
202 geomLocalType = u"Polygon"_s;
203 break;
204
207 geomType = u"gml:MultiPointPropertyType"_s;
208 geomLocalType = u"MultiPoint"_s;
209 break;
210
214 geomType = u"gml:MultiCurvePropertyType"_s;
215 geomLocalType = u"MultiCurve"_s;
216 break;
217
221 geomType = u"gml:MultiSurfacePropertyType"_s;
222 geomLocalType = u"MultiSurface"_s;
223 break;
224
225 default:
226 geomType = u"gml:GeometryPropertyType"_s;
227 }
228}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ LineString25D
LineString25D.
Definition qgis.h:362
@ 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
@ MultiLineString
MultiLineString.
Definition qgis.h:301
@ MultiPoint25D
MultiPoint25D.
Definition qgis.h:364
@ MultiCurve
MultiCurve.
Definition qgis.h:307
@ Point25D
Point25D.
Definition qgis.h:361
@ MultiSurface
MultiSurface.
Definition qgis.h:308
@ 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.
@ 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.
Defines interfaces exposed by QGIS Server and made available to plugins.
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.
QgsWfsDescribeFeatureTypeJson(const QgsWfs::QgsWfsParameters wfsParams)
Constructor.
void writeDescribeFeatureType(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response) const
Output GeoJson response.
Provides an interface to retrieve and manipulate WFS parameters received from the client.
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.