1
通过Python插件在QGIS中编辑属性的速度
我正在尝试使用QGIS Python插件编辑图层中每个要素的属性值。我发现在编辑模式之外执行此操作比在编辑时(甚至包括提交编辑)要慢得多。请参见下面的代码(在循环的同一点可互换的行)。我的样本数据集的速度差异为2秒(编辑模式)与72秒(非编辑模式)。 在编辑模式下修改属性: layer.changeAttributeValue(feature.id(), 17, QtCore.QVariant(value)) 在编辑模式之外修改属性: layer.dataProvider().changeAttributeValues({ feature.id() : { 17 : QtCore.QVariant(value) } }) 这是预期的行为吗?我不需要用户能够撤消更改,所以我认为我不需要使用编辑模式。 编辑1:请参阅下面的完整代码,包括两个版本(但已注释掉): def run(self): try: # create spatial index of buffered layer index = QgsSpatialIndex() self.layer_buffered.select() for feature in self.layer_buffered: index.insertFeature(feature) # enable editing #was_editing = self.layer_target.isEditable() #if was_editing is False: # self.layer_target.startEditing() # …