[Qgis-developer] Getting and setting attribute field names and values for vector layers

Ole Nielsen ole.moller.nielsen at gmail.com
Tue Dec 6 07:06:49 EST 2011


Hi all

I am a newcomer to Qgis plugins but not to Python and geoprocessing. Trying
my hand at a plugin today I got pretty much to where I want to be in a very
short time thanks to the excellent tutorials and logical design of the
Python bindings.
However, one issue threw me completely. The cookbook has a great example of
getting geometries and attributes (
http://www.qgis.org/pyqgis-cookbook/vector.html#iterating-over-vector-layer)
but when it comes to reading the attributes it suggests the code below
which produces the attribute *values* but uses integers for keys, even
though in general there are field names in the vector data. I consulted
with the class definition of the underlying library
http://qgis.sourceforge.net/html/classQgsFeatureAttribute.html which makes
a lot of sense having public methods fieldName ()  and fieldValue().
However, I was not able to find these in the Python bindings. I am sure
this is a super simple question reflecting that I have only spend 1 day so
far with this so please help me get on the right track.

Cheers and thanks
Ole Nielsen
Australia-Indonesia Facility for Disaster Reduction, AusAID, Jakarta

---------------------------------
Snippet from cookbook
---------------------------------

# fetch map of attributes
attrs = feat.attributeMap()

# attrs is a dictionary: key = field index, value = QgsFeatureAttribute
# show all attributes and their values
for (k,attr) in attrs.iteritems():
    print "%d: %s" % (k, attr.toString())   # PROBLEM: This only gives
attribute *values* and not their field names. I had expected them to be
keys in the dictionary.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20111206/181a418b/attachment.html


More information about the Qgis-developer mailing list