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