[Qgis-developer] Issue with creating new feature

Martin Dobias wonder.sk at gmail.com
Thu Mar 14 01:09:32 PDT 2013


On Wed, Feb 27, 2013 at 12:06 PM, Alexander Bruy
<alexander.bruy at gmail.com> wrote:
> 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

Just for the record, right now it is necessary to call
f.initAttributes(..) to tell the number of attributes. Before 2.0 I
would like to update QgsFeature API a bit to make things easier: e.g.
QgsFeature(fields) constructor call would also call initAttributes()
and setFields()

Martin


More information about the Qgis-developer mailing list