QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
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 ***************************************************************************/
23
25#include "qgsproject.h"
27#include "qgsvectorlayer.h"
30#include "qgswfsparameters.h"
31#include "qgswfsutils.h"
32
33#include <QDomDocument>
34#include <QDomElement>
35#include <QString>
36
37using namespace Qt::StringLiterals;
38
39namespace QgsWfs
40{
41 void writeDescribeFeatureType( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response )
42 {
43 const QgsWfsParameters wfsParameters( QUrlQuery( request.url() ) );
44 const QgsWfsParameters::Format oFormat = wfsParameters.outputFormat();
45
46 // test oFormat
47 switch ( oFormat )
48 {
51 {
52 auto formatter = QgsWfsDescribeFeatureTypeGml( wfsParameters );
53 formatter.writeDescribeFeatureType( serverIface, project, version, request, response );
54 }
55 break;
56
58 {
59 auto formatter = QgsWfsDescribeFeatureTypeJson( wfsParameters );
60 formatter.writeDescribeFeatureType( serverIface, project, version, request, response );
61 }
62 break;
63
64 default:
65 throw QgsBadRequestException( u"Invalid WFS Parameter"_s, u"OUTPUTFORMAT %1 is not supported"_s.arg( wfsParameters.outputFormatAsString() ) );
66 }
67 }
68
69 QStringList getRequestTypeNames( const QgsServerRequest &request, const QgsWfsParameters &wfsParams )
70 {
71 QStringList typeNameList;
72 QDomDocument queryDoc;
73 QString errorMsg;
74 if ( queryDoc.setContent( request.data(), true, &errorMsg ) )
75 {
76 //read doc
77 const QDomElement queryDocElem = queryDoc.documentElement();
78 const QDomNodeList docChildNodes = queryDocElem.childNodes();
79 if ( docChildNodes.size() )
80 {
81 for ( int i = 0; i < docChildNodes.size(); i++ )
82 {
83 const QDomElement docChildElem = docChildNodes.at( i ).toElement();
84 if ( docChildElem.tagName() == "TypeName"_L1 )
85 {
86 const QString typeName = docChildElem.text().trimmed();
87 if ( typeName.contains( ':' ) )
88 typeNameList << typeName.section( ':', 1, 1 );
89 else
90 typeNameList << typeName;
91 }
92 }
93 }
94 }
95 else
96 {
97 typeNameList = wfsParams.typeNames();
98 }
99
100 return typeNameList;
101 }
102
103
104 void getFieldAttributes( const QgsField &field, QString &fieldName, QString &fieldType )
105 {
106 fieldName = field.name();
107
108 const thread_local QRegularExpression sCleanTagNameRegExp( u"[^\\w\\.-_]"_s, QRegularExpression::PatternOption::UseUnicodePropertiesOption );
109 fieldName.replace( ' ', '_' ).replace( sCleanTagNameRegExp, QString() );
110
111 const QMetaType::Type attributeType = field.type();
112
113 if ( attributeType == QMetaType::Type::Int )
114 {
115 fieldType = u"int"_s;
116 }
117 else if ( attributeType == QMetaType::Type::UInt )
118 {
119 fieldType = u"unsignedInt"_s;
120 }
121 else if ( attributeType == QMetaType::Type::LongLong )
122 {
123 fieldType = u"long"_s;
124 }
125 else if ( attributeType == QMetaType::Type::ULongLong )
126 {
127 fieldType = u"unsignedLong"_s;
128 }
129 else if ( attributeType == QMetaType::Type::Double )
130 {
131 if ( field.length() > 0 && field.precision() == 0 )
132 fieldType = u"integer"_s;
133 else
134 fieldType = u"decimal"_s;
135 }
136 else if ( attributeType == QMetaType::Type::Bool )
137 {
138 fieldType = u"boolean"_s;
139 }
140 else if ( attributeType == QMetaType::Type::QDate )
141 {
142 fieldType = u"date"_s;
143 }
144 else if ( attributeType == QMetaType::Type::QTime )
145 {
146 fieldType = u"time"_s;
147 }
148 else if ( attributeType == QMetaType::Type::QDateTime )
149 {
150 fieldType = u"dateTime"_s;
151 }
152 else
153 {
154 fieldType = u"string"_s;
155 }
156
157 const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
158 if ( setup.type() == "DateTime"_L1 )
159 {
160 // Get editor widget setup config
161 const QVariantMap config = setup.config();
162 // Get field format from editor widget setup config
163 const QString fieldFormat = config.value(
164 u"field_format"_s,
166 )
167 .toString();
168 // Define type from field format
169 if ( fieldFormat == QgsDateTimeFieldFormatter::TIME_FORMAT ) // const QgsDateTimeFieldFormatter::TIME_FORMAT
170 fieldType = u"time"_s;
171 else if ( fieldFormat == QgsDateTimeFieldFormatter::DATE_FORMAT ) // const QgsDateTimeFieldFormatter::DATE_FORMAT since QGIS 3.30
172 fieldType = u"date"_s;
173 else if ( fieldFormat == QgsDateTimeFieldFormatter::DATETIME_FORMAT ) // const QgsDateTimeFieldFormatter::DATETIME_FORMAT since QGIS 3.30
174 fieldType = u"dateTime"_s;
175 else if ( fieldFormat == QgsDateTimeFieldFormatter::QT_ISO_FORMAT )
176 fieldType = u"dateTime"_s;
177 }
178 else if ( setup.type() == "Range"_L1 )
179 {
180 const QVariantMap config = setup.config();
181 if ( config.contains( u"Precision"_s ) )
182 {
183 // if precision in range config is not the same as the attributePrec
184 // we need to update type
185 bool ok;
186 const int configPrec( config[u"Precision"_s].toInt( &ok ) );
187 if ( ok && configPrec != field.precision() )
188 {
189 if ( configPrec == 0 )
190 fieldType = u"integer"_s;
191 else
192 fieldType = u"decimal"_s;
193 }
194 }
195 }
196 }
197
198
199} // namespace QgsWfs
static const QString QT_ISO_FORMAT
Date time format was localized by applyLocaleChange before QGIS 3.30.
static QString defaultFormat(QMetaType::Type type)
Gets the default format in function of the type.
static const QString TIME_FORMAT
Date format was localized by applyLocaleChange before QGIS 3.30.
Holder for the widget type and its configuration for a field.
QString type() const
Returns the widget type to use.
QVariantMap config() const
Returns the widget configuration.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QMetaType::Type type
Definition qgsfield.h:63
QString name
Definition qgsfield.h:65
int precision
Definition qgsfield.h:62
int length
Definition qgsfield.h:61
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:751
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
Defines interfaces exposed by QGIS Server and made available to plugins.
Defines requests passed to QgsService classes.
QUrl url() const
Returns the request URL as seen by QGIS server.
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.
Defines the response interface passed to QgsService.
GML output formatter for DescribeFeatureType.
Json output formatter for DescribeFeatureType.
Exception thrown in case of malformed request.
Provides an interface to retrieve and manipulate WFS parameters received from the client.
QStringList typeNames() const
Returns TYPENAME parameter as list.
QString outputFormatAsString() const
Returns OUTPUTFORMAT parameter as a string.
Format
Output format for the response.
Format outputFormat() const
Returns format.
WMS implementation.
Definition qgswfs.cpp:40
void getFieldAttributes(const QgsField &field, QString &fieldName, QString &fieldType)
Helper for returning the field type and type name.
void writeDescribeFeatureType(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output WFS GetCapabilities response.
QStringList getRequestTypeNames(const QgsServerRequest &request, const QgsWfsParameters &wfsParams)
Helper for returning typename list from the request.