Skip to content

Commit 018fd07

Browse files
authored
Merge pull request #7870 from rldhont/server-wfs-format-field-218
[Bugfix][Server][WFS] correctly define field type and encode values
2 parents ff5bd09 + f427577 commit 018fd07

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

src/server/qgswfsprojectparser.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ void QgsWFSProjectParser::describeFeatureType( const QString& aTypeName, QDomEle
455455
const QgsFields& fields = layer->pendingFields();
456456
for ( int idx = 0; idx < fields.count(); ++idx )
457457
{
458-
459-
QString attributeName = fields.at( idx ).name();
458+
const QgsField field = fields.at( idx );
459+
QString attributeName = field.name();
460460
//skip attribute if excluded from WFS publication
461461
if ( layerExcludedAttributes.contains( attributeName ) )
462462
{
@@ -466,27 +466,57 @@ void QgsWFSProjectParser::describeFeatureType( const QString& aTypeName, QDomEle
466466
//xsd:element
467467
QDomElement attElem = doc.createElement( "element"/*xsd:element*/ );
468468
attElem.setAttribute( "name", attributeName.replace( " ", "_" ).replace( mCleanTagNameRegExp, "" ) );
469-
QVariant::Type attributeType = fields[idx].type();
469+
QVariant::Type attributeType = field.type();
470470
if ( attributeType == QVariant::Int )
471-
attElem.setAttribute( "type", "integer" );
471+
{
472+
attElem.setAttribute( "type", "int" );
473+
}
474+
else if ( attributeType == QVariant::UInt )
475+
{
476+
attElem.setAttribute( "type", "unsignedInt" );
477+
}
472478
else if ( attributeType == QVariant::LongLong )
479+
{
473480
attElem.setAttribute( "type", "long" );
481+
}
482+
else if ( attributeType == QVariant::ULongLong )
483+
{
484+
attElem.setAttribute( "type", "unsignedLong" );
485+
}
474486
else if ( attributeType == QVariant::Double )
475-
attElem.setAttribute( "type", "double" );
487+
{
488+
// if the size is well known, it may be an integer
489+
// else a decimal
490+
// in sqlite the length is unknown but int type can be used
491+
if ( field.length() != 0 && field.precision() == 0 )
492+
attElem.setAttribute( "type", "integer" );
493+
else
494+
attElem.setAttribute( "type", "decimal" );
495+
}
476496
else if ( attributeType == QVariant::Bool )
497+
{
477498
attElem.setAttribute( "type", "boolean" );
499+
}
478500
else if ( attributeType == QVariant::Date )
501+
{
479502
attElem.setAttribute( "type", "date" );
503+
}
480504
else if ( attributeType == QVariant::Time )
505+
{
481506
attElem.setAttribute( "type", "time" );
507+
}
482508
else if ( attributeType == QVariant::DateTime )
509+
{
483510
attElem.setAttribute( "type", "dateTime" );
511+
}
484512
else
513+
{
485514
attElem.setAttribute( "type", "string" );
515+
}
486516

487517
sequenceElem.appendChild( attElem );
488518

489-
QString alias = fields.at( idx ).alias();
519+
QString alias = field.alias();
490520
if ( !alias.isEmpty() )
491521
{
492522
attElem.setAttribute( "alias", alias );

src/server/qgswfsserver.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ QDomElement QgsWFSServer::createFeatureGML2( QgsFeature* feat, QDomDocument& doc
21292129
}*/
21302130

21312131
QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( " ", "_" ).replace( mConfigParser->getCleanTagNameRegExp(), "" ) );
2132-
QDomText fieldText = doc.createTextNode( featureAttributes[idx].toString() );
2132+
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
21332133
fieldElem.appendChild( fieldText );
21342134
typeNameElement.appendChild( fieldElem );
21352135
}
@@ -2213,14 +2213,62 @@ QDomElement QgsWFSServer::createFeatureGML3( QgsFeature* feat, QDomDocument& doc
22132213
}*/
22142214

22152215
QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( " ", "_" ).replace( mConfigParser->getCleanTagNameRegExp(), "" ) );
2216-
QDomText fieldText = doc.createTextNode( featureAttributes[idx].toString() );
2216+
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
22172217
fieldElem.appendChild( fieldText );
22182218
typeNameElement.appendChild( fieldElem );
22192219
}
22202220

22212221
return featureElement;
22222222
}
22232223

2224+
QString QgsWFSServer::encodeValueToText( const QVariant& value )
2225+
{
2226+
if ( value.isNull() )
2227+
return "null";
2228+
2229+
switch ( value.type() )
2230+
{
2231+
case QVariant::Int:
2232+
case QVariant::UInt:
2233+
case QVariant::LongLong:
2234+
case QVariant::ULongLong:
2235+
case QVariant::Double:
2236+
return value.toString();
2237+
2238+
case QVariant::Bool:
2239+
return value.toBool() ? "true" : "false";
2240+
2241+
case QVariant::StringList:
2242+
case QVariant::List:
2243+
case QVariant::Map:
2244+
{
2245+
QString v = QgsJSONUtils::encodeValue( value );
2246+
2247+
//do we need CDATA
2248+
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
2249+
{
2250+
v.prepend( "<![CDATA[" ).append( "]]>" );
2251+
}
2252+
2253+
return v;
2254+
}
2255+
2256+
default:
2257+
case QVariant::String:
2258+
{
2259+
QString v = value.toString();
2260+
2261+
//do we need CDATA
2262+
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
2263+
{
2264+
v.prepend( "<![CDATA[" ).append( "]]>" );
2265+
}
2266+
2267+
return v;
2268+
}
2269+
}
2270+
}
2271+
22242272
QString QgsWFSServer::serviceUrl() const
22252273
{
22262274
QUrl mapUrl( getenv( "REQUEST_URI" ) );

src/server/qgswfsserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class QgsWFSServer: public QgsOWSServer
136136
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, int prec, QgsCoordinateReferenceSystem& crs, const QgsAttributeList& attrIndexes,
137137
const QgsAttributeList& pkAttributes = QgsAttributeList() ) /*const*/;
138138

139+
//methods to encode value to string for text node
140+
QString encodeValueToText( const QVariant& value );
141+
139142
void addTransactionResult( QDomDocument& responseDoc, QDomElement& responseElem, const QString& status, const QString& locator, const QString& message );
140143
};
141144

0 commit comments

Comments
 (0)