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
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?
2 Answers 2
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'
:
Now when you run your model, it should look something like the following where you can select the formula to be used:
-
1It's far for me criticizing your awesome workaround, but this means adding a boolean to the model for each new formula... On the other hand, this was really inspiring to me, and pointed me to another one (see below)Jochen Schwarze– Jochen Schwarze2017年02月22日 19:52:09 +00:00Commented Feb 22, 2017 at 19:52
Another workaround just came out of my brain, since in models we can select from table fields. So my workaroud goes like this:
I put my formula collection in a nice spreadsheet, with i.e. a description of what the formulae do as headers:
This I add to my project.
Since I cannot pass table field values as input to field calculator
, I have a tiny custom script, that makes strings of table field values:
##input=table
##a=field input
##text=output string
formeln=processing.getObject(input)
for fo in formeln.getFeatures():
text = fo[a]
In my model, I have as input parameters a table
, a table field
and a vector
, passing table
and table field
to the custom script, and passing its output to the field calculator
:
In the resulting gui, I can now select from the descriptions of my formulae (doing so, variety of different formula collections would be thinkable):
-
Now that is a very, very clever workaround. Kudos to you! I'm now curious as to what your secret formula is :)Joseph– Joseph2017年02月23日 09:55:12 +00:00Commented Feb 23, 2017 at 9:55
Explore related questions
See similar questions with these tags.
if("field2" is null, "field1", "field1"||' '"field2")