QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 <iostream>
21 
22 namespace QgsWfs
23 {
24  //
25  // QgsWfsParameter
26  //
28  const QVariant::Type type,
29  const QVariant defaultValue )
30  : QgsServerParameterDefinition( type, defaultValue )
31  , mName( name )
32  {
33  }
34 
36  {
37  bool ok = false;
38  const int val = QgsServerParameterDefinition::toInt( ok );
39 
40  if ( !ok )
41  {
42  raiseError();
43  }
44 
45  return val;
46  }
47 
49  {
50  QString value = toString();
51  const QStringList corners = mValue.toString().split( ',' );
52  if ( corners.size() == 5 )
53  {
54  value.resize( value.size() - corners[4].size() - 1 );
55  }
56 
58  param.mValue = QVariant( value );
59 
60  bool ok = false;
61  const QgsRectangle rectangle = param.toRectangle( ok );
62 
63  if ( !ok )
64  {
65  const QString msg = QString( "%1 ('%2') cannot be converted into rectangle" ).arg( name( mName ), toString() );
67  }
68 
69  return rectangle;
70  }
71 
72  QStringList QgsWfsParameter::toStringListWithExp( const QString &exp ) const
73  {
74  QStringList theList;
75 
76  QString val = mValue.toString();
77  if ( val.isEmpty() )
78  return theList;
79 
80  if ( exp.isEmpty() )
81  theList << val;
82  else
83  {
84  QRegExp rx( exp );
85  if ( rx.indexIn( val, 0 ) == -1 )
86  {
87  theList << val;
88  }
89  else
90  {
91  int pos = 0;
92  while ( ( pos = rx.indexIn( val, pos ) ) != -1 )
93  {
94  theList << rx.cap( 1 );
95  pos += rx.matchedLength();
96  }
97  }
98  }
99 
100  return theList;
101  }
102 
104  {
105  const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), toString(), typeName() );
107  }
108 
110  {
111  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
112  return metaEnum.valueToKey( name );
113  }
114 
116  {
117  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWfsParameter::Name>() );
118  return ( QgsWfsParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
119  }
120 
121  //
122  // QgsWfsParameters
123  //
126  {
127  // Available version number
128  mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
129  mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
130 
132  save( pOutputFormat );
133 
135  save( pResultType );
136 
138  save( pPropertyName );
139 
141  QVariant::Int,
142  QVariant( -1 ) );
143  save( pMaxFeatures );
144 
146  QVariant::Int,
147  QVariant( 0 ) );
148  save( pStartIndex );
149 
151  save( pSrsName );
152 
154  save( pTypeName );
155 
157  save( pFeatureId );
158 
160  save( pFilter );
161 
163  save( pBbox );
164 
166  save( pSortBy );
167 
169  save( pExpFilter );
170 
172  save( pGeometryName );
173  }
174 
176  : QgsWfsParameters()
177  {
178  load( parameters.urlQuery() );
179  }
180 
181  bool QgsWfsParameters::loadParameter( const QString &key, const QString &value )
182  {
183  bool loaded = false;
184 
185  const QgsWfsParameter::Name name = QgsWfsParameter::name( key );
186  if ( name >= 0 )
187  {
188  mWfsParameters[name].mValue = value;
189  if ( ! mWfsParameters[name].isValid() )
190  {
191  mWfsParameters[name].raiseError();
192  }
193 
194  loaded = true;
195  }
196 
197  return loaded;
198  }
199 
200  void QgsWfsParameters::save( const QgsWfsParameter &parameter )
201  {
202  mWfsParameters[ parameter.mName ] = parameter;
203  }
204 
206  {
207  log( "WFS Request parameters:" );
208  for ( auto parameter : mWfsParameters.toStdMap() )
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  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  f = Format::GeoJSON;
247  else if ( fStr.compare( QLatin1String( "gml2" ), Qt::CaseInsensitive ) == 0 )
248  f = Format::GML2;
249  else if ( fStr.compare( QLatin1String( "gml3" ), Qt::CaseInsensitive ) == 0 )
250  f = Format::GML3;
251  else if ( fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0 )
252  f = Format::GeoJSON;
253 
254  if ( f == Format::NONE &&
255  request().compare( QLatin1String( "describefeaturetype" ), Qt::CaseInsensitive ) == 0 &&
256  fStr.compare( QLatin1String( "xmlschema" ), Qt::CaseInsensitive ) == 0 )
257  f = Format::GML2;
258 
259  return f;
260  }
261 
263  {
264  return mWfsParameters[ QgsWfsParameter::RESULTTYPE ].toString();
265  }
266 
268  {
269  QString rtStr = resultTypeAsString();
270  if ( rtStr.isEmpty() )
271  return ResultType::RESULTS;
272 
273  ResultType rt = ResultType::RESULTS;
274  if ( rtStr.compare( QLatin1String( "hits" ), Qt::CaseInsensitive ) == 0 )
275  rt = ResultType::HITS;
276  return rt;
277  }
278 
280  {
281  return mWfsParameters[ QgsWfsParameter::PROPERTYNAME ].toStringListWithExp();
282  }
283 
285  {
286  return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toString();
287  }
288 
290  {
291  return mWfsParameters[ QgsWfsParameter::MAXFEATURES ].toInt();
292  }
293 
295  {
296  return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toString();
297  }
298 
300  {
301  return mWfsParameters[ QgsWfsParameter::STARTINDEX ].toInt();
302  }
303 
305  {
306  return mWfsParameters[ QgsWfsParameter::SRSNAME ].toString();
307  }
308 
309  QStringList QgsWfsParameters::typeNames() const
310  {
311  return mWfsParameters[ QgsWfsParameter::TYPENAME ].toStringList();
312  }
313 
314  QStringList QgsWfsParameters::featureIds() const
315  {
316  return mWfsParameters[ QgsWfsParameter::FEATUREID ].toStringList();
317  }
318 
319  QStringList QgsWfsParameters::filters() const
320  {
321  return mWfsParameters[ QgsWfsParameter::FILTER ].toStringListWithExp();
322  }
323 
324  QString QgsWfsParameters::bbox() const
325  {
326  return mWfsParameters[ QgsWfsParameter::BBOX ].toString();
327  }
328 
330  {
331  return mWfsParameters[ QgsWfsParameter::BBOX ].toRectangle();
332  }
333 
334  QStringList QgsWfsParameters::sortBy() const
335  {
336  return mWfsParameters[ QgsWfsParameter::SORTBY ].toStringListWithExp();
337  }
338 
339  QStringList QgsWfsParameters::expFilters() const
340  {
341  return mWfsParameters[ QgsWfsParameter::EXP_FILTER ].toStringListWithExp( QString( ) );
342  }
343 
345  {
346  return mWfsParameters[ QgsWfsParameter::GEOMETRYNAME ].toString();
347  }
348 
350  {
351  QString vStr = version();
353 
354  if ( vStr.isEmpty() )
355  version = QgsProjectVersion( 1, 1, 0 ); // default value
356  else if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
357  version = QgsProjectVersion( vStr );
358 
359  return version;
360  }
361 
362  void QgsWfsParameters::log( const QString &msg ) const
363  {
364  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
365  }
366 }
int toInt(bool &ok) const
Converts the parameter into an integer.
QString toString() const
Converts the parameter into a string.
QStringList sortBy() const
Returns SORTBY parameter as list.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QgsRectangle toRectangle() const
Converts the parameter into a rectangle.
void dump() const
Dumps parameters.
QString geometryNameAsString() const
Returns GEOMETRYNAME parameter as a string.
Format outputFormat() const
Returns format.
QStringList propertyNames() const
Returns PROPERTYNAME parameter as list.
QgsWfsParameters()
Constructor for WFS parameters with default values only.
QString value(const QString &key) const
Returns the value of a parameter.
ResultType resultType() const
Returns resultType.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
QStringList featureIds() const
Returns FEATUREID parameter as list.
void load(const QUrlQuery &query)
Loads new parameters.
WFS parameter received from the client.
QString resultTypeAsString() const
Returns RESULTTYPE parameter as a string.
QString typeName() const
Returns the type of the parameter as a string.
int toInt() const
Converts the parameter into an integer.
QStringList expFilters() const
Returns EXP_FILTER parameter as list.
QgsWfsParameter(const QgsWfsParameter::Name name=QgsWfsParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsWfsParameter.
void raiseError() const
Raises an error in case of an invalid conversion.
QStringList typeNames() const
Returns TYPENAME parameter as list.
QString outputFormatAsString() const
Returns OUTPUTFORMAT parameter as a string.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
WMS implementation.
Definition: qgswfs.cpp:35
A class to describe the version of a project.
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QString bbox() const
Returns BBOX if defined or an empty string.
Format
Output format for the response.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
QStringList filters() const
Returns FILTER parameter as list.
QStringList toStringListWithExp(const QString &exp="\\(([^()]+)\\)") const
Converts the parameter into a list of string.
QgsWfsParameter::Name mName
int startIndexAsInt() const
Returns STARTINDEX parameter as an int or its default value if not defined.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
QString startIndex() const
Returns STARTINDEX parameter as a string.
QString srsName() const
Returns SRSNAME parameter as a string.
QString maxFeatures() const
Returns MAXFEATURES parameter as a string.
ResultType
Type of results.
Name
Available parameters for WFS requests.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
Provides an interface to retrieve and manipulate WFS parameters received from the client...
static QString name(const QgsWfsParameter::Name)
Converts a parameter&#39;s name into its string representation.
int maxFeaturesAsInt() const
Returns MAXFEATURES parameter as an int or its default value if not defined.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
Definition of a parameter with basic conversion methods.