[Qgis-developer] Few improvements in PyQGIS

Chris Crook ccrook at linz.govt.nz
Wed Feb 10 17:11:27 EST 2010

Hi Martin

Since you asked, one suggestion for pythonisation(??)   It would be good to have an layers() iterator over map canvas layers something like

for l in iface.mapCanvas().layers():


-----Original Message-----
From: qgis-developer-bounces at lists.osgeo.org [mailto:qgis-developer-bounces at lists.osgeo.org] On Behalf Of Martin Dobias
Sent: Friday, 5 February 2010 10:24 a.m.
To: qgis-dev
Subject: [Qgis-developer] Few improvements in PyQGIS

Hi all

I hope this is a good news for developers using python. In svn trunk
(r12878) there are few additions that make QGIS API more pythonic.

QgsFeature allows direct access to attributes (get, set, delete attribute):
>>> f = QgsFeature()
>>> f[1] = QVariant("hello")
>>> print f[1].toString()
>>> del f[1]
>>> print f.attributeMap()

QgsVectorLayer and QgsVectorDataProvider support iterating (available for those with sip >= 4.9).

layer.select([], QgsRectangle())
for f in layer:
  # do something with feature f

--- equals to ---

layer.select([], QgsRectangle())
f = QgsFeature()
while layer.nextFeature(f):
  # do something with feature f

That's it. The older idioms (using nextFeature) keep working too.

Inspired by a blog post from Sean Gillies:

Btw. fetching a list of values in an column gets this simple:
>>> lst = [ feat[1] for feat in layer ]
(just don't forget a select() call before iterating)

If you have any tips what other parts of QGIS API could be made more pythonic, let me know.

Qgis-developer mailing list
Qgis-developer at lists.osgeo.org

This message contains information, which is confidential and may be subject to legal privilege. 
If you are not the intended recipient, you must not peruse, use, disseminate, distribute or copy this message.
If you have received this message in error, please notify us immediately (Phone 0800 665 463 or info at linz.govt.nz) and destroy the original message.
LINZ accepts no responsibility for changes to this email, or for any attachments, after its transmission from LINZ.

Thank you.

More information about the Qgis-developer mailing list