Thursday, 21 July 2016

qgis - Ensure model runs from a specific layer?


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.



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...