Would it be possible to run a script in a QGIS model so that it only runs on selected features from an input layer? I don't want to use the option in the toolbox options as I have other models that require to run without selections.

I just a model to only run on selected features in the input layer and return an error if no feature is selected. I'm thinking a script added immediately after the input in the model?

2 Answers 2


Adding Save selected features should do the trick:

enter image description here

  • Thanks but adding Save Selected Features doesn't prevent my model from running if there are no selected features in the input layer which I need it to do.
    – webg
    Commented Jun 13, 2017 at 6:12

I think the answer by @underdark ♦ is correct in that you could use the Save selected features algorithm. However, as you mentioned, if no features are selected then it saves all features.

Building on from your previous question, we could add another if statement to the script to check if any features are selected. If yes then run the mentioned algorithm, otherwise show an error:

##output=output vector

from qgis.utils import iface
from qgis.gui import QgsMessageBar

layer = processing.getObject(input_layer)
# Add your names into the list within single quotes
allowed_layers = ['map', 'layerName2', 'layerName3']

if layer.name() in allowed_layers:
    if layer.selectedFeatures():        
        processing.runalg('qgis:saveselectedfeatures', layer, output)
        iface.messageBar().pushWidget(iface.messageBar().createMessage( u'No features selected' ), QgsMessageBar.WARNING, 3)
    iface.messageBar().pushWidget(iface.messageBar().createMessage( u'Layer not allowed' ), QgsMessageBar.WARNING, 3)
  • Thank you but I need this to be separate from the other model. I've tried taking out parts of the script relating to the name but it won't run. I don't really know Python. I really want a model to run on any layer but only if a feature is selected and to return an error if no feature is selected. ;)
    – webg
    Commented Jun 14, 2017 at 17:12
  • 1
    Got it. I was getting an indent error but I've managed to sort it. Thanks for your help. ;)
    – webg
    Commented Jun 14, 2017 at 18:27
  • @webg - Most welcome, glad you got it to work :)
    – Joseph
    Commented Jun 15, 2017 at 9:14
  • After upgrading to 2.18.13 this script no longer seems to work. I get this.. Parameters: INPUT_LAYER "=C:\Users\Will\AppData\Local\Temp\processing8c9f09fbe2fd45f19ac2bbc41972cdc8\53509f25301645bcb6190158cdfe229c\output.shp Failed Error executing algorithm Save selected features There are no selected features in the input layer." If I remove the script the model works.
    – webg
    Commented Oct 13, 2017 at 14:34
  • @webg - Which QGIS version did you use beforehand? I would suggest asking a new question and include a screenshot of your model and the script you use (I just tested a simple model with this script on 2.18.13 and it works fine).
    – Joseph
    Commented Oct 13, 2017 at 14:44

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.