Skip to content

Commit

Permalink
[Server][WFS] Use editor widget setup for DateTime fields
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rldhont committed Sep 26, 2018
1 parent cfddac5 commit 37c1e23
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/server/services/wfs/CMakeLists.txt
Expand Up @@ -45,6 +45,7 @@ INCLUDE_DIRECTORIES(
../../../core/raster
../../../core/symbology
../../../core/layertree
../../../core/fieldformatter
../..
..
.
Expand Down
17 changes: 17 additions & 0 deletions src/server/services/wfs/qgswfsdescribefeaturetype.cpp
Expand Up @@ -30,6 +30,9 @@
#include "qgsvectordataprovider.h"
#include "qgsmapserviceexception.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsdatetimefieldformatter.h"

#include <QStringList>

Expand Down Expand Up @@ -307,6 +310,20 @@ namespace QgsWfs
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "string" ) );
}

const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
if ( setup.type() == QStringLiteral( "DateTime" ) )
{
QgsDateTimeFieldFormatter fieldFormatter;
const QVariantMap config = setup.config();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( field.type() ) ).toString();
if ( fieldFormat == QStringLiteral( "yyyy-MM-dd" ) )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
else if ( fieldFormat == QStringLiteral( "HH:mm:ss" ) )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
else
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
}

sequenceElem.appendChild( attElem );

QString alias = field.alias();
Expand Down
32 changes: 26 additions & 6 deletions src/server/services/wfs/qgswfsgetfeature.cpp
Expand Up @@ -22,6 +22,9 @@
#include "qgswfsutils.h"
#include "qgsserverprojectutils.h"
#include "qgsfields.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsdatetimefieldformatter.h"
#include "qgsexpression.h"
#include "qgsgeometry.h"
#include "qgsmaplayer.h"
Expand Down Expand Up @@ -62,7 +65,7 @@ namespace QgsWfs

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

QString encodeValueToText( const QVariant &value );
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup );

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

Expand Down Expand Up @@ -1337,10 +1340,12 @@ namespace QgsWfs
{
continue;
}
QString attributeName = fields.at( idx ).name();
const QgsField field = fields.at( idx );
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
QString attributeName = field.name();

QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}
Expand Down Expand Up @@ -1432,22 +1437,37 @@ namespace QgsWfs
{
continue;
}
QString attributeName = fields.at( idx ).name();
const QgsField field = fields.at( idx );
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
QString attributeName = field.name();

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

return featureElement;
}

QString encodeValueToText( const QVariant &value )
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup )
{
if ( value.isNull() )
return QStringLiteral( "null" );

if ( setup.type() == QStringLiteral( "DateTime" ) )
{
QgsDateTimeFieldFormatter fieldFormatter;
const QVariantMap config = setup.config();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( value.type() ) ).toString();
QDateTime date = value.toDateTime();

if ( date.isValid() )
{
return date.toString( fieldFormat );
}
}

switch ( value.type() )
{
case QVariant::Int:
Expand Down

0 comments on commit 37c1e23

Please sign in to comment.