Skip to content

Commit

Permalink
Merge pull request #7902 from rldhont/server-wfs-format-field
Browse files Browse the repository at this point in the history
[Bugfix][Server][WFS] Server wfs format field
  • Loading branch information
rldhont committed Sep 27, 2018
2 parents 3a70f88 + 68ba754 commit b9f20b0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 10 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
74 changes: 68 additions & 6 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 @@ -251,8 +254,8 @@ namespace QgsWfs
const QSet<QString> &layerExcludedAttributes = layer->excludeAttributesWfs();
for ( int idx = 0; idx < fields.count(); ++idx )
{

QString attributeName = fields.at( idx ).name();
const QgsField field = fields.at( idx );
QString attributeName = field.name();
//skip attribute if excluded from WFS publication
if ( layerExcludedAttributes.contains( attributeName ) )
{
Expand All @@ -262,27 +265,86 @@ namespace QgsWfs
//xsd:element
QDomElement attElem = doc.createElement( QStringLiteral( "element" )/*xsd:element*/ );
attElem.setAttribute( QStringLiteral( "name" ), attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QVariant::Type attributeType = fields.at( idx ).type();
QVariant::Type attributeType = field.type();
if ( attributeType == QVariant::Int )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "integer" ) );
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
}
else if ( attributeType == QVariant::UInt )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unsignedInt" ) );
}
else if ( attributeType == QVariant::LongLong )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "long" ) );
}
else if ( attributeType == QVariant::ULongLong )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unsignedLong" ) );
}
else if ( attributeType == QVariant::Double )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "double" ) );
{
if ( field.length() != 0 && field.precision() == 0 )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "integer" ) );
else
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "decimal" ) );
}
else if ( attributeType == QVariant::Bool )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "boolean" ) );
}
else if ( attributeType == QVariant::Date )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
}
else if ( attributeType == QVariant::Time )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
}
else if ( attributeType == QVariant::DateTime )
{
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
}
else
{
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" ) );
}
else if ( setup.type() == QStringLiteral( "Range" ) )
{
const QVariantMap config = setup.config();
if ( config.contains( QStringLiteral( "Precision" ) ) )
{
// if precision in range config is not the same as the attributePrec
// we need to update type
bool ok;
int configPrec( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
if ( ok && configPrec != field.precision() )
{
if ( configPrec == 0 )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "integer" ) );
else
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "decimal" ) );
}
}
}

sequenceElem.appendChild( attElem );

QString alias = fields.at( idx ).alias();
QString alias = field.alias();
if ( !alias.isEmpty() )
{
attElem.setAttribute( QStringLiteral( "alias" ), alias );
Expand Down
84 changes: 80 additions & 4 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,6 +65,8 @@ namespace QgsWfs

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

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

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

QDomElement createFeatureGML3( QgsFeature *feat, QDomDocument &doc, const createFeatureParams &params, const QgsProject *project );
Expand Down Expand Up @@ -1335,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( featureAttributes[idx].toString() );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}
Expand Down Expand Up @@ -1430,18 +1437,87 @@ 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( featureAttributes[idx].toString() );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}

return featureElement;
}

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 );
}
}
else if ( setup.type() == QStringLiteral( "Range" ) )
{
const QVariantMap config = setup.config();
if ( config.contains( QStringLiteral( "Precision" ) ) )
{
// if precision is defined, use it
bool ok;
int precision( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
if ( ok )
return QString::number( value.toDouble(), 'f', precision );
}
}

switch ( value.type() )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Double:
return value.toString();

case QVariant::Bool:
return value.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );

case QVariant::StringList:
case QVariant::List:
case QVariant::Map:
{
QString v = QgsJsonUtils::encodeValue( value );

//do we need CDATA
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
v.prepend( QStringLiteral( "<![CDATA[" ) ).append( QStringLiteral( "]]>" ) );

return v;
}

default:
case QVariant::String:
{
QString v = value.toString();

//do we need CDATA
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
v.prepend( QStringLiteral( "<![CDATA[" ) ).append( QStringLiteral( "]]>" ) );

return v;
}
}
}


} // namespace
Expand Down

0 comments on commit b9f20b0

Please sign in to comment.