QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 }
40
42 {
43 bool ok = false;
44 const int val = QgsServerParameterDefinition::toInt( ok );
45
46 if ( !ok )
47 {
48 raiseError();
49 }
50
51 return val;
52 }
53
55 {
56 QString value = toString();
57 const QStringList corners = mValue.toString().split( ',' );
58 if ( corners.size() == 5 )
59 {
60 value.resize( value.size() - corners[4].size() - 1 );
61 }
62
64 param.mValue = QVariant( value );
65
66 bool ok = false;
67 const QgsRectangle rectangle = param.toRectangle( ok );
68
69 if ( !ok )
70 {
71 const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
73 }
74
75 return rectangle;
76 }
77
78 QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
79 {
80 QStringList theList;
81
82 const QString val = mValue.toString();
83 if ( val.isEmpty() )
84 return theList;
85
86 if ( exp.isEmpty() )
87 theList << val;
88 else
89 {
90 const QRegularExpression rx( exp );
91 QRegularExpressionMatchIterator matchIt = rx.globalMatch( val );
92 if ( !matchIt.hasNext() )
93 {
94 theList << val;
95 }
96 else
97 {
98 while ( matchIt.hasNext() )
99 {
100 const QRegularExpressionMatch match = matchIt.next();
101 if ( match.hasMatch() )
102 {
103 QStringList matches = match.capturedTexts();
104 matches.pop_front(); // remove whole match
105 theList.append( matches );
106 }
107 }
108 }
109 }
110
111 return theList;
112 }
113
115 {
116 const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
118 }
119
121 {
122 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
123 return metaEnum.valueToKey( name );
124 }
125
127 {
128 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
129 return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
130 }
131
132 //
133 // QgsWfsParameters
134 //
137 {
138 // Available version number
139 mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
140 mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
141
143 save( pOutputFormat );
144
146 save( pResultType );
147
149 save( pPropertyName );
150
151 const QgsWfsParameter pMaxFeatures = QgsWfsParameter( QgsWfsParameter::MAXFEATURES, QMetaType::Type::Int, QVariant( -1 ) );
152 save( pMaxFeatures );
153
154 const QgsWfsParameter pStartIndex = QgsWfsParameter( QgsWfsParameter::STARTINDEX, QMetaType::Type::Int, QVariant( 0 ) );
155 save( pStartIndex );
156
158 save( pSrsName );
159
161 save( pTypeName );
162
164 save( pFeatureId );
165
167 save( pFilter );
168
170 save( pBbox );
171
173 save( pSortBy );
174
176 save( pExpFilter );
177
179 save( pGeometryName );
180 }
181
184 {
185 load( parameters.urlQuery() );
186 }
187
188 bool QgsWfsParameters::loadParameter( const QString &key, const QString &value )
189 {
190 bool loaded = false;
191
193 if ( name >= 0 )
194 {
195 mWfsParameters[name].mValue = value;
196 if ( !mWfsParameters[name].isValid() )
197 {
198 mWfsParameters[name].raiseError();
199 }
200
201 loaded = true;
202 }
203
204 return loaded;
205 }
206
207 void QgsWfsParameters::save( const QgsWfsParameter &parameter )
208 {
209 mWfsParameters[parameter.mName] = parameter;
210 }
211
213 {
214 log( "WFS Request parameters:" );
215 const auto map = mWfsParameters.toStdMap();
216 for ( const auto &parameter : map )
217 {
218 const QString value = parameter.second.toString();
219
220 if ( !value.isEmpty() )
221 {
222 const QString name = QgsWfsParameter::name( parameter.first );
223 log( u" - %1 : %2"_s.arg( name, value ) );
224 }
225 }
226
227 if ( !version().isEmpty() )
228 log( u" - VERSION : %1"_s.arg( version() ) );
229 }
230
232 {
233 return mWfsParameters[QgsWfsParameter::OUTPUTFORMAT].toString();
234 }
235
237 {
238 const QString fStr = outputFormatAsString();
239
240 if ( fStr.isEmpty() )
241 {
242 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
243 return Format::GML3;
244 else
245 return Format::GML2;
246 }
247
249 if ( fStr.compare( "text/xml; subtype=gml/2.1.2"_L1, Qt::CaseInsensitive ) == 0 )
250 f = Format::GML2;
251 else if ( fStr.compare( "text/xml; subtype=gml/3.1.1"_L1, Qt::CaseInsensitive ) == 0 )
252 f = Format::GML3;
253 else if ( fStr.compare( "application/vnd.geo+json"_L1, Qt::CaseInsensitive ) == 0 ||
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 || fStr.compare( "application/geo+json"_L1, Qt::CaseInsensitive ) == 0 || fStr.compare( "application/geo json"_L1, Qt::CaseInsensitive ) == 0 || fStr.compare( "application/json"_L1, Qt::CaseInsensitive ) == 0 || fStr.compare( "geojson"_L1, Qt::CaseInsensitive ) == 0 )
256 f = Format::GeoJSON;
257 else if ( fStr.compare( "gml2"_L1, Qt::CaseInsensitive ) == 0 )
258 f = Format::GML2;
259 else if ( fStr.compare( "gml3"_L1, Qt::CaseInsensitive ) == 0 )
260 f = Format::GML3;
261
262 if ( f == Format::NONE && request().compare( "describefeaturetype"_L1, Qt::CaseInsensitive ) == 0 && fStr.compare( "xmlschema"_L1, Qt::CaseInsensitive ) == 0 )
263 {
264 if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
265 return Format::GML3;
266 else
267 return Format::GML2;
268 }
269
270 return f;
271 }
272
274 {
275 return mWfsParameters[QgsWfsParameter::RESULTTYPE].toString();
276 }
277
279 {
280 const QString rtStr = resultTypeAsString();
281 if ( rtStr.isEmpty() )
282 return ResultType::RESULTS;
283
285 if ( rtStr.compare( "hits"_L1, Qt::CaseInsensitive ) == 0 )
286 rt = ResultType::HITS;
287 return rt;
288 }
289
291 {
292 return mWfsParameters[QgsWfsParameter::PROPERTYNAME].toStringListWithExp();
293 }
294
296 {
297 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toString();
298 }
299
301 {
302 return mWfsParameters[QgsWfsParameter::MAXFEATURES].toInt();
303 }
304
306 {
307 return mWfsParameters[QgsWfsParameter::STARTINDEX].toString();
308 }
309
311 {
312 return mWfsParameters[QgsWfsParameter::STARTINDEX].toInt();
313 }
314
316 {
317 return mWfsParameters[QgsWfsParameter::SRSNAME].toString();
318 }
319
320 QStringList QgsWfsParameters::typeNames() const
321 {
322 return mWfsParameters[QgsWfsParameter::TYPENAME].toStringList();
323 }
324
326 {
327 return mWfsParameters[QgsWfsParameter::FEATUREID].toStringList();
328 }
329
330 QStringList QgsWfsParameters::filters() const
331 {
332 return mWfsParameters[QgsWfsParameter::FILTER].toStringListWithExp();
333 }
334
336 {
337 return mWfsParameters[QgsWfsParameter::BBOX].toString();
338 }
339
341 {
342 return mWfsParameters[QgsWfsParameter::BBOX].toRectangle();
343 }
344
345 QStringList QgsWfsParameters::sortBy() const
346 {
347 return mWfsParameters[QgsWfsParameter::SORTBY].toStringListWithExp();
348 }
349
351 {
352 return mWfsParameters[QgsWfsParameter::EXP_FILTER].toExpressionList();
353 }
354
356 {
357 return mWfsParameters[QgsWfsParameter::GEOMETRYNAME].toString();
358 }
359
361 {
362 const QString vStr = version();
364
365 if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
366 version = QgsProjectVersion( vStr );
367 else
368 version = QgsProjectVersion( 1, 1, 0 ); // default value
369
370 return version;
371 }
372
373 void QgsWfsParameters::log( const QString &msg, const char *file, const char *function, int line ) const
374 {
375 QgsMessageLog::logMessage( msg, "Server", Qgis::MessageLevel::Info, true, file, function, line );
376 }
377} // namespace QgsWfs
@ Info
Information message.
Definition qgis.h:160
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())
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 truncated to the sp...
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:40