1

I'm trying to select multiple features at once to do batch editing. The first selection is supposed to be the expression within the request, but I can't seem to get anything to select. I want the selection to be deleted.

##layer=vector
##zone_index=field layer
##subty_index=field layer
##sfha_index=field layer

from qgis.core import *import qgis.utils
from PyQt4.QtCore import *

layer=processing.getObject(layer)

layer.startEditing()

request = QgsExpression(u"\"zone_index\"='AREA NOT INCLUDED' OR \"zone_index\"='OPEN WATER' OR (\"sfha_index\" = 'F' AND \"zone_index\" = 'D')")

it= layer.getFeatures( QgsFeatureRequest( request))

ids = [i.id() for i in it]
print ids

layer.setSelectedFeatures(ids)


layer.commitChanges()
3

You was on the right way, but you forgave using a provider for deleting the features (you don't need to set a selection before deleting the features). Furthermore, I edited just a bit your code when creating the request.

I think that this code should work (it worked for me using another request expression):

##layer=vector
##fld_zone=field layer
##zone_subty=field layer
##sfha=field layer

from qgis.core import * 

layer=processing.getObject(layer)

prov = layer.dataProvider() # Define the provider

request = '"%s"=\'AREA NOT INCLUDED\' OR "%s"=\'OPEN WATER\' OR ("%s"=\'F\' AND "%s"=\'D\')' % (zone_index, zone_index, sfha_index, zone_index)
it= layer.getFeatures(QgsFeatureRequest().setFilterExpression(request))

prov.deleteFeatures([i.id() for i in it]) # Delete the selected features
  • I wasn't able to get this to work. – monkeyon Jan 26 '17 at 21:00
  • Does it show some error or simply nothing happens to your layer? – mgri Jan 26 '17 at 21:10
  • Nothing happens to the layer when I run it. – monkeyon Jan 26 '17 at 21:14
  • Ok, this means that there is some error in the string where you set the request. However, I edited the request in the code: it only depends from it, but I can't help you for this because I don't know how your data are structured (I hope you will understand how the syntax for a request should be to best fit it to your case). Let me know if it works by now! – mgri Jan 26 '17 at 21:31
  • Well, I'm glad it worked also for you ;) – mgri Jan 26 '17 at 21:48

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.