添加新的QgsFeatu时,QgsVectorLayer的属性表不会更新

2024-05-20 02:04:04 发布

您现在位置:Python中文网/ 问答频道 /正文

将新的QgsFeature添加到现有QgsVectorLayer要素时,不会添加属性。在

我想添加一个新的QgsFeature(多边形)到已经存在的QgsVectorLayer。特性被添加到画布(显示在屏幕上),但层的相应属性表没有更新(新创建的特性的属性不会被添加)

我读过PyQGIS的烹饪书和相关的StackOverflow问题,但是代码不起作用,我不知道我遗漏了什么。在

layer = self.projectInstance.mapLayersByName('draba_obcina')[0]
pr = layer.dataProvider() 
points = [QgsPointXY(0, 0), QgsPointXY(0, 1), QgsPointXY(1, 0), QgsPointXY(0, 0)]

poly = QgsFeature(layer.fields())
#if i set OGC_FID i get SQLite error: UNIQUE constraint failed (although there is no entry with OGC_FID = 3640)
#poly.setAttribute ("OGC_FID", 3640)
poly.setAttribute ("tip_spr", None)
poly.setAttribute ("id", None)
poly.setAttribute ("sif_upr", None)
poly.setAttribute ("id_upr", None)
poly.setAttribute ("vrsta_dr", None)
poly.setAttribute ("vrsta_pov", None)
poly.setAttribute ("nac_dol", None)
poly.setAttribute ("nat_dol", None)
poly.setAttribute ("usklajenost_zk", None)
poly.setAttribute ("graf_pov", None)
poly.setAttribute ("d_spr", None)
poly.setAttribute ("d_vir", None)
poly.setAttribute ("vrsta_el", None)
poly.setAttribute ("opis", "I was created by plugin")
poly.setAttribute ("createdtime", "2019-06-25 18:51:34")
poly.setAttribute ("kat", "10")

poly.setGeometry(QgsGeometry.fromPolygonXY([points]))

res, outFeats = pr.addFeatures([poly])

layer.updateExtents()
layer.commitChanges()
layer.reload() 

在pr.addFeatures属性([poly])返回(res==True)并且(outFeats length为1) 对我来说,这意味着添加功能是成功的。在

不会抛出错误。在

但是新特性只显示在画布上,没有属性添加到属性表中。在


Tags: ogcnonelayer属性画布特性prpoints