Skip to content

Commit 37c1e23

Browse files
committedSep 26, 2018
[Server][WFS] Use editor widget setup for DateTime fields
The user can define with the editor widget the data type of the field with more accuracy than the data provider. In the case of DateTime, this data can be stored in a text field or the field is DateTime by default because the data provider does not identify Date or Time. QGIS Server in the WFS case can use this config to provide well formed data and description.
1 parent cfddac5 commit 37c1e23

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed
 

‎src/server/services/wfs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ INCLUDE_DIRECTORIES(
4545
../../../core/raster
4646
../../../core/symbology
4747
../../../core/layertree
48+
../../../core/fieldformatter
4849
../..
4950
..
5051
.

‎src/server/services/wfs/qgswfsdescribefeaturetype.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "qgsvectordataprovider.h"
3131
#include "qgsmapserviceexception.h"
3232
#include "qgscoordinatereferencesystem.h"
33+
#include "qgsfieldformatterregistry.h"
34+
#include "qgsfieldformatter.h"
35+
#include "qgsdatetimefieldformatter.h"
3336

3437
#include <QStringList>
3538

@@ -307,6 +310,20 @@ namespace QgsWfs
307310
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "string" ) );
308311
}
309312

313+
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
314+
if ( setup.type() == QStringLiteral( "DateTime" ) )
315+
{
316+
QgsDateTimeFieldFormatter fieldFormatter;
317+
const QVariantMap config = setup.config();
318+
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( field.type() ) ).toString();
319+
if ( fieldFormat == QStringLiteral( "yyyy-MM-dd" ) )
320+
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
321+
else if ( fieldFormat == QStringLiteral( "HH:mm:ss" ) )
322+
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
323+
else
324+
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
325+
}
326+
310327
sequenceElem.appendChild( attElem );
311328

312329
QString alias = field.alias();

‎src/server/services/wfs/qgswfsgetfeature.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "qgswfsutils.h"
2323
#include "qgsserverprojectutils.h"
2424
#include "qgsfields.h"
25+
#include "qgsfieldformatterregistry.h"
26+
#include "qgsfieldformatter.h"
27+
#include "qgsdatetimefieldformatter.h"
2528
#include "qgsexpression.h"
2629
#include "qgsgeometry.h"
2730
#include "qgsmaplayer.h"
@@ -62,7 +65,7 @@ namespace QgsWfs
6265

6366
QString createFeatureGeoJSON( QgsFeature *feat, const createFeatureParams &params );
6467

65-
QString encodeValueToText( const QVariant &value );
68+
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup );
6669

6770
QDomElement createFeatureGML2( QgsFeature *feat, QDomDocument &doc, const createFeatureParams &params, const QgsProject *project );
6871

@@ -1337,10 +1340,12 @@ namespace QgsWfs
13371340
{
13381341
continue;
13391342
}
1340-
QString attributeName = fields.at( idx ).name();
1343+
const QgsField field = fields.at( idx );
1344+
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
1345+
QString attributeName = field.name();
13411346

13421347
QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
1343-
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
1348+
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
13441349
fieldElem.appendChild( fieldText );
13451350
typeNameElement.appendChild( fieldElem );
13461351
}
@@ -1432,22 +1437,37 @@ namespace QgsWfs
14321437
{
14331438
continue;
14341439
}
1435-
QString attributeName = fields.at( idx ).name();
1440+
const QgsField field = fields.at( idx );
1441+
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
1442+
QString attributeName = field.name();
14361443

14371444
QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
1438-
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
1445+
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
14391446
fieldElem.appendChild( fieldText );
14401447
typeNameElement.appendChild( fieldElem );
14411448
}
14421449

14431450
return featureElement;
14441451
}
14451452

1446-
QString encodeValueToText( const QVariant &value )
1453+
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup )
14471454
{
14481455
if ( value.isNull() )
14491456
return QStringLiteral( "null" );
14501457

1458+
if ( setup.type() == QStringLiteral( "DateTime" ) )
1459+
{
1460+
QgsDateTimeFieldFormatter fieldFormatter;
1461+
const QVariantMap config = setup.config();
1462+
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( value.type() ) ).toString();
1463+
QDateTime date = value.toDateTime();
1464+
1465+
if ( date.isValid() )
1466+
{
1467+
return date.toString( fieldFormat );
1468+
}
1469+
}
1470+
14511471
switch ( value.type() )
14521472
{
14531473
case QVariant::Int:

0 commit comments

Comments
 (0)