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