QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 ***************************************************************************/
17#include "qgswfsutils.h"
21#include "qgswfsparameters.h"
22#include "qgsjsonutils.h"
23#include "qgsproject.h"
24#include "qgsvectorlayer.h"
26
27#include <QJsonObject>
28#include <QJsonArray>
29#include <QJsonDocument>
30
31using namespace QgsWfs;
32
34 : wfsParameters( wfsParams )
35{}
36
37void QgsWfsDescribeFeatureTypeJson::writeDescribeFeatureType( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response ) const
38{
39 const QJsonDocument doc( createDescribeFeatureTypeDocument( serverIface, project, version, request ) );
40
41 response.setHeader( "Content-Type", "application/vnd.geo+json; charset=utf-8" );
42 response.write( doc.toJson( QJsonDocument::Compact ) );
43}
44
45
46QJsonObject QgsWfsDescribeFeatureTypeJson::createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) const
47{
48 Q_UNUSED( version )
49
50
51#ifdef HAVE_SERVER_PYTHON_PLUGINS
52 QgsAccessControl *accessControl = serverIface->accessControls();
53#else
54 ( void ) serverIface;
55#endif
56
57 QJsonObject json;
58 json[QStringLiteral( "elementFormDefault" )] = QStringLiteral( "qualified" );
59 json[QStringLiteral( "targetNamespace" )] = QGS_NAMESPACE;
60 json[QStringLiteral( "targetPrefix" )] = QStringLiteral( "qgs" );
61
62 QJsonArray featureTypes;
63
64 QStringList typeNameList = getRequestTypeNames( request, wfsParameters );
65
66 const QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
67 for ( int i = 0; i < wfsLayerIds.size(); ++i )
68 {
69 QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) );
70 if ( !layer )
71 {
72 continue;
73 }
74
75 const QString name = layerTypeName( layer );
76
77 if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) )
78 {
79 continue;
80 }
81#ifdef HAVE_SERVER_PYTHON_PLUGINS
82 if ( accessControl && !accessControl->layerReadPermission( layer ) )
83 {
84 if ( !typeNameList.isEmpty() )
85 {
86 throw QgsSecurityAccessException( QStringLiteral( "Feature access permission denied" ) );
87 }
88 else
89 {
90 continue;
91 }
92 }
93#endif
94 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
95 QgsVectorDataProvider *provider = vLayer->dataProvider();
96 if ( !provider )
97 {
98 continue;
99 }
100
101 featureTypes.append( schemaLayerToJson( const_cast<QgsVectorLayer *>( vLayer ) ) );
102 }
103
104 json[QStringLiteral( "featureTypes" )] = featureTypes;
105 return json;
106}
107
108QJsonObject QgsWfsDescribeFeatureTypeJson::schemaLayerToJson( const QgsVectorLayer *layer ) const
109{
110 const QString typeName = layerTypeName( layer );
111
112 QJsonObject json;
113 QJsonArray properties;
114
115 json[QStringLiteral( "typeName" )] = typeName;
116
117 if ( layer->isSpatial() )
118 {
119 QString geomType, geomLocalType;
120 getGeometryType( layer, geomType, geomLocalType );
121
122 QJsonObject property;
123 property[QStringLiteral( "name" )] = QStringLiteral( "geometry" );
124 property[QStringLiteral( "minOccurs" )] = QStringLiteral( "0" );
125 property[QStringLiteral( "maxOccurs" )] = QStringLiteral( "1" );
126 property[QStringLiteral( "type" )] = geomType;
127
128
129 if ( !geomLocalType.isEmpty() )
130 {
131 property[QStringLiteral( "localType" )] = geomLocalType;
132 }
133 properties.append( property );
134 }
135
136 //Attributes
137 const QgsFields fields = layer->fields();
138 //hidden attributes for this layer
139 for ( int idx = 0; idx < fields.count(); ++idx )
140 {
141 const QgsField field = fields.at( idx );
142 //skip attribute if excluded from WFS publication
144 {
145 continue;
146 }
147
148 QString attributeName, attributeType;
149
150 // Defined in qgswfsdescribefeaturetype.h
151 getFieldAttributes( field, attributeName, attributeType );
152
153 QJsonObject property;
154 property[QStringLiteral( "name" )] = attributeName;
155 property[QStringLiteral( "type" )] = attributeType;
156 property[QStringLiteral( "localType" )] = attributeType;
157
159 {
160 property[QStringLiteral( "nillable" )] = "true";
161 }
162
163 const QString alias = field.alias();
164 if ( !alias.isEmpty() )
165 {
166 property[QStringLiteral( "alias" )] = alias;
167 }
168
169 properties.append( property );
170 }
171
172 json[QStringLiteral( "properties" )] = properties;
173 return json;
174}
175
176void QgsWfsDescribeFeatureTypeJson::getGeometryType( const QgsVectorLayer *layer, QString &geomType, QString &geomLocalType ) const
177{
178 const Qgis::WkbType wkbType = layer->wkbType();
179 switch ( wkbType )
180 {
183 geomType = QStringLiteral( "gml:PointPropertyType" );
184 geomLocalType = QStringLiteral( "Point" );
185 break;
186
189 geomType = QStringLiteral( "gml:LineStringPropertyType" );
190 geomLocalType = QStringLiteral( "LineString" );
191 break;
192
195 geomType = QStringLiteral( "gml:PolygonPropertyType" );
196 geomLocalType = QStringLiteral( "Polygon" );
197 break;
198
201 geomType = QStringLiteral( "gml:MultiPointPropertyType" );
202 geomLocalType = QStringLiteral( "MultiPoint" );
203 break;
204
208 geomType = QStringLiteral( "gml:MultiCurvePropertyType" );
209 geomLocalType = QStringLiteral( "MultiCurve" );
210 break;
211
215 geomType = QStringLiteral( "gml:MultiSurfacePropertyType" );
216 geomLocalType = QStringLiteral( "MultiSurface" );
217 break;
218
219 default:
220 geomType = QStringLiteral( "gml:GeometryPropertyType" );
221 }
222}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ LineString25D
LineString25D.
@ LineString
LineString.
@ MultiPolygon25D
MultiPolygon25D.
@ MultiPoint
MultiPoint.
@ Polygon
Polygon.
@ MultiLineString25D
MultiLineString25D.
@ MultiPolygon
MultiPolygon.
@ MultiLineString
MultiLineString.
@ MultiPoint25D
MultiPoint25D.
@ MultiCurve
MultiCurve.
@ Point25D
Point25D.
@ MultiSurface
MultiSurface.
@ Polygon25D
Polygon25D.
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
A helper class that centralizes restrictions given by all the access control filter plugins.
@ ConstraintNotNull
Field may not be null.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:66
QString alias
Definition qgsfield.h:63
QgsFieldConstraints constraints
Definition qgsfield.h:65
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Base class for all map layer types.
Definition qgsmaplayer.h:76
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
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...
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
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.
Exception thrown when data access violates access controls.
Provides an interface to retrieve and manipulate WFS parameters received from the client.
SERVER_EXPORT QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
WMS implementation.
Definition qgswfs.cpp:36
QString layerTypeName(const QgsMapLayer *layer)
Returns typename from vector layer.
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:77
QStringList getRequestTypeNames(const QgsServerRequest &request, const QgsWfsParameters &wfsParams)
Helper for returning typename list from the request.
const QString & typeName