In QGIS 3.10, I have layers in which I select features by location (to find duplicates), using :
selected_features = processing.run('qgis:selectbylocation', {'INPUT':layer1, 'PREDICATE':0, 'INTERSECT':layer2, 'METHOD':0})
'METHOD':0
means I begin a new selection with the selected features
Then, in this selection, I want to select features by expression, using :
subselected_features = processing.run('qgis:selectbyexpression', {'INPUT':layer1, 'EXPRESSION':expression, 'METHOD':3})
'METHOD':3
means I select features in the current selection
Now, I want to delete all the subselected features. I know how to delete features by expression, with the method explained here (Deleting selected features using PyQGIS?) :
with edit(layer1):
# 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 layer1.getFeatures(request):
layer1.deleteFeature(f.id())
But this way selects features in the entire layer1
. It does not allow to choose a METHOD as with processing, to make a subselection in the current selection.
Does someone has an idea to delete these subselected features ?
edit(layer)
intoedit(layer1)
. Then, I think, it will work.