Merge pull request #7902 from rldhont/server-wfs-format-field

[Bugfix][Server][WFS] Server wfs format field
This commit is contained in:
rldhont 2018-09-27 23:03:10 +02:00 committed by GitHub
commit b9f20b0ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 149 additions and 10 deletions

View File

@ -45,6 +45,7 @@ INCLUDE_DIRECTORIES(
../../../core/raster
../../../core/symbology
../../../core/layertree
../../../core/fieldformatter
../..
..
.

View File

@ -30,6 +30,9 @@
#include "qgsvectordataprovider.h"
#include "qgsmapserviceexception.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsdatetimefieldformatter.h"
#include <QStringList>
@ -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 ) )
{
@ -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 );

View File

@ -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"
@ -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 );
@ -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 );
}
@ -1430,10 +1437,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 );
}
@ -1441,7 +1450,74 @@ namespace QgsWfs
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