[Qgis-developer] Few improvements in PyQGIS

Carson Farmer carson.farmer at gmail.com
Thu Apr 8 18:23:10 EDT 2010


Is there a minimum SIP version that we can assume? I'd like to take
advantage of some of Martin's nice changes, but I want to make sure
they'll be usable for all...

Carson

On Thu, Feb 4, 2010 at 9:24 PM, Martin Dobias <wonder.sk at gmail.com> wrote:
> 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