QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
20#include "qgsmessagelog.h"
21
22#include <QRegularExpression>
23#include <QRegularExpressionMatch>
24#include <QString>
25
26#include "moc_qgswfsparameters.cpp"
27
28using namespace Qt::StringLiterals;
29
30namespace QgsWfs
31{
32 //
33 // QgsWfsParameter
34 //
35 QgsWfsParameter::QgsWfsParameter( const QgsWfsParameter::Name name, const QMetaType::Type type, const QVariant defaultValue )
36 : QgsServerParameterDefinition( type, defaultValue )
37 , mName( name )
38 {}
39
41 {
42 bool ok = false;
43 const int val = QgsServerParameterDefinition::toInt( ok );
44
45 if ( !ok )
46 {
47 raiseError();
48 }
49
50 return val;
51 }
52
54 {
55 QString value = toString();
56 const QStringList corners = mValue.toString().split( ',' );
57 if ( corners.size() == 5 )
58 {
59 value.resize( value.size() - corners[4].size() - 1 );
60 }
61
63 param.mValue = QVariant( value );
64
65 bool ok = false;
66 const QgsRectangle rectangle = param.toRectangle( ok );
67
68 if ( !ok )
69 {
70 const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
72 }
73
74 return rectangle;
75 }
76
77 QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
78 {
79 QStringList theList;
80
81 const QString val = mValue.toString();
82 if ( val.isEmpty() )
83 return theList;
84
85 if ( exp.isEmpty() )
86 theList << val;
87 else
88 {
89 const QRegularExpression rx( exp );
90 QRegularExpressionMatchIterator matchIt = rx.globalMatch( val );
91 if ( !matchIt.hasNext() )
92 {
93 theList << val;
94 }
95 else
96 {
97 while ( matchIt.hasNext() )
98 {
99 const QRegularExpressionMatch match = matchIt.next();
100 if ( match.hasMatch() )
101 {
102 QStringList matches = match.capturedTexts();
103 matches.pop_front(); // remove whole match
104 theList.append( matches );
105 }
106 }
107 }
108 }
109
110 return theList;
111 }
112
114 {
115 const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
117 }
118
120 {
121 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
122 return metaEnum.valueToKey( name );
123 }
124
126 {
127 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
128 return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
129 }
130
131 //
132 // QgsWfsParameters
133 //
136 {
137 // Available version number
138 mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
139 mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
140
142 save( pOutputFormat );
143
145 save( pResultType );
146
148 save( pPropertyName );
149
150 const QgsWfsParameter pMaxFeatures = QgsWfsParameter( QgsWfsParameter::MAXFEATURES, QMetaType::Type::Int, QVariant( -1 ) );
151 save( pMaxFeatures );
152
153 const QgsWfsParameter pStartIndex = QgsWfsParameter( QgsWfsParameter::STARTINDEX, QMetaType::Type::Int, 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( u" - %1 : %2"_s.arg( name, value ) );
223 }
224 }
225
226 if ( !version().isEmpty() )
227 log( u" - VERSION : %1"_s.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
248 if ( fStr.compare( "text/xml; subtype=gml/2.1.2"_L1, Qt::CaseInsensitive ) == 0 )
249 f = Format::GML2;
250 else if ( fStr.compare( "text/xml; subtype=gml/3.1.1"_L1, Qt::CaseInsensitive ) == 0 )
251 f = Format::GML3;
252 else if ( fStr.compare( "application/vnd.geo+json"_L1, Qt::CaseInsensitive ) == 0
253 ||
254 // Needs to check for space too, because a + sign in the query string is interpreted as a space
255 fStr.compare( "application/vnd.geo json"_L1, Qt::CaseInsensitive ) == 0
256 || fStr.compare( "application/geo+json"_L1, Qt::CaseInsensitive ) == 0
257 || fStr.compare( "application/geo json"_L1, Qt::CaseInsensitive ) == 0
258 || fStr.compare( "application/json"_L1, Qt::CaseInsensitive ) == 0
259 || fStr.compare( "geojson"_L1, Qt::CaseInsensitive ) == 0 )
260 f = Format::GeoJSON;
261 else if ( fStr.compare( "gml2"_L1, Qt::CaseInsensitive ) == 0 )
262 f = Format::GML2;
263 else if ( fStr.compare( "gml3"_L1, Qt::CaseInsensitive ) == 0 )
264 f = Format::GML3;
265
266 if ( f == Format::NONE && request().compare( "describefeaturetype"_L1, Qt::CaseInsensitive ) == 0 && fStr.compare( "xmlschema"_L1, Qt::CaseInsensitive ) == 0 )
267 {
268 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
269 return Format::GML3;
270 else
271 return Format::GML2;
272 }
273
274 return f;
275 }
276
278 {
279 return mWfsParameters[QgsWfsParameter::RESULTTYPE].toString();
280 }
281
283 {
284 const QString rtStr = resultTypeAsString();
285 if ( rtStr.isEmpty() )
286 return ResultType::RESULTS;
287
289 if ( rtStr.compare( "hits"_L1, Qt::CaseInsensitive ) == 0 )
290 rt = ResultType::HITS;
291 return rt;
292 }
293
295 {
296 return mWfsParameters[QgsWfsParameter::PROPERTYNAME].toStringListWithExp();
297 }
298
300 {
301 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toString();
302 }
303
305 {
306 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toInt();
307 }
308
310 {
311 return mWfsParameters[QgsWfsParameter::STARTINDEX].toString();
312 }
313
315 {
316 return mWfsParameters[QgsWfsParameter::STARTINDEX].toInt();
317 }
318
320 {
321 return mWfsParameters[QgsWfsParameter::SRSNAME].toString();
322 }
323
324 QStringList QgsWfsParameters::typeNames() const
325 {
326 return mWfsParameters[QgsWfsParameter::TYPENAME].toStringList();
327 }
328
330 {
331 return mWfsParameters[QgsWfsParameter::FEATUREID].toStringList();
332 }
333
334 QStringList QgsWfsParameters::filters() const
335 {
336 return mWfsParameters[QgsWfsParameter::FILTER].toStringListWithExp();
337 }
338
340 {
341 return mWfsParameters[QgsWfsParameter::BBOX].toString();
342 }
343
345 {
346 return mWfsParameters[QgsWfsParameter::BBOX].toRectangle();
347 }
348
349 QStringList QgsWfsParameters::sortBy() const
350 {
351 return mWfsParameters[QgsWfsParameter::SORTBY].toStringListWithExp();
352 }
353
355 {
356 return mWfsParameters[QgsWfsParameter::EXP_FILTER].toExpressionList();
357 }
358
360 {
361 return mWfsParameters[QgsWfsParameter::GEOMETRYNAME].toString();
362 }
363
365 {
366 const QString vStr = version();
368
369 if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
370 version = QgsProjectVersion( vStr );
371 else
372 version = QgsProjectVersion( 1, 1, 0 ); // default value
373
374 return version;
375 }
376
377 void QgsWfsParameters::log( const QString &msg, const char *file, const char *function, int line ) const
378 {
379 QgsMessageLog::logMessage( msg, "Server", Qgis::MessageLevel::Info, true, file, function, line );
380 }
381} // namespace QgsWfs
@ Info
Information message.
Definition qgis.h:161
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Describes 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 rounded to the spec...
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
QgsServerParameterDefinition(const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
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.
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.
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.
QgsWfsParameters(const QgsServerParameters &parameters)
Constructor for WFS parameters with specific values.
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:39