Wednesday, 8 July 2015

Selection input for QGIS Processing models


QGIS 2.14.11 LTR. In a processing script we can define a selection as input parameter by ##auswahl_test=selection spam;egg;dummy resulting in


enter image description here


Background is, that I want to provide a selection of formulas (strings) as input for field calculator algorithm used in a model. By now, I'm typing these formulas in a string input field by hand, which is annoying, time-consuming and error-prone.


Now is there a way to obtain such input field in the auto-generated gui of a processing model? Or any other suggestions of how to provide a selection of strings as a model input?



Answer



This is more of a workaround as I'm not sure if it's possible at the moment to allow a user to select from a range of string values. Instead, you could use booleans which would represent your formulae (these would be connected to a custom script which will contain your formulae). So that when a user clicks on a boolean from the modeler, the script will output the relevant formula to the field calculator.





Here is an example custom script where the inputs are three booleans and depending on which is selected, the output string will contain the formula. The formulae are very simple, multiplying values from the id field with an integer:


##Example=name
##formula_1=boolean
##formula_2=boolean
##formula_3=boolean
##selected_formula=output string

if formula_1 == True:
selected_formula=""" "id" * 2 """

if formula_2 == True:
selected_formula=""" "id" * 3 """
if formula_3 == True:
selected_formula=""" "id" * 4 """
else:
pass

Now in your modeler, create the three booleans (I would leave them unchecked as default). Then add your custom script linking the booleans. Then add your field calculator, and for your Formula parameter, choose the 'selected formula' from algorithm 'Example':


Example modeler


Now when you run your model, it should look something like the following where you can select the formula to be used:



Running model


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...