[Qgis-developer] QgsFeature addAttribute to writer

Nathan Woodrow madmanwoo at gmail.com
Sat Aug 17 03:08:32 PDT 2013


Hi Bjorn,

The API has changed in 2.0.   You can now just do this:

writer = QgsVectorFileWriter(Output, "CP1250", fields,
layer.dataProvider().geometryType(),layer.crs(),
"ESRI Shapefile")
fields = layer.pendingFields()
feature = QgsFeature(fields)
feature[0] = 1

- Nathan




On Sat, Aug 17, 2013 at 6:35 PM, Bjorn Nyberg
<bjorn.burr.nyberg at gmail.com>wrote:

> Hej,
> Is it possible to add an attribute to the writer when writing a geometry?
> It would appear this would be possible as explained in the 1.8 docs here (
> http://www.qgis.org/pyqgis-cookbook/vector.html#writing-vector-layers).
> However when I attempt something similar in 2.0
> I received the following errors
>
> writer = QgsVectorFileWriter(Output, "CP1250", fields,
> layer.dataProvider().geometryType(),layer.crs(), "ESRI Shapefile")
> fet = QgsFeature()
> fet.addAttribute(0, QVariant(1))
> AttributeError: 'QgsFeature' object has no attribute 'addAttribute'
> fet.setAttributeMap( { 0 : QVariant(1) })
> AttributeError: 'QgsFeature' object has no attribute 'setAttributeMap'
>
> Currently I have a work around by creating a dictionary keyed by the
> feature id (i.e. corresponding to the order in which the geometries are
> created) and then looping through the layer a second time and updating the
> attributes. It appears that this second loop is unnecessary if I can write
> the attributes when writing the geometries to the QgsFeature().
>
> Cheers,
> Bjorn
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130817/3b38f762/attachment-0001.html>


More information about the Qgis-developer mailing list