QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
21 namespace QgsWfs
22 {
23  //
24  // QgsWfsParameter
25  //
27  const QVariant::Type type,
28  const QVariant defaultValue )
29  : QgsServerParameterDefinition( type, defaultValue )
30  , mName( name )
31  {
32  }
33 
34  int QgsWfsParameter::toInt() const
35  {
36  bool ok = false;
37  const int val = QgsServerParameterDefinition::toInt( ok );
38 
39  if ( !ok )
40  {
41  raiseError();
42  }
43 
44  return val;
45  }
46 
48  {
49  QString value = toString();
50  const QStringList corners = mValue.toString().split( ',' );
51  if ( corners.size() == 5 )
52  {
53  value.resize( value.size() - corners[4].size() - 1 );
54  }
55 
57  param.mValue = QVariant( value );
58 
59  bool ok = false;
60  const QgsRectangle rectangle = param.toRectangle( ok );
61 
62  if ( !ok )
63  {
64  const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
66  }
67 
68  return rectangle;
69  }
70 
71  QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
72  {
73  QStringList theList;
74 
75  const QString val = mValue.toString();
76  if ( val.isEmpty() )
77  return theList;
78 
79  if ( exp.isEmpty() )
80  theList << val;
81  else
82  {
83  QRegExp rx( exp );
84  if ( rx.indexIn( val, 0 ) == -1 )
85  {
86  theList << val;
87  }
88  else
89  {
90  int pos = 0;
91  while ( ( pos = rx.indexIn( val, pos ) ) != -1 )
92  {
93  theList << rx.cap( 1 );
94  pos += rx.matchedLength();
95  }
96  }
97  }
98 
99  return theList;
100  }
101 
102  void QgsWfsParameter::raiseError() const
103  {
104  const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
106  }
107 
108  QString QgsWfsParameter::name( const QgsWfsParameter::Name name )
109  {
110  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
111  return metaEnum.valueToKey( name );
112  }
113 
114  QgsWfsParameter::Name QgsWfsParameter::name( const QString &name )
115  {
116  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
117  return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
118  }
119 
120  //
121  // QgsWfsParameters
122  //
125  {
126  // Available version number
127  mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
128  mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
129 
131  save( pOutputFormat );
132 
134  save( pResultType );
135 
137  save( pPropertyName );
138 
140  QVariant::Int,
141  QVariant( -1 ) );
142  save( pMaxFeatures );
143 
145  QVariant::Int,
146  QVariant( 0 ) );
147  save( pStartIndex );
148 
150  save( pSrsName );
151 
153  save( pTypeName );
154 
156  save( pFeatureId );
157 
159  save( pFilter );
160 
162  save( pBbox );
163 
165  save( pSortBy );
166 
168  save( pExpFilter );
169 
171  save( pGeometryName );
172  }
173 
175  : QgsWfsParameters()
176  {
177  load( parameters.urlQuery() );
178  }
179 
180  bool QgsWfsParameters::loadParameter( const QString &key, const QString &value )
181  {
182  bool loaded = false;
183 
184  const QgsWfsParameter::Name name = QgsWfsParameter::name( key );
185  if ( name >= 0 )
186  {
187  mWfsParameters[name].mValue = value;
188  if ( ! mWfsParameters[name].isValid() )
189  {
190  mWfsParameters[name].raiseError();
191  }
192 
193  loaded = true;
194  }
195 
196  return loaded;
197  }
198 
199  void QgsWfsParameters::save( const QgsWfsParameter &parameter )
200  {
201  mWfsParameters[ parameter.mName ] = parameter;
202  }
203 
204  void QgsWfsParameters::dump() const
205  {
206  log( "WFS Request parameters:" );
207  const auto map = mWfsParameters.toStdMap();
208  for ( const auto &parameter : map )
209  {
210  const QString value = parameter.second.toString();
211 
212  if ( ! value.isEmpty() )
213  {
214  const QString name = QgsWfsParameter::name( parameter.first );
215  log( QStringLiteral( " - %1 : %2" ).arg( name, value ) );
216  }
217  }
218 
219  if ( !version().isEmpty() )
220  log( QStringLiteral( " - VERSION : %1" ).arg( version() ) );
221  }
222 
224  {
225  return mWfsParameters[ QgsWfsParameter::OUTPUTFORMAT ].toString();
226  }
227 
229  {
230  const QString fStr = outputFormatAsString();
231 
232  if ( fStr.isEmpty() )
233  {
234  if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
235  return Format::GML3;
236  else
237  return Format::GML2;
238  }
239 
240  Format f = Format::NONE;
241  if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/2.1.2" ), Qt::CaseInsensitive ) == 0 )
242  f = Format::GML2;
243  else if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/3.1.1" ), Qt::CaseInsensitive ) == 0 )
244  f = Format::GML3;
245  else if ( fStr.compare( QLatin1String( "application/vnd.geo+json" ), Qt::CaseInsensitive ) == 0 ||
246  // Needs to check for space too, because a + sign in the query string is interpreted as a space
247  fStr.compare( QLatin1String( "application/vnd.geo json" ), Qt::CaseInsensitive ) == 0 ||
248  fStr.compare( QLatin1String( "application/geo+json" ), Qt::CaseInsensitive ) == 0 ||
249  fStr.compare( QLatin1String( "application/geo json" ), Qt::CaseInsensitive ) == 0 ||
250  fStr.compare( QLatin1String( "application/json" ), Qt::CaseInsensitive ) == 0 ||
251  fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0
252  )
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 &&
260  request().compare( QLatin1String( "describefeaturetype" ), Qt::CaseInsensitive ) == 0 &&
261  fStr.compare( QLatin1String( "xmlschema" ), Qt::CaseInsensitive ) == 0 )
262  {
263  if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
264  return Format::GML3;
265  else
266  return Format::GML2;
267  }
268 
269  return f;
270  }
271 
273  {
274  return mWfsParameters[ QgsWfsParameter::RESULTTYPE ].toString();
275  }
276 
278  {
279  const QString rtStr = resultTypeAsString();
280  if ( rtStr.isEmpty() )
281  return ResultType::RESULTS;
282 
283  ResultType rt = ResultType::RESULTS;
284  if ( rtStr.compare( QLatin1String( "hits" ), Qt::CaseInsensitive ) == 0 )
285  rt = ResultType::HITS;
286  return rt;
287  }
288 
289  QStringList QgsWfsParameters::propertyNames() const
290  {
291  return mWfsParameters[ QgsWfsParameter::PROPERTYNAME ].toStringListWithExp();
292  }
293 
294  QString QgsWfsParameters::maxFeatures() const
295  {
296  return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toString();
297  }
298 
300  {
301  return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toInt();
302  }
303 
304  QString QgsWfsParameters::startIndex() const
305  {
306  return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toString();
307  }
308 
310  {
311  return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toInt();
312  }
313 
314  QString QgsWfsParameters::srsName() const
315  {
316  return mWfsParameters[ QgsWfsParameter::SRSNAME ].toString();
317  }
318 
319  QStringList QgsWfsParameters::typeNames() const
320  {
321  return mWfsParameters[ QgsWfsParameter::TYPENAME ].toStringList();
322  }
323 
324  QStringList QgsWfsParameters::featureIds() const
325  {
326  return mWfsParameters[ QgsWfsParameter::FEATUREID ].toStringList();
327  }
328 
329  QStringList QgsWfsParameters::filters() const
330  {
331  return mWfsParameters[ QgsWfsParameter::FILTER ].toStringListWithExp();
332  }
333 
334  QString QgsWfsParameters::bbox() const
335  {
336  return mWfsParameters[ QgsWfsParameter::BBOX ].toString();
337  }
338 
340  {
341  return mWfsParameters[ QgsWfsParameter::BBOX ].toRectangle();
342  }
343 
344  QStringList QgsWfsParameters::sortBy() const
345  {
346  return mWfsParameters[ QgsWfsParameter::SORTBY ].toStringListWithExp();
347  }
348 
349  QStringList QgsWfsParameters::expFilters() const
350  {
351  return mWfsParameters[ QgsWfsParameter::EXP_FILTER ].toExpressionList();
352  }
353 
355  {
356  return mWfsParameters[ QgsWfsParameter::GEOMETRYNAME ].toString();
357  }
358 
360  {
361  const QString vStr = version();
363 
364  if ( vStr.isEmpty() )
365  version = QgsProjectVersion( 1, 1, 0 ); // default value
366  else if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
368 
369  return version;
370  }
371 
372  void QgsWfsParameters::log( const QString &msg ) const
373  {
374  QgsMessageLog::logMessage( msg, "Server", Qgis::MessageLevel::Info );
375  }
376 }
QgsWfs::QgsWfsParameters::resultTypeAsString
QString resultTypeAsString() const
Returns RESULTTYPE parameter as a string.
Definition: qgswfsparameters.cpp:285
QgsWfs::QgsWfsParameter::RESULTTYPE
@ RESULTTYPE
Definition: qgswfsparameters.h:60
QgsWfs::QgsWfsParameter::raiseError
void raiseError() const
Raises an error in case of an invalid conversion.
Definition: qgswfsparameters.cpp:115
QgsWfs::QgsWfsParameters::featureIds
QStringList featureIds() const
Returns FEATUREID parameter as list.
Definition: qgswfsparameters.cpp:337
QgsServerParameterDefinition::raiseError
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
Definition: qgsserverparameters.cpp:452
QgsWfs::QgsWfsParameter::FILTER
@ FILTER
Definition: qgswfsparameters.h:67
QgsWfs::QgsWfsParameters::maxFeaturesAsInt
int maxFeaturesAsInt() const
Returns MAXFEATURES parameter as an int or its default value if not defined.
Definition: qgswfsparameters.cpp:312
QgsServerParameters::request
virtual QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
Definition: qgsserverparameters.cpp:625
QgsWfs::QgsWfsParameter
WFS parameter received from the client.
Definition: qgswfsparameters.h:50
QgsWfs::QgsWfsParameters::srsName
QString srsName() const
Returns SRSNAME parameter as a string.
Definition: qgswfsparameters.cpp:327
QgsWfs::QgsWfsParameters::outputFormatAsString
QString outputFormatAsString() const
Returns OUTPUTFORMAT parameter as a string.
Definition: qgswfsparameters.cpp:236
QgsServerParameters::load
void load(const QUrlQuery &query)
Loads new parameters.
Definition: qgsserverparameters.cpp:647
QgsWfs::QgsWfsParameters::resultType
ResultType resultType() const
Returns resultType.
Definition: qgswfsparameters.cpp:290
QgsWfs::QgsWfsParameters::dump
void dump() const
Dumps parameters.
Definition: qgswfsparameters.cpp:217
QgsWfs::QgsWfsParameters::startIndexAsInt
int startIndexAsInt() const
Returns STARTINDEX parameter as an int or its default value if not defined.
Definition: qgswfsparameters.cpp:322
QgsWfs::QgsWfsParameter::toRectangle
QgsRectangle toRectangle() const
Converts the parameter into a rectangle.
Definition: qgswfsparameters.cpp:60
QgsWfs::QgsWfsParameter::STARTINDEX
@ STARTINDEX
Definition: qgswfsparameters.h:63
QgsWfs::QgsWfsParameters::Format
Format
Output format for the response.
Definition: qgswfsparameters.h:145
QgsServerParameters::value
QString value(const QString &key) const
Returns the value of a parameter.
Definition: qgsserverparameters.cpp:630
QgsWfs::QgsWfsParameters::versionAsNumber
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
Definition: qgswfsparameters.cpp:372
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsWfs::QgsWfsParameters::outputFormat
Format outputFormat() const
Returns format.
Definition: qgswfsparameters.cpp:241
QgsWfs::QgsWfsParameter::MAXFEATURES
@ MAXFEATURES
Definition: qgswfsparameters.h:62
QgsWfs::QgsWfsParameter::mName
QgsWfsParameter::Name mName
Definition: qgswfsparameters.h:129
QgsWfs::QgsWfsParameter::BBOX
@ BBOX
Definition: qgswfsparameters.h:68
QgsServerParameters::version
virtual QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
Definition: qgsserverparameters.cpp:575
QgsWfs::QgsWfsParameter::PROPERTYNAME
@ PROPERTYNAME
Definition: qgswfsparameters.h:61
QgsWfs::QgsWfsParameter::Name
Name
Available parameters for WFS requests.
Definition: qgswfsparameters.h:56
QgsWfs::QgsWfsParameters::typeNames
QStringList typeNames() const
Returns TYPENAME parameter as list.
Definition: qgswfsparameters.cpp:332
QgsWfs::QgsWfsParameters::filters
QStringList filters() const
Returns FILTER parameter as list.
Definition: qgswfsparameters.cpp:342
QgsWfs::QgsWfsParameter::toInt
int toInt() const
Converts the parameter into an integer.
Definition: qgswfsparameters.cpp:47
QgsWfs::QgsWfsParameter::GEOMETRYNAME
@ GEOMETRYNAME
Definition: qgswfsparameters.h:71
QgsWfs::QgsWfsParameters::bbox
QString bbox() const
Returns BBOX if defined or an empty string.
Definition: qgswfsparameters.cpp:347
QgsWfs::QgsWfsParameter::QgsWfsParameter
QgsWfsParameter(const QgsWfsParameter::Name name=QgsWfsParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsWfsParameter.
Definition: qgswfsparameters.cpp:39
QgsWfs::QgsWfsParameter::SRSNAME
@ SRSNAME
Definition: qgswfsparameters.h:64
QgsServerParameterDefinition
Definition of a parameter with basic conversion methods.
Definition: qgsserverparameters.h:34
QgsWfs::QgsWfsParameter::OUTPUTFORMAT
@ OUTPUTFORMAT
Definition: qgswfsparameters.h:59
QgsWfs::QgsWfsParameters
Provides an interface to retrieve and manipulate WFS parameters received from the client.
Definition: qgswfsparameters.h:138
QgsWfs
WMS implementation.
Definition: qgswfs.cpp:35
QgsWfs::QgsWfsParameters::maxFeatures
QString maxFeatures() const
Returns MAXFEATURES parameter as a string.
Definition: qgswfsparameters.cpp:307
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
QgsWfs::QgsWfsParameters::ResultType
ResultType
Type of results.
Definition: qgswfsparameters.h:154
QgsServerParameterDefinition::toString
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
Definition: qgsserverparameters.cpp:65
QgsServerParameters::map
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
Definition: qgsserverparameters.cpp:570
QgsWfs::QgsWfsParameters::sortBy
QStringList sortBy() const
Returns SORTBY parameter as list.
Definition: qgswfsparameters.cpp:357
QgsServerParameterDefinition::toInt
int toInt(bool &ok) const
Converts the parameter into an integer.
Definition: qgsserverparameters.cpp:409
QgsServerParameterDefinition::mValue
QVariant mValue
Definition: qgsserverparameters.h:179
QgsWfs::QgsWfsParameter::EXP_FILTER
@ EXP_FILTER
Definition: qgswfsparameters.h:70
QgsWfs::QgsWfsParameter::FEATUREID
@ FEATUREID
Definition: qgswfsparameters.h:66
QgsWfs::QgsWfsParameters::startIndex
QString startIndex() const
Returns STARTINDEX parameter as a string.
Definition: qgswfsparameters.cpp:317
qgswfsparameters.h
QgsWfs::QgsWfsParameters::propertyNames
QStringList propertyNames() const
Returns PROPERTYNAME parameter as list.
Definition: qgswfsparameters.cpp:302
QgsServerParameters::urlQuery
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
Definition: qgsserverparameters.cpp:530
QgsWfs::QgsWfsParameters::bboxAsRectangle
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
Definition: qgswfsparameters.cpp:352
QgsServerParameters
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
Definition: qgsserverparameters.h:242
QgsWfs::QgsWfsParameters::geometryNameAsString
QString geometryNameAsString() const
Returns GEOMETRYNAME parameter as a string.
Definition: qgswfsparameters.cpp:367
QgsWfs::QgsWfsParameter::name
static QString name(const QgsWfsParameter::Name)
Converts a parameter's name into its string representation.
Definition: qgswfsparameters.cpp:121
QgsServerParameterDefinition::typeName
QString typeName() const
Returns the type of the parameter as a string.
Definition: qgsserverparameters.cpp:38
QgsWfs::QgsWfsParameter::toStringListWithExp
QStringList toStringListWithExp(const QString &exp="\\(([^()]+)\\)") const
Converts the parameter into a list of string.
Definition: qgswfsparameters.cpp:84
QgsWfs::QgsWfsParameter::SORTBY
@ SORTBY
Definition: qgswfsparameters.h:69
QgsWfs::QgsWfsParameters::QgsWfsParameters
QgsWfsParameters()
Constructor for WFS parameters with default values only.
Definition: qgswfsparameters.cpp:136
QgsProjectVersion
A class to describe the version of a project.
Definition: qgsprojectversion.h:32
QgsWfs::QgsWfsParameter::TYPENAME
@ TYPENAME
Definition: qgswfsparameters.h:65
QgsWfs::QgsWfsParameters::expFilters
QStringList expFilters() const
Returns EXP_FILTER parameter as list.
Definition: qgswfsparameters.cpp:362
qgsmessagelog.h
QgsServerParameterDefinition::toRectangle
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
Definition: qgsserverparameters.cpp:282