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)?


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
--> {61L: <qgis._core.QgsGeometry object at 0x7f8ed555e808>}

  # changed Attribute Value

  # 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.

  • @Senwei Glad to help!
    – YoLecomte
    Dec 9 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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