QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgsmessagelog.h"
20#include <QRegularExpression>
21#include <QRegularExpressionMatch>
22
23namespace QgsWfs
24{
25 //
26 // QgsWfsParameter
27 //
29 const QVariant::Type type,
30 const QVariant defaultValue )
31 : QgsServerParameterDefinition( type, defaultValue )
32 , mName( name )
33 {
34 }
35
37 {
38 bool ok = false;
39 const int val = QgsServerParameterDefinition::toInt( ok );
40
41 if ( !ok )
42 {
43 raiseError();
44 }
45
46 return val;
47 }
48
50 {
51 QString value = toString();
52 const QStringList corners = mValue.toString().split( ',' );
53 if ( corners.size() == 5 )
54 {
55 value.resize( value.size() - corners[4].size() - 1 );
56 }
57
59 param.mValue = QVariant( value );
60
61 bool ok = false;
62 const QgsRectangle rectangle = param.toRectangle( ok );
63
64 if ( !ok )
65 {
66 const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
68 }
69
70 return rectangle;
71 }
72
73 QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
74 {
75 QStringList theList;
76
77 const QString val = mValue.toString();
78 if ( val.isEmpty() )
79 return theList;
80
81 if ( exp.isEmpty() )
82 theList << val;
83 else
84 {
85 const QRegularExpression rx( exp );
86 QRegularExpressionMatchIterator matchIt = rx.globalMatch( val );
87 if ( !matchIt.hasNext() )
88 {
89 theList << val;
90 }
91 else
92 {
93 while ( matchIt.hasNext() )
94 {
95 const QRegularExpressionMatch match = matchIt.next();
96 if ( match.hasMatch() )
97 {
98 QStringList matches = match.capturedTexts();
99 matches.pop_front(); // remove whole match
100 theList.append( matches );
101 }
102 }
103 }
104 }
105
106 return theList;
107 }
108
110 {
111 const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
113 }
114
116 {
117 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
118 return metaEnum.valueToKey( name );
119 }
120
122 {
123 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
124 return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
125 }
126
127 //
128 // QgsWfsParameters
129 //
132 {
133 // Available version number
134 mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
135 mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
136
138 save( pOutputFormat );
139
141 save( pResultType );
142
144 save( pPropertyName );
145
147 QVariant::Int,
148 QVariant( -1 ) );
149 save( pMaxFeatures );
150
152 QVariant::Int,
153 QVariant( 0 ) );
154 save( pStartIndex );
155
157 save( pSrsName );
158
160 save( pTypeName );
161
163 save( pFeatureId );
164
166 save( pFilter );
167
169 save( pBbox );
170
172 save( pSortBy );
173
175 save( pExpFilter );
176
178 save( pGeometryName );
179 }
180
183 {
184 load( parameters.urlQuery() );
185 }
186
187 bool QgsWfsParameters::loadParameter( const QString &key, const QString &value )
188 {
189 bool loaded = false;
190
192 if ( name >= 0 )
193 {
194 mWfsParameters[name].mValue = value;
195 if ( ! mWfsParameters[name].isValid() )
196 {
197 mWfsParameters[name].raiseError();
198 }
199
200 loaded = true;
201 }
202
203 return loaded;
204 }
205
206 void QgsWfsParameters::save( const QgsWfsParameter &parameter )
207 {
208 mWfsParameters[ parameter.mName ] = parameter;
209 }
210
212 {
213 log( "WFS Request parameters:" );
214 const auto map = mWfsParameters.toStdMap();
215 for ( const auto &parameter : map )
216 {
217 const QString value = parameter.second.toString();
218
219 if ( ! value.isEmpty() )
220 {
221 const QString name = QgsWfsParameter::name( parameter.first );
222 log( QStringLiteral( " - %1 : %2" ).arg( name, value ) );
223 }
224 }
225
226 if ( !version().isEmpty() )
227 log( QStringLiteral( " - VERSION : %1" ).arg( version() ) );
228 }
229
231 {
232 return mWfsParameters[ QgsWfsParameter::OUTPUTFORMAT ].toString();
233 }
234
236 {
237 const QString fStr = outputFormatAsString();
238
239 if ( fStr.isEmpty() )
240 {
241 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
242 return Format::GML3;
243 else
244 return Format::GML2;
245 }
246
247 Format f = Format::NONE;
248 if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/2.1.2" ), Qt::CaseInsensitive ) == 0 )
249 f = Format::GML2;
250 else if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/3.1.1" ), Qt::CaseInsensitive ) == 0 )
251 f = Format::GML3;
252 else if ( fStr.compare( QLatin1String( "application/vnd.geo+json" ), Qt::CaseInsensitive ) == 0 ||
253 // Needs to check for space too, because a + sign in the query string is interpreted as a space
254 fStr.compare( QLatin1String( "application/vnd.geo json" ), Qt::CaseInsensitive ) == 0 ||
255 fStr.compare( QLatin1String( "application/geo+json" ), Qt::CaseInsensitive ) == 0 ||
256 fStr.compare( QLatin1String( "application/geo json" ), Qt::CaseInsensitive ) == 0 ||
257 fStr.compare( QLatin1String( "application/json" ), Qt::CaseInsensitive ) == 0 ||
258 fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0
259 )
260 f = Format::GeoJSON;
261 else if ( fStr.compare( QLatin1String( "gml2" ), Qt::CaseInsensitive ) == 0 )
262 f = Format::GML2;
263 else if ( fStr.compare( QLatin1String( "gml3" ), Qt::CaseInsensitive ) == 0 )
264 f = Format::GML3;
265
266 if ( f == Format::NONE &&
267 request().compare( QLatin1String( "describefeaturetype" ), Qt::CaseInsensitive ) == 0 &&
268 fStr.compare( QLatin1String( "xmlschema" ), Qt::CaseInsensitive ) == 0 )
269 {
270 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
271 return Format::GML3;
272 else
273 return Format::GML2;
274 }
275
276 return f;
277 }
278
280 {
281 return mWfsParameters[ QgsWfsParameter::RESULTTYPE ].toString();
282 }
283
285 {
286 const QString rtStr = resultTypeAsString();
287 if ( rtStr.isEmpty() )
288 return ResultType::RESULTS;
289
290 ResultType rt = ResultType::RESULTS;
291 if ( rtStr.compare( QLatin1String( "hits" ), Qt::CaseInsensitive ) == 0 )
292 rt = ResultType::HITS;
293 return rt;
294 }
295
297 {
298 return mWfsParameters[ QgsWfsParameter::PROPERTYNAME ].toStringListWithExp();
299 }
300
302 {
303 return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toString();
304 }
305
307 {
308 return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toInt();
309 }
310
312 {
313 return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toString();
314 }
315
317 {
318 return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toInt();
319 }
320
322 {
323 return mWfsParameters[ QgsWfsParameter::SRSNAME ].toString();
324 }
325
326 QStringList QgsWfsParameters::typeNames() const
327 {
328 return mWfsParameters[ QgsWfsParameter::TYPENAME ].toStringList();
329 }
330
332 {
333 return mWfsParameters[ QgsWfsParameter::FEATUREID ].toStringList();
334 }
335
336 QStringList QgsWfsParameters::filters() const
337 {
338 return mWfsParameters[ QgsWfsParameter::FILTER ].toStringListWithExp();
339 }
340
342 {
343 return mWfsParameters[ QgsWfsParameter::BBOX ].toString();
344 }
345
347 {
348 return mWfsParameters[ QgsWfsParameter::BBOX ].toRectangle();
349 }
350
351 QStringList QgsWfsParameters::sortBy() const
352 {
353 return mWfsParameters[ QgsWfsParameter::SORTBY ].toStringListWithExp();
354 }
355
357 {
358 return mWfsParameters[ QgsWfsParameter::EXP_FILTER ].toExpressionList();
359 }
360
362 {
363 return mWfsParameters[ QgsWfsParameter::GEOMETRYNAME ].toString();
364 }
365
367 {
368 const QString vStr = version();
370
371 if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
372 version = QgsProjectVersion( vStr );
373 else
374 version = QgsProjectVersion( 1, 1, 0 ); // default value
375
376 return version;
377 }
378
379 void QgsWfsParameters::log( const QString &msg ) const
380 {
381 QgsMessageLog::logMessage( msg, "Server", Qgis::MessageLevel::Info );
382 }
383}
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.
Definition: qgsrectangle.h:42
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.
QgsWfsParameter(const QgsWfsParameter::Name name=QgsWfsParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsWfsParameter.
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.
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