21
21
22
22
namespace QgsWfs
23
23
{
24
+ //
25
+ // QgsWfsParameter
26
+ //
27
+ QgsWfsParameter::QgsWfsParameter ( const QgsWfsParameter::Name name,
28
+ const QVariant::Type type,
29
+ const QVariant defaultValue )
30
+ : QgsServerParameterDefinition( type, defaultValue )
31
+ , mName ( name )
32
+ {
33
+ }
34
+
35
+ int QgsWfsParameter::toInt () const
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
+
48
+ QgsRectangle QgsWfsParameter::toRectangle () const
49
+ {
50
+ QString value = toString ();
51
+ const QStringList corners = mValue .toString ().split ( ' ,' );
52
+ if ( corners.size () == 5 )
53
+ {
54
+ value = value.chopped ( corners[4 ].size () + 1 );
55
+ }
56
+
57
+ QgsServerParameterDefinition param;
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 () );
66
+ QgsServerParameterDefinition::raiseError ( msg );
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
+ QRegExp rx ( exp );
81
+ if ( rx.indexIn ( val, 0 ) == -1 )
82
+ {
83
+ theList << val;
84
+ }
85
+ else
86
+ {
87
+ int pos = 0 ;
88
+ while ( ( pos = rx.indexIn ( val, pos ) ) != -1 )
89
+ {
90
+ theList << rx.cap ( 1 );
91
+ pos += rx.matchedLength ();
92
+ }
93
+ }
94
+
95
+ return theList;
96
+ }
97
+
98
+ void QgsWfsParameter::raiseError () const
99
+ {
100
+ const QString msg = QString ( " %1 ('%2') cannot be converted into %3" ).arg ( name ( mName ), toString (), typeName () );
101
+ QgsServerParameterDefinition::raiseError ( msg );
102
+ }
103
+
104
+ QString QgsWfsParameter::name ( const QgsWfsParameter::Name name )
105
+ {
106
+ const QMetaEnum metaEnum ( QMetaEnum::fromType<QgsWfsParameter::Name>() );
107
+ return metaEnum.valueToKey ( name );
108
+ }
109
+
110
+ QgsWfsParameter::Name QgsWfsParameter::name ( const QString &name )
111
+ {
112
+ const QMetaEnum metaEnum ( QMetaEnum::fromType<QgsWfsParameter::Name>() );
113
+ return ( QgsWfsParameter::Name ) metaEnum.keyToValue ( name.toUpper ().toStdString ().c_str () );
114
+ }
115
+
116
+ //
117
+ // QgsWfsParameters
118
+ //
24
119
QgsWfsParameters::QgsWfsParameters ()
120
+ : QgsServerParameters()
25
121
{
26
122
// Available version number
27
123
mVersions .append ( QgsProjectVersion ( 1 , 0 , 0 ) );
28
124
mVersions .append ( QgsProjectVersion ( 1 , 1 , 0 ) );
29
125
30
- const Parameter pOutputFormat = { ParameterName::OUTPUTFORMAT,
31
- QVariant::String,
32
- QVariant ( " " ),
33
- QVariant ()
34
- };
126
+ const QgsWfsParameter pOutputFormat = QgsWfsParameter ( QgsWfsParameter::OUTPUTFORMAT );
35
127
save ( pOutputFormat );
36
128
37
- const Parameter pResultType = { ParameterName::RESULTTYPE,
38
- QVariant::String,
39
- QVariant ( " " ),
40
- QVariant ()
41
- };
129
+ const QgsWfsParameter pResultType = QgsWfsParameter ( QgsWfsParameter::RESULTTYPE );
42
130
save ( pResultType );
43
131
44
- const Parameter pPropertyName = { ParameterName::PROPERTYNAME,
45
- QVariant::String,
46
- QVariant ( " " ),
47
- QVariant ()
48
- };
132
+ const QgsWfsParameter pPropertyName = QgsWfsParameter ( QgsWfsParameter::PROPERTYNAME );
49
133
save ( pPropertyName );
50
134
51
- const Parameter pMaxFeatures = { ParameterName::MAXFEATURES,
52
- QVariant::Int,
53
- QVariant ( -1 ),
54
- QVariant ()
55
- };
135
+ const QgsWfsParameter pMaxFeatures = QgsWfsParameter ( QgsWfsParameter::MAXFEATURES,
136
+ QVariant::Int,
137
+ QVariant ( -1 ) );
56
138
save ( pMaxFeatures );
57
139
58
- const Parameter pStartIndex = { ParameterName::STARTINDEX,
59
- QVariant::Int,
60
- QVariant ( 0 ),
61
- QVariant ()
62
- };
140
+ const QgsWfsParameter pStartIndex = QgsWfsParameter ( QgsWfsParameter::STARTINDEX,
141
+ QVariant::Int,
142
+ QVariant ( 0 ) );
63
143
save ( pStartIndex );
64
144
65
- const Parameter pSrsName = { ParameterName::SRSNAME,
66
- QVariant::String,
67
- QVariant ( " " ),
68
- QVariant ()
69
- };
145
+ const QgsWfsParameter pSrsName = QgsWfsParameter ( QgsWfsParameter::SRSNAME );
70
146
save ( pSrsName );
71
147
72
- const Parameter pTypeName = { ParameterName::TYPENAME,
73
- QVariant::String,
74
- QVariant ( " " ),
75
- QVariant ()
76
- };
148
+ const QgsWfsParameter pTypeName = QgsWfsParameter ( QgsWfsParameter::TYPENAME );
77
149
save ( pTypeName );
78
150
79
- const Parameter pFeatureId = { ParameterName::FEATUREID,
80
- QVariant::String,
81
- QVariant ( " " ),
82
- QVariant ()
83
- };
151
+ const QgsWfsParameter pFeatureId = QgsWfsParameter ( QgsWfsParameter::FEATUREID );
84
152
save ( pFeatureId );
85
153
86
- const Parameter pFilter = { ParameterName::FILTER,
87
- QVariant::String,
88
- QVariant ( " " ),
89
- QVariant ()
90
- };
154
+ const QgsWfsParameter pFilter = QgsWfsParameter ( QgsWfsParameter::FILTER );
91
155
save ( pFilter );
92
156
93
- const Parameter pBbox = { ParameterName::BBOX,
94
- QVariant::String,
95
- QVariant ( " " ),
96
- QVariant ()
97
- };
157
+ const QgsWfsParameter pBbox = QgsWfsParameter ( QgsWfsParameter::BBOX );
98
158
save ( pBbox );
99
159
100
- const Parameter pSortBy = { ParameterName::SORTBY,
101
- QVariant::String,
102
- QVariant ( " " ),
103
- QVariant ()
104
- };
160
+ const QgsWfsParameter pSortBy = QgsWfsParameter ( QgsWfsParameter::SORTBY );
105
161
save ( pSortBy );
106
162
107
- const Parameter pExpFilter = { ParameterName::EXP_FILTER,
108
- QVariant::String,
109
- QVariant ( " " ),
110
- QVariant ()
111
- };
163
+ const QgsWfsParameter pExpFilter = QgsWfsParameter ( QgsWfsParameter::EXP_FILTER );
112
164
save ( pExpFilter );
113
165
114
- const Parameter pGeometryName = { ParameterName::GEOMETRYNAME,
115
- QVariant::String,
116
- QVariant ( " " ),
117
- QVariant ()
118
- };
166
+ const QgsWfsParameter pGeometryName = QgsWfsParameter ( QgsWfsParameter::GEOMETRYNAME );
119
167
save ( pGeometryName );
120
168
}
121
169
122
- QgsWfsParameters::QgsWfsParameters ( const QgsServerRequest::Parameters ¶meters ) : QgsWfsParameters()
170
+ QgsWfsParameters::QgsWfsParameters ( const QgsServerParameters ¶meters )
171
+ : QgsWfsParameters()
123
172
{
124
- load ( parameters );
173
+ load ( parameters. urlQuery () );
125
174
}
126
175
127
- void QgsWfsParameters::load ( const QgsServerRequest::Parameters ¶meters )
176
+ bool QgsWfsParameters::loadParameter ( const QPair<QString, QString> ¶meter )
128
177
{
129
- mRequestParameters = parameters ;
178
+ bool loaded = false ;
130
179
131
- const QMetaEnum metaEnum ( QMetaEnum::fromType<ParameterName>() );
132
- foreach ( QString key, parameters.keys () )
180
+ const QString key = parameter.first ;
181
+ const QgsWfsParameter::Name name = QgsWfsParameter::name ( key );
182
+ if ( name >= 0 )
133
183
{
134
- const ParameterName name = ( ParameterName ) metaEnum. keyToValue ( key. toStdString (). c_str () ) ;
135
- if ( name >= 0 )
184
+ mWfsParameters [ name]. mValue = parameter. second ;
185
+ if ( ! mWfsParameters [name]. isValid () )
136
186
{
137
- QVariant value ( parameters[key] );
138
- if ( value.canConvert ( mParameters [name].mType ) )
139
- {
140
- mParameters [name].mValue = value;
141
- }
142
- else
143
- {
144
- raiseError ( name );
145
- }
187
+ mWfsParameters [name].raiseError ();
146
188
}
189
+
190
+ loaded = true ;
147
191
}
192
+
193
+ return loaded;
148
194
}
149
195
150
- void QgsWfsParameters::dump () const
196
+ void QgsWfsParameters::save ( const QgsWfsParameter ¶meter )
151
197
{
152
- const QMetaEnum metaEnum ( QMetaEnum::fromType<ParameterName>() );
198
+ mWfsParameters [ parameter.mName ] = parameter;
199
+ }
153
200
201
+ void QgsWfsParameters::dump () const
202
+ {
154
203
log ( " WFS Request parameters:" );
155
- for ( auto parameter : mParameters .toStdMap () )
204
+ for ( auto parameter : mWfsParameters .toStdMap () )
156
205
{
157
- const QString value = parameter.second .mValue . toString ();
206
+ const QString value = parameter.second .toString ();
158
207
159
208
if ( ! value.isEmpty () )
160
209
{
161
- const QString name = metaEnum. valueToKey ( parameter.first );
162
- log ( " - " + name + " : " + value );
210
+ const QString name = QgsWfsParameter::name ( parameter.first );
211
+ log ( QStringLiteral ( " - %1 : %2 " ). arg ( name, value ) );
163
212
}
164
213
}
165
214
166
215
if ( !version ().isEmpty () )
167
- log ( " - VERSION : " + version () );
168
- }
169
-
170
- void QgsWfsParameters::save ( const Parameter ¶meter )
171
- {
172
- mParameters [ parameter.mName ] = parameter;
173
- }
174
-
175
- QVariant QgsWfsParameters::value ( ParameterName name ) const
176
- {
177
- return mParameters [name].mValue ;
178
- }
179
-
180
- QVariant QgsWfsParameters::defaultValue ( ParameterName name ) const
181
- {
182
- return mParameters [name].mDefaultValue ;
216
+ log ( QStringLiteral ( " - VERSION : %1" ).arg ( version () ) );
183
217
}
184
218
185
219
QString QgsWfsParameters::outputFormatAsString () const
186
220
{
187
- return value ( ParameterName ::OUTPUTFORMAT ) .toString ();
221
+ return mWfsParameters [ QgsWfsParameter ::OUTPUTFORMAT ] .toString ();
188
222
}
189
223
190
224
QgsWfsParameters::Format QgsWfsParameters::outputFormat () const
@@ -223,7 +257,7 @@ namespace QgsWfs
223
257
224
258
QString QgsWfsParameters::resultTypeAsString () const
225
259
{
226
- return value ( ParameterName ::RESULTTYPE ) .toString ();
260
+ return mWfsParameters [ QgsWfsParameter ::RESULTTYPE ] .toString ();
227
261
}
228
262
229
263
QgsWfsParameters::ResultType QgsWfsParameters::resultType () const
@@ -240,90 +274,72 @@ namespace QgsWfs
240
274
241
275
QStringList QgsWfsParameters::propertyNames () const
242
276
{
243
- return toStringListWithExp ( ParameterName ::PROPERTYNAME );
277
+ return mWfsParameters [ QgsWfsParameter ::PROPERTYNAME ]. toStringListWithExp ( );
244
278
}
245
279
246
280
QString QgsWfsParameters::maxFeatures () const
247
281
{
248
- return value ( ParameterName ::MAXFEATURES ) .toString ();
282
+ return mWfsParameters [ QgsWfsParameter ::MAXFEATURES ] .toString ();
249
283
}
250
284
251
285
int QgsWfsParameters::maxFeaturesAsInt () const
252
286
{
253
- return toInt ( ParameterName ::MAXFEATURES );
287
+ return mWfsParameters [ QgsWfsParameter ::MAXFEATURES ]. toInt ( );
254
288
}
255
289
256
290
QString QgsWfsParameters::startIndex () const
257
291
{
258
- return value ( ParameterName ::STARTINDEX ) .toString ();
292
+ return mWfsParameters [ QgsWfsParameter ::STARTINDEX ] .toString ();
259
293
}
260
294
261
295
int QgsWfsParameters::startIndexAsInt () const
262
296
{
263
- return toInt ( ParameterName ::STARTINDEX );
297
+ return mWfsParameters [ QgsWfsParameter ::STARTINDEX ]. toInt ( );
264
298
}
265
299
266
300
QString QgsWfsParameters::srsName () const
267
301
{
268
- return value ( ParameterName ::SRSNAME ) .toString ();
302
+ return mWfsParameters [ QgsWfsParameter ::SRSNAME ] .toString ();
269
303
}
270
304
271
305
QStringList QgsWfsParameters::typeNames () const
272
306
{
273
- return toStringList ( ParameterName ::TYPENAME );
307
+ return mWfsParameters [ QgsWfsParameter ::TYPENAME ]. toStringList ( );
274
308
}
275
309
276
310
QStringList QgsWfsParameters::featureIds () const
277
311
{
278
- return toStringList ( ParameterName ::FEATUREID );
312
+ return mWfsParameters [ QgsWfsParameter ::FEATUREID ]. toStringList ( );
279
313
}
280
314
281
315
QStringList QgsWfsParameters::filters () const
282
316
{
283
- return toStringListWithExp ( ParameterName ::FILTER );
317
+ return mWfsParameters [ QgsWfsParameter ::FILTER ]. toStringListWithExp ( );
284
318
}
285
319
286
320
QString QgsWfsParameters::bbox () const
287
321
{
288
- return value ( ParameterName ::BBOX ) .toString ();
322
+ return mWfsParameters [ QgsWfsParameter ::BBOX ] .toString ();
289
323
}
290
324
291
325
QgsRectangle QgsWfsParameters::bboxAsRectangle () const
292
326
{
293
- return toRectangle ( ParameterName ::BBOX );
327
+ return mWfsParameters [ QgsWfsParameter ::BBOX ]. toRectangle ( );
294
328
}
295
329
296
330
QStringList QgsWfsParameters::sortBy () const
297
331
{
298
- return toStringListWithExp ( ParameterName ::SORTBY );
332
+ return mWfsParameters [ QgsWfsParameter ::SORTBY ]. toStringListWithExp ( );
299
333
}
300
334
301
335
QStringList QgsWfsParameters::expFilters () const
302
336
{
303
- return toStringListWithExp ( ParameterName ::EXP_FILTER );
337
+ return mWfsParameters [ QgsWfsParameter ::EXP_FILTER ]. toStringListWithExp ( );
304
338
}
305
339
306
340
QString QgsWfsParameters::geometryNameAsString () const
307
341
{
308
- return value ( ParameterName::GEOMETRYNAME ).toString ();
309
- }
310
-
311
- QString QgsWfsParameters::request () const
312
- {
313
- if ( mRequestParameters .contains ( " REQUEST" ) )
314
- return mRequestParameters [" REQUEST" ];
315
- else
316
- return QString ();
317
- }
318
-
319
- QString QgsWfsParameters::version () const
320
- {
321
- // VERSION parameter is not managed with other parameters because
322
- // there's a conflict with qgis VERSION defined in qgsconfig.h
323
- if ( mRequestParameters .contains ( " VERSION" ) )
324
- return mRequestParameters [" VERSION" ];
325
- else
326
- return QString ();
342
+ return mWfsParameters [ QgsWfsParameter::GEOMETRYNAME ].toString ();
327
343
}
328
344
329
345
QgsProjectVersion QgsWfsParameters::versionAsNumber () const
@@ -339,141 +355,8 @@ namespace QgsWfs
339
355
return version;
340
356
}
341
357
342
- QString QgsWfsParameters::name ( ParameterName name ) const
343
- {
344
- const QMetaEnum metaEnum ( QMetaEnum::fromType<ParameterName>() );
345
- return metaEnum.valueToKey ( name );
346
- }
347
-
348
- int QgsWfsParameters::toInt ( const QVariant &value, const QVariant &defaultValue, bool *error ) const
349
- {
350
- int val = defaultValue.toInt ();
351
- QString valStr = value.toString ();
352
- bool ok = true ;
353
-
354
- if ( !valStr.isEmpty () )
355
- {
356
- val = value.toInt ( &ok );
357
- }
358
- *error = !ok;
359
-
360
- return val;
361
- }
362
-
363
- int QgsWfsParameters::toInt ( ParameterName p ) const
364
- {
365
- bool error;
366
- int val = toInt ( value ( p ), defaultValue ( p ), &error );
367
- if ( error )
368
- {
369
- QString n = name ( p );
370
- QString valStr = value ( p ).toString ();
371
- QString msg = n + " ('" + valStr + " ') cannot be converted into int" ;
372
- raiseError ( msg );
373
- }
374
-
375
- return val;
376
- }
377
-
378
- QgsRectangle QgsWfsParameters::toRectangle ( const QVariant &value, bool *error ) const
379
- {
380
- *error = false ;
381
- QString bbox = value.toString ();
382
- QgsRectangle extent;
383
-
384
- if ( !bbox.isEmpty () )
385
- {
386
- QStringList corners = bbox.split ( ' ,' );
387
- // We need at least 4 elements, an optional fifth could be the CRS in WFS 1.1.0 BBOX
388
- if ( corners.size () >= 4 )
389
- {
390
- double d[4 ];
391
- bool ok;
392
-
393
- for ( int i = 0 ; i < 4 ; i++ )
394
- {
395
- corners[i].replace ( QLatin1String ( " " ), QLatin1String ( " +" ) );
396
- d[i] = corners[i].toDouble ( &ok );
397
- if ( !ok )
398
- {
399
- *error = !ok;
400
- return extent;
401
- }
402
- }
403
-
404
- extent = QgsRectangle ( d[0 ], d[1 ], d[2 ], d[3 ] );
405
-
406
- }
407
- else
408
- {
409
- *error = true ;
410
- return extent;
411
- }
412
- }
413
-
414
- return extent;
415
- }
416
-
417
- QgsRectangle QgsWfsParameters::toRectangle ( ParameterName p ) const
418
- {
419
- bool error;
420
- QgsRectangle extent = toRectangle ( value ( p ), &error );
421
- if ( error )
422
- {
423
- QString n = name ( p );
424
- QString valStr = value ( p ).toString ();
425
- QString msg = n + " ('" + valStr + " ') cannot be converted into a rectangle" ;
426
- raiseError ( msg );
427
- }
428
-
429
- return extent;
430
- }
431
-
432
- QStringList QgsWfsParameters::toStringList ( ParameterName p, char delimiter ) const
433
- {
434
- return value ( p ).toString ().split ( delimiter, QString::SkipEmptyParts );
435
- }
436
-
437
- QStringList QgsWfsParameters::toStringListWithExp ( ParameterName p, const QString &exp ) const
438
- {
439
- QStringList theList;
440
-
441
- QString val = value ( p ).toString ();
442
- if ( val.isEmpty () )
443
- return theList;
444
-
445
- QRegExp rx ( exp );
446
- if ( rx.indexIn ( val, 0 ) == -1 )
447
- {
448
- theList << val;
449
- }
450
- else
451
- {
452
- int pos = 0 ;
453
- while ( ( pos = rx.indexIn ( val, pos ) ) != -1 )
454
- {
455
- theList << rx.cap ( 1 );
456
- pos += rx.matchedLength ();
457
- }
458
- }
459
- return theList;
460
- }
461
-
462
358
void QgsWfsParameters::log ( const QString &msg ) const
463
359
{
464
360
QgsMessageLog::logMessage ( msg, " Server" , Qgis::Info );
465
361
}
466
-
467
- void QgsWfsParameters::raiseError ( ParameterName paramName ) const
468
- {
469
- const QString value = mParameters [paramName].mValue .toString ();
470
- const QString param = name ( paramName );
471
- const QString type = QVariant::typeToName ( mParameters [paramName].mType );
472
- raiseError ( param + " ('" + value + " ') cannot be converted into " + type );
473
- }
474
-
475
- void QgsWfsParameters::raiseError ( const QString &msg ) const
476
- {
477
- throw QgsBadRequestException ( QStringLiteral ( " Invalid WFS Parameter" ), msg );
478
- }
479
362
}
0 commit comments