[Qgis-developer] addAttribute to memory layer
Ing. Pierluigi De Rosa
pierluigi.derosa at gfosservices.it
Mon Oct 7 01:17:17 PDT 2013
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131007/d6367045/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Banner_sito.jpg
Type: image/jpeg
Size: 5164 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131007/d6367045/attachment.jpg>
More information about the Qgis-developer
mailing list