Skip to content

Commit 6373876

Browse files
author
mhugent
committed
Improved handling of attribute types in WFS provider
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10866 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent dcb312e commit 6373876

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/providers/wfs/qgswfsdata.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ QgsWFSData::QgsWFSData(
3636
QgsCoordinateReferenceSystem* srs,
3737
QList<QgsFeature*> &features,
3838
const QString& geometryAttribute,
39-
const QSet<QString>& thematicAttributes,
39+
const QMap<QString, QPair<int, QgsField> >& thematicAttributes,
4040
QGis::WkbType* wkbType )
4141
: QObject(),
4242
mUri( uri ),
@@ -193,7 +193,6 @@ void QgsWFSData::startElement( const XML_Char* el, const XML_Char** attr )
193193
else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + "featureMember" )
194194
{
195195
mCurrentFeature = new QgsFeature( mFeatureCount );
196-
mAttributeIndex = 0;
197196
mParseModeStack.push( QgsWFSData::featureMember );
198197
}
199198
else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + "Box" && mParseModeStack.top() == QgsWFSData::boundingBox )
@@ -262,15 +261,35 @@ void QgsWFSData::endElement( const XML_Char* el )
262261
mParseModeStack.pop();
263262
}
264263
}
265-
else if ( localName == mAttributeName )
264+
else if ( localName == mAttributeName ) //add a thematic attribute to the feature
266265
{
267266
if ( !mParseModeStack.empty() )
268267
{
269268
mParseModeStack.pop();
270269
}
271270

272-
mCurrentFeature->addAttribute( mAttributeIndex, QVariant( mStringCash ) );
273-
++mAttributeIndex;
271+
//find index with attribute name
272+
QMap<QString, QPair<int, QgsField> >::const_iterator att_it = mThematicAttributes.find(mAttributeName);
273+
if(att_it != mThematicAttributes.constEnd())
274+
{
275+
QVariant var;
276+
switch(att_it.value().second.type())
277+
{
278+
case QVariant::Double:
279+
var = QVariant(mStringCash.toDouble());
280+
break;
281+
case QVariant::Int:
282+
var = QVariant(mStringCash.toInt());
283+
break;
284+
case QVariant::LongLong:
285+
var = QVariant(mStringCash.toLongLong());
286+
break;
287+
default: //string type is default
288+
var = QVariant( mStringCash );
289+
break;
290+
}
291+
mCurrentFeature->addAttribute(att_it.value().first, QVariant( mStringCash ));
292+
}
274293
}
275294
else if ( localName == mGeometryAttribute )
276295
{

src/providers/wfs/qgswfsdata.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsapplication.h"
2222
#include "qgsdataprovider.h"
2323
#include "qgsfeature.h"
24+
#include "qgsfield.h"
2425
#include "qgspoint.h"
2526
#include <list>
2627
#include <set>
@@ -40,7 +41,7 @@ class QgsWFSData: public QObject
4041
QgsCoordinateReferenceSystem* srs,
4142
QList<QgsFeature*> &features,
4243
const QString& geometryAttribute,
43-
const QSet<QString>& thematicAttributes,
44+
const QMap<QString, QPair<int, QgsField> >& thematicAttributes,
4445
QGis::WkbType* wkbType );
4546
~QgsWFSData();
4647

@@ -150,7 +151,7 @@ class QgsWFSData: public QObject
150151
QList<QgsFeature*> &mFeatures;
151152
/**Name of geometry attribute*/
152153
QString mGeometryAttribute;
153-
const QSet<QString> &mThematicAttributes;
154+
const QMap<QString, QPair<int, QgsField> > &mThematicAttributes;
154155
QGis::WkbType* mWkbType;
155156
/**True if the request is finished*/
156157
bool mFinished;
@@ -171,8 +172,6 @@ class QgsWFSData: public QObject
171172
/**Similar to mCurrentWKB, but only the size*/
172173
std::list< std::list<int> > mCurrentWKBFragmentSizes;
173174
QString mAttributeName;
174-
/**Index where the current attribute should be inserted*/
175-
int mAttributeIndex;
176175
QString mTypeName;
177176
QgsApplication::endian_t mEndian;
178177
/**Coordinate separator for coordinate strings. Usually "," */

src/providers/wfs/qgswfsprovider.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ int QgsWFSProvider::describeFeatureType( const QString& uri, QString& geometryAt
234234

235235
int QgsWFSProvider::getFeatureGET( const QString& uri, const QString& geometryAttribute )
236236
{
237-
#if 0
237+
#if 0 //the old and slower method with DOM
238238
//assemble request string
239239
QString request = uri /*+ "&OUTPUTFORMAT=gml3"*/; //use gml2 as it is supported by most wfs servers
240240
QByteArray result;
@@ -265,11 +265,13 @@ int QgsWFSProvider::getFeatureGET( const QString& uri, const QString& geometryAt
265265
return 0;
266266
#endif
267267

268-
//the new and faster method with the expat parser
269-
QSet<QString> thematicAttributes;
268+
//the new and faster method with the expat SAX parser
269+
270+
//allows fast searchings with attribute name. Also needed is attribute Index and type infos
271+
QMap<QString, QPair<int, QgsField> > thematicAttributes;
270272
for ( QgsFieldMap::const_iterator it = mFields.begin(); it != mFields.end(); ++it )
271273
{
272-
thematicAttributes << it->name();
274+
thematicAttributes.insert(it.value().name(), qMakePair(it.key(), it.value()));
273275
}
274276

275277
QgsWFSData dataReader( uri, &mExtent, &mSourceCRS, mFeatures, geometryAttribute, thematicAttributes, &mWKBType );
@@ -512,7 +514,20 @@ int QgsWFSProvider::readAttributesFromSchema( QDomDocument& schemaDoc, QString&
512514
}
513515
else //todo: distinguish between numerical and non-numerical types
514516
{
515-
fields[fields.size()] = QgsField( name, QVariant::String, type );
517+
QVariant::Type attributeType = QVariant::String; //string is default type
518+
if(type.contains("double", Qt::CaseInsensitive) || type.contains("float", Qt::CaseInsensitive))
519+
{
520+
attributeType = QVariant::Double;
521+
}
522+
else if(type.contains("int", Qt::CaseInsensitive))
523+
{
524+
attributeType = QVariant::Int;
525+
}
526+
else if(type.contains("long", Qt::CaseInsensitive))
527+
{
528+
attributeType = QVariant::LongLong;
529+
}
530+
fields[fields.size()] = QgsField( name, attributeType, type );
516531
}
517532
}
518533
return 0;

0 commit comments

Comments
 (0)