Skip to content

Commit 2034de2

Browse files
author
wonder
committedFeb 4, 2010
PyQGIS: few additions for more pythonic api
- QgsVectorLayer and QgsVectorDataProvider support iterating - QgsFeature allows direct access to attributes (get/set/del) git-svn-id: http://svn.osgeo.org/qgis/trunk@12878 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 54b079e commit 2034de2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
 

‎python/core/qgsfeature.sip

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ class QgsFeature
88

99
public:
1010

11+
SIP_PYOBJECT __getitem__(int key);
12+
%MethodCode
13+
const QgsAttributeMap& attrMap = sipCpp->attributeMap();
14+
QgsAttributeMap::const_iterator it = attrMap.find(a0);
15+
if (it == attrMap.end())
16+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
17+
else
18+
{
19+
QVariant* v = new QVariant(it.value());
20+
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
21+
}
22+
%End
23+
24+
void __setitem__(int key, QVariant value);
25+
%MethodCode
26+
sipCpp->addAttribute(a0, *a1);
27+
%End
28+
29+
void __delitem__(int key);
30+
%MethodCode
31+
if (sipCpp->attributeMap().contains(a0))
32+
sipCpp->deleteAttribute(a0);
33+
else
34+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
35+
%End
36+
1137
//! Constructor
1238
QgsFeature(int id = 0, QString typeName = "" );
1339

‎python/core/qgsvectordataprovider.sip

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ class QgsVectorDataProvider : QgsDataProvider
77

88
public:
99

10+
QgsVectorDataProvider* __iter__();
11+
%MethodCode
12+
sipRes = sipCpp;
13+
%End
14+
15+
SIP_PYOBJECT __next__();
16+
%MethodCode
17+
QgsFeature* f = new QgsFeature;
18+
if (sipCpp->nextFeature(*f))
19+
sipRes = sipConvertFromInstance(f, sipClass_QgsFeature, Py_None);
20+
else
21+
{
22+
delete f;
23+
PyErr_SetString(PyExc_StopIteration,"");
24+
}
25+
%End
26+
1027
// If you add to this, please also add to capabilitiesString()
1128
/**
1229
* enumeration with capabilities that providers might implement

‎python/core/qgsvectorlayer.sip

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ class QgsVectorLayer : QgsMapLayer
55
%End
66

77
public:
8+
9+
QgsVectorLayer* __iter__();
10+
%MethodCode
11+
sipRes = sipCpp;
12+
%End
13+
14+
SIP_PYOBJECT __next__();
15+
%MethodCode
16+
QgsFeature* f = new QgsFeature;
17+
if (sipCpp->nextFeature(*f))
18+
sipRes = sipConvertFromInstance(f, sipClass_QgsFeature, Py_None);
19+
else
20+
{
21+
delete f;
22+
PyErr_SetString(PyExc_StopIteration,"");
23+
}
24+
%End
25+
26+
827
enum EditType {
928
LineEdit,
1029
UniqueValues,

0 commit comments

Comments
 (0)
Please sign in to comment.