[Qgis-developer] Issue with creating new feature
Alexander Bruy
alexander.bruy at gmail.com
Wed Feb 27 03:06:36 PST 2013
Hi all,
during plugin update to new vector API I found some strange
behaviour with creating new features in vector layer/file writer.
There is nice description about how to retrieve features and
attributes from existing vector layer, but I can't find any
information about creating new ones.
Here is my test code for Python console:
>>>from PyQt4.QtCore import *
>>>fields = QgsFields()
>>>fields.append(QgsField("id", QVariant.Int, "", 20))
>>>fields.append(QgsField("value", QVariant.String, "", 255))
>>>fields.append(QgsField("area", QVariant.Double, "", 20, 6))
>>>fields.append(QgsField("perim", QVariant.Double, "", 20, 6))
>>>w = QgsVectorFileWriter("/home/alex/test.shp", "utf-8", fields, >>>QGis.WKBPolygon, None)
>>>f = QgsFeature()
>>>f.setAttribute("id", QVariant(0))
False
>>>f.setAttribute(0, QVariant(0))
False
It is impossible to add attribute by its name or index. In API docs
I found method setFields() that used to "assign a field map with
the feature to allow attribute access by attribute name". But when
I assign field map to feature
>>>f.setFields(fields)
and try to add attribute again by name or id I get crash
(stacktrace attached).
It is possible to set all attributes at once using
>>>f.setAttributes([QVariant(0), QVariant("text"), QVariant(2.3), QVariant(100.456)])
Is this correct or maybe I miss something?
Thanks
--
Alexander Bruy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stacktrace.txt
Type: application/octet-stream
Size: 4214 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130227/da0f6370/attachment.obj>
More information about the Qgis-developer
mailing list