Is there a method I can find out, how many features in a layer have changed during edit mode (including changes in the attribute and the geometry)?

up vote 2 down vote accepted

If you want to count the number of Features that are in the buffer (i.e that have changed since last commit on the layer) you can use the editBuffer of your QgsVectorLayer. It's a QgsVectorLayerEditBuffer object which has interesting methods (changedAttributeValues(), changedGeometries(), addedAttributes(), addedFeatures(), ...)

# get a layer
layer = iface.activeLayer()

# get its editBuffer
buff = layer.editBuffer()

# make some changes in the layer...
  # changed geometries
buff.changedGeometries()
--> {61L: <qgis._core.QgsGeometry object at 0x7f8ed555e808>}

  # changed Attribute Value
buff.changedAttributeValues()

  # Number of changedFeatures regarding geometry or attributes
len(buff.changedGeometries()) + len(buff.changedAttributeValues())

Be aware that if you count the features that have changed like that, you will count two times the same feature if you change it's geometry and one (or more) of it's attribute(s).

If you want the count of distinct features that have changed you can do something like that:

changed_geom =  buff.changedGeometries().keys() #list of feature ID
changed_attributes = buff.changedAttributeValues().keys() #list of feature ID
distinct_changed_features = set(changed_geom + changed_attributes) # set of distinct feature ID
number_changed_features = len(distinct_changed_features)

Now, if you want the count features that have change during an "Edit session" on a layer, you can connect to editingStarted() and editingStopped signals of QgsVectorLayer to implements a counter of modified features.

  • Thanks! That was exactly what I was looking for. – Senwei 2 days ago
  • @Senwei Glad to help! – YoLecomte 2 days ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.