Skip to content

Commit 6f3b0ca

Browse files
committedNov 8, 2016
Add method to QgsAttributes to convert to QgsAttributeMap
1 parent f9bb230 commit 6f3b0ca

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
 

‎src/core/qgsfeature.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ email : sherman at mrcc.com
2929
* See details in QEP #17
3030
****************************************************************************/
3131

32+
33+
QgsAttributeMap QgsAttributes::toMap() const
34+
{
35+
QgsAttributeMap map;
36+
for ( int idx = 0; idx < count(); ++idx )
37+
{
38+
QVariant v = at( idx );
39+
if ( v.isValid() )
40+
map.insert( idx, v );
41+
}
42+
return map;
43+
}
44+
45+
//
46+
// QgsFeature
47+
//
48+
3249
QgsFeature::QgsFeature( QgsFeatureId id )
3350
{
3451
d = new QgsFeaturePrivate( id );
@@ -327,3 +344,4 @@ uint qHash( const QgsFeature& key, uint seed )
327344

328345
return hash;
329346
}
347+

‎src/core/qgsfeature.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class CORE_EXPORT QgsAttributes : public QVector<QVariant>
107107
return true;
108108
}
109109

110+
/**
111+
* Returns a QgsAttributeMap of the attribute values. Null values are
112+
* excluded from the map.
113+
* @note added in QGIS 3.0
114+
* @note not available in Python bindings
115+
*/
116+
QgsAttributeMap toMap() const;
117+
110118
inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
111119
};
112120

‎tests/src/core/testqgsfeature.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TestQgsFeature: public QObject
3333
void init();// will be called before each testfunction is executed.
3434
void cleanup();// will be called after every testfunction.
3535
void attributesTest(); //test QgsAttributes
36+
void attributesToMap();
3637
void create();//test creating a feature
3738
void copy();// test cpy destruction (double delete)
3839
void assignment();
@@ -120,6 +121,28 @@ void TestQgsFeature::attributesTest()
120121
QCOMPARE( attr7.size(), 5 );
121122
}
122123

124+
void TestQgsFeature::attributesToMap()
125+
{
126+
QgsAttributes attr1;
127+
attr1 << QVariant( 5 ) << QVariant() << QVariant( "val" );
128+
QgsAttributeMap map1 = attr1.toMap();
129+
130+
QCOMPARE( map1.count(), 2 );
131+
QCOMPARE( map1.value( 0 ), QVariant( 5 ) );
132+
QCOMPARE( map1.value( 2 ), QVariant( "val" ) );
133+
134+
QgsAttributes attr2;
135+
attr2 << QVariant() << QVariant( 5 ) << QVariant();
136+
QgsAttributeMap map2 = attr2.toMap();
137+
138+
QCOMPARE( map2.count(), 1 );
139+
QCOMPARE( map2.value( 1 ), QVariant( 5 ) );
140+
141+
QgsAttributes attr3;
142+
QgsAttributeMap map3 = attr3.toMap();
143+
QVERIFY( map3.isEmpty() );
144+
}
145+
123146
void TestQgsFeature::create()
124147
{
125148
//test constructors

0 commit comments

Comments
 (0)
Please sign in to comment.