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