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