添加新的 QgsFeature 时,QgsVectorLayer 的属性 Table 不会更新
QgsVectorLayer's Attribute Table doesn't get updated when adding a new QgsFeature
将新的 QgsFeature 添加到现有 QgsVectorLayer 特征时,不会添加属性。
我想向已有的 QgsVectorLayer 添加新的 QgsFeature(多边形)。特征被添加到 canvas(它显示在屏幕上)但是层的相应属性 Table 没有得到更新(新创建的特征的属性没有被添加)
我已阅读 PyQGIS Cookbook 和相关的 Whosebug 问题,但代码无法正常工作,我不知道我错过了什么。
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]) returns (res == True) and the (outFeats length is 1)
这对我来说意味着添加该功能是成功的。
没有抛出任何错误。
但新功能只显示在 canvas 上,没有添加属性到 Attribute Table。
解决了我的问题,代码工作正常,但问题是我必须 重新加载属性 Table UI 才能显示新属性它。
将新的 QgsFeature 添加到现有 QgsVectorLayer 特征时,不会添加属性。
我想向已有的 QgsVectorLayer 添加新的 QgsFeature(多边形)。特征被添加到 canvas(它显示在屏幕上)但是层的相应属性 Table 没有得到更新(新创建的特征的属性没有被添加)
我已阅读 PyQGIS Cookbook 和相关的 Whosebug 问题,但代码无法正常工作,我不知道我错过了什么。
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]) returns (res == True) and the (outFeats length is 1) 这对我来说意味着添加该功能是成功的。
没有抛出任何错误。
但新功能只显示在 canvas 上,没有添加属性到 Attribute Table。
解决了我的问题,代码工作正常,但问题是我必须 重新加载属性 Table UI 才能显示新属性它。