Friday, 7 June 2019

pyqgis - How to change attributes values in a memory layer?


i want to change attributes values in a memory layer.


I already asked a similar question but i'm re-stuck on the problem and don't find any solution to this basic need.


Here is what i'm doing so far :


from PyQt4.QtCore import *

from PyQt4.QtGui import *

vl_r=QgsVectorLayer('Polygon?crs=epsg:2154','population','memory')
pro_r = vl_r.dataProvider()

pro_r.addAttributes([QgsField("name", QVariant.String)])
pro_r.addAttributes([QgsField("pop", QVariant.Int)])

vl_r.updateFields()


QgsMapLayerRegistry.instance().addMapLayer(vl_r)

fet = QgsFeature()
dummy=pro_r.addFeatures([fet])

print(pro_r.capabilitiesString())

attrib={0:'hell',1:666}
dummy=vl_r.dataProvider().changeAttributeValues({fet.id():attrib})
print('Change values : ' + str(dummy))


The value returned by 'dummy' is always True but i never see any change in the attributes table.


When i use a 'normal' layer (stored on disk) the change is made and is immediately visible without any additional command.


What could i be missing ? My code is so simple that i can't see what i forgot ...


Could anybody give me a working sample of attributes change in a memory Layer ?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...