Dear All,
I'm converting a plugin with new API but I still doen't figure out how
to add attribute to feature.
I know it's a stupid question but i think the cookbook is till not
updated?

http://qgis.org/it/docs/pyqgis_developer_cookbook/vector.html#add-features

I tried this code without success:

vl = QgsVectorLayer("Point?crs=epsg:4326", 'test', "memory")
pr = vl.dataProvider()                 
#~ add fields
pr.addAttributes( [QgsField('text', QVariant.String),QgsField('date',
QVariant.String)])
fields = vl.pendingFields()
feature = QgsFeature(fields)
feature[0] = 'some text'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: '0'


Below I attached the code I used in 1.8 


vl = QgsVectorLayer("Point?crs=epsg:4326", 'test', "memory")
pr = vl.dataProvider()                        
 #~ add fields
 pr.addAttributes( [QgsField('text', QVariant.String),QgsField('date',
QVariant.String)])                           
#~ add features point
for pointID in range(len(coordNotes)):
       fet = QgsFeature()
        xcoor=float(coordNotes[pointID][0])
        ycoor=float(coordNotes[pointID][1])
        fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(xcoor,ycoor))) 
        #~ add attribute value
         fet.addAttribute(0, QVariant( dateNote[pointID][0] ))
         fet.addAttribute(1, QVariant( dateNote[pointID][1] ))
pr.addFeatures( [ fet ] )
#~ update vector layer    
vl.updateExtents()
vl.updateFieldMap()


Thanks
Pierluigi
-- 


                                    
________________________________________________________________________

Ing. Pierluigi De Rosa (PhD)

Studio Associato GFOSSERVICES

                
Via Tilli 58 - 06127 Perugia (PG) 

tel: 075 7825101 / fax: 075 7823038

cel: 3497558268

web: www.gfosservices.it

skype: pierluigi.derosa 

<<attachment: Banner_sito.jpg>>

_______________________________________________
Qgis-developer mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to