QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
41void QgsWfsDescribeFeatureTypeJson::writeDescribeFeatureType( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response ) const
42{
43 const QJsonDocument doc( createDescribeFeatureTypeDocument( serverIface, project, version, request ) );
44
45 response.setHeader( "Content-Type", "application/vnd.geo+json; charset=utf-8" );
46 response.write( doc.toJson( QJsonDocument::Compact ) );
47}
48
49
50QJsonObject QgsWfsDescribeFeatureTypeJson::createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) const
51{
52 Q_UNUSED( version )
53
54
55#ifdef HAVE_SERVER_PYTHON_PLUGINS
56 QgsAccessControl *accessControl = serverIface->accessControls();
57#else
58 ( void ) serverIface;
59#endif
60
61 QJsonObject json;
62 json[u"elementFormDefault"_s] = u"qualified"_s;
63 json[u"targetNamespace"_s] = QGS_NAMESPACE;
64 json[u"targetPrefix"_s] = u"qgs"_s;
65
66 QJsonArray featureTypes;
67
68 QStringList typeNameList = getRequestTypeNames( request, wfsParameters );
69
70 const QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
71 for ( int i = 0; i < wfsLayerIds.size(); ++i )
72 {
73 QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) );
74 if ( !layer )
75 {
76 continue;
77 }
78
79 const QString name = layer->serverProperties()->wfsTypeName();
80
81 if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) )
82 {
83 continue;
84 }
85#ifdef HAVE_SERVER_PYTHON_PLUGINS
86 if ( accessControl && !accessControl->layerReadPermission( layer ) )
87 {
88 if ( !typeNameList.isEmpty() )
89 {
90 throw QgsSecurityAccessException( u"Feature access permission denied"_s );
91 }
92 else
93 {
94 continue;
95 }
96 }
97#endif
98 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
99 QgsVectorDataProvider *provider = vLayer->dataProvider();
100 if ( !provider )
101 {
102 continue;
103 }
104
105 featureTypes.append( schemaLayerToJson( const_cast<QgsVectorLayer *>( vLayer ) ) );
106 }
107
108 json[u"featureTypes"_s] = featureTypes;
109 return json;
110}
111
112QJsonObject QgsWfsDescribeFeatureTypeJson::schemaLayerToJson( const QgsVectorLayer *layer ) const
113{
114 const QString typeName = layer->serverProperties()->wfsTypeName();
115
116 QJsonObject json;
117 QJsonArray properties;
118
119 json[u"typeName"_s] = typeName;
120
121 if ( layer->isSpatial() )
122 {
123 QString geomType, geomLocalType;
124 getGeometryType( layer, geomType, geomLocalType );
125
126 QJsonObject property;
127 property[u"name"_s] = u"geometry"_s;
128 property[u"minOccurs"_s] = u"0"_s;
129 property[u"maxOccurs"_s] = u"1"_s;
130 property[u"type"_s] = geomType;
131
132
133 if ( !geomLocalType.isEmpty() )
134 {
135 property[u"localType"_s] = geomLocalType;
136 }
137 properties.append( property );
138 }
139
140 //Attributes
141 const QgsFields fields = layer->fields();
142 //hidden attributes for this layer
143 for ( int idx = 0; idx < fields.count(); ++idx )
144 {
145 const QgsField field = fields.at( idx );
146 //skip attribute if excluded from WFS publication
148 {
149 continue;
150 }
151
152 QString attributeName, attributeType;
153
154 // Defined in qgswfsdescribefeaturetype.h
155 getFieldAttributes( field, attributeName, attributeType );
156
157 QJsonObject property;
158 property[u"name"_s] = attributeName;
159 property[u"type"_s] = attributeType;
160 property[u"localType"_s] = attributeType;
161
163 {
164 property[u"nillable"_s] = "true";
165 }
166
167 const QString alias = field.alias();
168 if ( !alias.isEmpty() )
169 {
170 property[u"alias"_s] = alias;
171 }
172
173 properties.append( property );
174 }
175
176 json[u"properties"_s] = properties;
177 return json;
178}
179
180void QgsWfsDescribeFeatureTypeJson::getGeometryType( const QgsVectorLayer *layer, QString &geomType, QString &geomLocalType ) const
181{
182 const Qgis::WkbType wkbType = layer->wkbType();
183 switch ( wkbType )
184 {
187 geomType = u"gml:PointPropertyType"_s;
188 geomLocalType = u"Point"_s;
189 break;
190
193 geomType = u"gml:LineStringPropertyType"_s;
194 geomLocalType = u"LineString"_s;
195 break;
196
199 geomType = u"gml:PolygonPropertyType"_s;
200 geomLocalType = u"Polygon"_s;
201 break;
202
205 geomType = u"gml:MultiPointPropertyType"_s;
206 geomLocalType = u"MultiPoint"_s;
207 break;
208
212 geomType = u"gml:MultiCurvePropertyType"_s;
213 geomLocalType = u"MultiCurve"_s;
214 break;
215
219 geomType = u"gml:MultiSurfacePropertyType"_s;
220 geomLocalType = u"MultiSurface"_s;
221 break;
222
223 default:
224 geomType = u"gml:GeometryPropertyType"_s;
225 }
226}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ LineString25D
LineString25D.
Definition qgis.h:348
@ Point
Point.
Definition qgis.h:282
@ LineString
LineString.
Definition qgis.h:283
@ MultiPolygon25D
MultiPolygon25D.
Definition qgis.h:352
@ MultiPoint
MultiPoint.
Definition qgis.h:286
@ Polygon
Polygon.
Definition qgis.h:284
@ MultiLineString25D
MultiLineString25D.
Definition qgis.h:351
@ MultiPolygon
MultiPolygon.
Definition qgis.h:288
@ MultiLineString
MultiLineString.
Definition qgis.h:287
@ MultiPoint25D
MultiPoint25D.
Definition qgis.h:350
@ MultiCurve
MultiCurve.
Definition qgis.h:293
@ Point25D
Point25D.
Definition qgis.h:347
@ MultiSurface
MultiSurface.
Definition qgis.h:294
@ Polygon25D
Polygon25D.
Definition qgis.h:349
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
Definition qgis.h:1783
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:40
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.