[Qgis-developer] Few improvements in PyQGIS

Carson Farmer carson.farmer at gmail.com
Thu Feb 4 18:50:59 EST 2010


beautiful Martin! Nice job!

Carson

2010/2/4 Martin Dobias <wonder.sk at gmail.com>:
> 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()
> hello
>>>> 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:
> http://sgillies.net/blog/952/iterators
>
> 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.
>
> Martin
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation
National University of Ireland, Maynooth,
http://www.carsonfarmer.com/


More information about the Qgis-developer mailing list