2

I am using QGIS 3.10.2-A Coruña and its Python Console. Currently I am modifying features and attributes of a shape file, e.g. dropping features:

caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.DeleteFeatures:
    res = layer.dataProvider().deleteFeatures(selId)

where selId are the features to be dropped.

I noticed that when I do such operations changes are immediately saved to the file that is stored on my hard disk, which sort of eliminates the advantage of using a script in the first place.

What do I need to do to get around changes being saved automatically? I read about "editing buffers" in the QGIS Developer Cookbook but I am unsure whether that's what I need.

EDIT:

Just noticed that the layer is only saved on the hard disk (overwrites the current file) if executing the code results in actual changes in the layer. Running the code below for a second time doesn't delete any additional features bc selId doesn't change and the layer is NOT saved again. The same applies to the code proposed in the answer below. It seems like the automatic saving isn't triggered by the code itself but maybe rather by some general preference setting according which any changes to a layer are automatically saved to the hard disk? Does sth like that exist?

2 Answers 2

2

I think if you don't use dataProvider it's ok. Found in Deleting selected features using PyQGIS?:

with edit(layer):
    # build a request to filter the features based on an attribute
    request = QgsFeatureRequest().setFilterExpression('"DN" != 3')

    # we don't need attributes or geometry, skip them to minimize overhead.
    # these lines are not strictly required but improve performance
    request.setSubsetOfAttributes([])
    request.setFlags(QgsFeatureRequest.NoGeometry)

    # loop over the features and delete
    for f in layer.getFeatures(request):
        layer.deleteFeature(f.id())
2
  • Unfortunately, changes are still saved immediately.
    – L B
    Commented Mar 20, 2020 at 9:44
  • Are you sure your underlying file is not updated at the end of the "edit" session ? Can you try with 'layer.startEditing()' and 'layer.commitChanges()' instead of 'with edit(layer)'?
    – etrimaille
    Commented Mar 20, 2020 at 10:03
0

dataProvider will commit changes to the end dataset immediately, therefore there is no ability to rollback or undo changes.

using the command 'with edit(layer):' will also function the same way as the dataProvider. Changes are commit immediately.

The only way is to manually capture your captures with a startEditing(), commitChanges() and stopEditing() calls. This is what the cookbook is referring to as 'editing buffers'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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