3

I have some models for various routines but I really want them to run on a specific layer. I don't want to be able to run the model with any input layer as if done so by accident it can mean crashing out of QGIS due to the issue of having my model run on too big a dataset.

I would just like the model to only run if a specific named layer is selected. For example if the layer I want to use as my input is "map" then I don't want the model to run if any other input layer is chosen.

I have no python knowledge but I'm sure something could be done.

3

I think you will need to use a custom script which checks the name of the input layer. If the script allows that name then the rest of the model can be executed otherwise it will produce an error message.

You can create a script from:

Processing Toolbox > Scripts > Tools > Create new script

Then use something like the following:

##Example=name
##input_layer=vector
##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:
    output=input_layer
else:
    iface.messageBar().pushWidget(iface.messageBar().createMessage( u'Layer not allowed' ), QgsMessageBar.WARNING, 3)

Make sure the script is saved in your /.qgis2/processing/scripts/ directory. Then in your modeler, include the script immediately after your vector input and set its input parameter to be from the vector input. Add the first function to take the output from the script as its input.

For example:

Modeler

Now when you run your model, if the selected layer is not mentioned in the script, it will fail to run:

Failed modeler

  • Thank you Joseph that works perfectly. :) I would also like a script that only allows you to run a model if a feature is selected in the input layer if at all possible? – webg Jun 10 '17 at 15:21
  • @webg - I noticed you asked that as a new question and the answer provided should do what you asked if you insert the Save selected features tool after the script :) – Joseph Jun 12 '17 at 10:00
  • Thanks but adding the Save Selected Features doesn't prevent the model from running if there are no selected features. I need the model to return an error if there are no selected features in the input layer. – webg Jun 13 '17 at 6:06
  • @webg - Posted a possible method in your new question which contains another if statement in the script :) – Joseph Jun 13 '17 at 9:37

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.