QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgswfsparameters.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswfsparameters.cpp
3 --------------------
4 begin : Sept 14, 2017
5 copyright : (C) 2017 by René-Luc Dhont
6 email : rldhont 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
18#include "qgswfsparameters.h"
19#include "moc_qgswfsparameters.cpp"
20#include "qgsmessagelog.h"
21#include <QRegularExpression>
22#include <QRegularExpressionMatch>
23
24namespace QgsWfs
25{
26 //
27 // QgsWfsParameter
28 //
29 QgsWfsParameter::QgsWfsParameter( const QgsWfsParameter::Name name, const QMetaType::Type type, const QVariant defaultValue )
30 : QgsServerParameterDefinition( type, defaultValue )
31 , mName( name )
32 {
33 }
34
36 {
37 bool ok = false;
38 const int val = QgsServerParameterDefinition::toInt( ok );
39
40 if ( !ok )
41 {
42 raiseError();
43 }
44
45 return val;
46 }
47
49 {
50 QString value = toString();
51 const QStringList corners = mValue.toString().split( ',' );
52 if ( corners.size() == 5 )
53 {
54 value.resize( value.size() - corners[4].size() - 1 );
55 }
56
58 param.mValue = QVariant( value );
59
60 bool ok = false;
61 const QgsRectangle rectangle = param.toRectangle( ok );
62
63 if ( !ok )
64 {
65 const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
67 }
68
69 return rectangle;
70 }
71
72 QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
73 {
74 QStringList theList;
75
76 const QString val = mValue.toString();
77 if ( val.isEmpty() )
78 return theList;
79
80 if ( exp.isEmpty() )
81 theList << val;
82 else
83 {
84 const QRegularExpression rx( exp );
85 QRegularExpressionMatchIterator matchIt = rx.globalMatch( val );
86 if ( !matchIt.hasNext() )
87 {
88 theList << val;
89 }
90 else
91 {
92 while ( matchIt.hasNext() )
93 {
94 const QRegularExpressionMatch match = matchIt.next();
95 if ( match.hasMatch() )
96 {
97 QStringList matches = match.capturedTexts();
98 matches.pop_front(); // remove whole match
99 theList.append( matches );
100 }
101 }
102 }
103 }
104
105 return theList;
106 }
107
109 {
110 const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
112 }
113
115 {
116 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
117 return metaEnum.valueToKey( name );
118 }
119
121 {
122 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
123 return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
124 }
125
126 //
127 // QgsWfsParameters
128 //
131 {
132 // Available version number
133 mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
134 mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
135
137 save( pOutputFormat );
138
140 save( pResultType );
141
143 save( pPropertyName );
144
145 const QgsWfsParameter pMaxFeatures = QgsWfsParameter( QgsWfsParameter::MAXFEATURES, QMetaType::Type::Int, QVariant( -1 ) );
146 save( pMaxFeatures );
147
148 const QgsWfsParameter pStartIndex = QgsWfsParameter( QgsWfsParameter::STARTINDEX, QMetaType::Type::Int, QVariant( 0 ) );
149 save( pStartIndex );
150
152 save( pSrsName );
153
155 save( pTypeName );
156
158 save( pFeatureId );
159
161 save( pFilter );
162
164 save( pBbox );
165
167 save( pSortBy );
168
170 save( pExpFilter );
171
173 save( pGeometryName );
174 }
175
178 {
179 load( parameters.urlQuery() );
180 }
181
182 bool QgsWfsParameters::loadParameter( const QString &key, const QString &value )
183 {
184 bool loaded = false;
185
187 if ( name >= 0 )
188 {
189 mWfsParameters[name].mValue = value;
190 if ( !mWfsParameters[name].isValid() )
191 {
192 mWfsParameters[name].raiseError();
193 }
194
195 loaded = true;
196 }
197
198 return loaded;
199 }
200
201 void QgsWfsParameters::save( const QgsWfsParameter &parameter )
202 {
203 mWfsParameters[parameter.mName] = parameter;
204 }
205
207 {
208 log( "WFS Request parameters:" );
209 const auto map = mWfsParameters.toStdMap();
210 for ( const auto &parameter : map )
211 {
212 const QString value = parameter.second.toString();
213
214 if ( !value.isEmpty() )
215 {
216 const QString name = QgsWfsParameter::name( parameter.first );
217 log( QStringLiteral( " - %1 : %2" ).arg( name, value ) );
218 }
219 }
220
221 if ( !version().isEmpty() )
222 log( QStringLiteral( " - VERSION : %1" ).arg( version() ) );
223 }
224
226 {
227 return mWfsParameters[QgsWfsParameter::OUTPUTFORMAT].toString();
228 }
229
231 {
232 const QString fStr = outputFormatAsString();
233
234 if ( fStr.isEmpty() )
235 {
236 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
237 return Format::GML3;
238 else
239 return Format::GML2;
240 }
241
243 if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/2.1.2" ), Qt::CaseInsensitive ) == 0 )
244 f = Format::GML2;
245 else if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/3.1.1" ), Qt::CaseInsensitive ) == 0 )
246 f = Format::GML3;
247 else if ( fStr.compare( QLatin1String( "application/vnd.geo+json" ), Qt::CaseInsensitive ) == 0 ||
248 // Needs to check for space too, because a + sign in the query string is interpreted as a space
249 fStr.compare( QLatin1String( "application/vnd.geo json" ), Qt::CaseInsensitive ) == 0 || fStr.compare( QLatin1String( "application/geo+json" ), Qt::CaseInsensitive ) == 0 || fStr.compare( QLatin1String( "application/geo json" ), Qt::CaseInsensitive ) == 0 || fStr.compare( QLatin1String( "application/json" ), Qt::CaseInsensitive ) == 0 || fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0 )
250 f = Format::GeoJSON;
251 else if ( fStr.compare( QLatin1String( "gml2" ), Qt::CaseInsensitive ) == 0 )
252 f = Format::GML2;
253 else if ( fStr.compare( QLatin1String( "gml3" ), Qt::CaseInsensitive ) == 0 )
254 f = Format::GML3;
255
256 if ( f == Format::NONE && request().compare( QLatin1String( "describefeaturetype" ), Qt::CaseInsensitive ) == 0 && fStr.compare( QLatin1String( "xmlschema" ), Qt::CaseInsensitive ) == 0 )
257 {
258 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
259 return Format::GML3;
260 else
261 return Format::GML2;
262 }
263
264 return f;
265 }
266
268 {
269 return mWfsParameters[QgsWfsParameter::RESULTTYPE].toString();
270 }
271
273 {
274 const QString rtStr = resultTypeAsString();
275 if ( rtStr.isEmpty() )
276 return ResultType::RESULTS;
277
279 if ( rtStr.compare( QLatin1String( "hits" ), Qt::CaseInsensitive ) == 0 )
280 rt = ResultType::HITS;
281 return rt;
282 }
283
285 {
286 return mWfsParameters[QgsWfsParameter::PROPERTYNAME].toStringListWithExp();
287 }
288
290 {
291 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toString();
292 }
293
295 {
296 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toInt();
297 }
298
300 {
301 return mWfsParameters[QgsWfsParameter::STARTINDEX].toString();
302 }
303
305 {
306 return mWfsParameters[QgsWfsParameter::STARTINDEX].toInt();
307 }
308
310 {
311 return mWfsParameters[QgsWfsParameter::SRSNAME].toString();
312 }
313
314 QStringList QgsWfsParameters::typeNames() const
315 {
316 return mWfsParameters[QgsWfsParameter::TYPENAME].toStringList();
317 }
318
320 {
321 return mWfsParameters[QgsWfsParameter::FEATUREID].toStringList();
322 }
323
324 QStringList QgsWfsParameters::filters() const
325 {
326 return mWfsParameters[QgsWfsParameter::FILTER].toStringListWithExp();
327 }
328
330 {
331 return mWfsParameters[QgsWfsParameter::BBOX].toString();
332 }
333
335 {
336 return mWfsParameters[QgsWfsParameter::BBOX].toRectangle();
337 }
338
339 QStringList QgsWfsParameters::sortBy() const
340 {
341 return mWfsParameters[QgsWfsParameter::SORTBY].toStringListWithExp();
342 }
343
345 {
346 return mWfsParameters[QgsWfsParameter::EXP_FILTER].toExpressionList();
347 }
348
350 {
351 return mWfsParameters[QgsWfsParameter::GEOMETRYNAME].toString();
352 }
353
355 {
356 const QString vStr = version();
358
359 if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
360 version = QgsProjectVersion( vStr );
361 else
362 version = QgsProjectVersion( 1, 1, 0 ); // default value
363
364 return version;
365 }
366
367 void QgsWfsParameters::log( const QString &msg ) const
368 {
370 }
371} // namespace QgsWfs
@ Info
Information message.
Definition qgis.h:155
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A class to describe the version of a project.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Definition of a parameter with basic conversion methods.
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
QString typeName() const
Returns the type of the parameter as a string.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
int toInt(bool &ok) const
Converts the parameter into an integer.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
virtual QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
void load(const QUrlQuery &query)
Loads new parameters.
virtual QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QString value(const QString &key) const
Returns the value of a parameter.
WFS parameter received from the client.
QgsWfsParameter::Name mName
Name
Available parameters for WFS requests.
int toInt() const
Converts the parameter into an integer.
QStringList toStringListWithExp(const QString &exp="\\‍(([^()]+)\\‍)") const
Converts the parameter into a list of string.
void raiseError() const
Raises an error in case of an invalid conversion.
QgsWfsParameter(const QgsWfsParameter::Name name=QgsWfsParameter::UNKNOWN, const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsWfsParameter.
QgsRectangle toRectangle() const
Converts the parameter into a rectangle.
static QString name(const QgsWfsParameter::Name)
Converts a parameter's name into its string representation.
Provides an interface to retrieve and manipulate WFS parameters received from the client.
QgsWfsParameters()
Constructor for WFS parameters with default values only.
int startIndexAsInt() const
Returns STARTINDEX parameter as an int or its default value if not defined.
QString geometryNameAsString() const
Returns GEOMETRYNAME parameter as a string.
QStringList sortBy() const
Returns SORTBY parameter as list.
QStringList typeNames() const
Returns TYPENAME parameter as list.
QStringList expFilters() const
Returns EXP_FILTER parameter as list.
QString maxFeatures() const
Returns MAXFEATURES parameter as a string.
QStringList filters() const
Returns FILTER parameter as list.
QString srsName() const
Returns SRSNAME parameter as a string.
QString resultTypeAsString() const
Returns RESULTTYPE parameter as a string.
int maxFeaturesAsInt() const
Returns MAXFEATURES parameter as an int or its default value if not defined.
QString bbox() const
Returns BBOX if defined or an empty string.
void dump() const
Dumps parameters.
QString outputFormatAsString() const
Returns OUTPUTFORMAT parameter as a string.
ResultType resultType() const
Returns resultType.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
QStringList featureIds() const
Returns FEATUREID parameter as list.
QString startIndex() const
Returns STARTINDEX parameter as a string.
Format
Output format for the response.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
QStringList propertyNames() const
Returns PROPERTYNAME parameter as list.
Format outputFormat() const
Returns format.
WMS implementation.
Definition qgswfs.cpp:36