<div dir="ltr">Hi Bjorn,<div><br></div><div>The API has changed in 2.0.   You can now just do this:</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">writer = QgsVectorFileWriter(Output, "CP1250", fields, layer.dataProvider().</span><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">geometryType(),layer.crs(), "ESRI Shapefile")</span><br>

</div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">fields = layer.pendingFields()</span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">feature = QgsFeature(fields)</span></div>

<div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">feature[0] = 1</span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font color="#000000" face="arial, sans-serif">- Nathan</font></div>

<div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div></div><div class="gmail_extra">

<br><br><div class="gmail_quote">On Sat, Aug 17, 2013 at 6:35 PM, Bjorn Nyberg <span dir="ltr"><<a href="mailto:bjorn.burr.nyberg@gmail.com" target="_blank">bjorn.burr.nyberg@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div>Hej,</div><div>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 (<a href="http://www.qgis.org/pyqgis-cookbook/vector.html#writing-vector-layers" target="_blank">http://www.qgis.org/pyqgis-cookbook/vector.html#writing-vector-layers</a>). However when I attempt something similar in 2.0 </div>

<div>I received the following errors</div><div><br></div><div><div style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">writer = QgsVectorFileWriter(Output, "CP1250", fields, layer.dataProvider().geometryType(),layer.crs(), "ESRI Shapefile")</div>

</div><div><div style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">fet = QgsFeature()</div></div>fet.addAttribute(0, QVariant(1)) <br>AttributeError: 'QgsFeature' object has no attribute 'addAttribute' <br>

fet.setAttributeMap( { 0 : QVariant(1) }) <br>AttributeError: 'QgsFeature' object has no attribute 'setAttributeMap' <div><br></div><div>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().</div>

<div><br></div><div>Cheers,</div><div>Bjorn</div></div><br>_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br></blockquote></div><br></div>