4

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?

  • 1
    May you post an example of formula? – mgri Feb 22 '17 at 14:15
  • It does't not matter in this context, since my issue is about selection from any set of strings, but as an input for field calculator, a formula might be something like if("field2" is null, "field1", "field1"||' '"field2") – Jochen Schwarze Feb 22 '17 at 19:01
5

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

  • 1
    It'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 Feb 22 '17 at 19:52
3

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:

enter image description here

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:

enter image description here

In the resulting gui, I can now select from the descriptions of my formulae (doing so, variety of different formula collections would be thinkable):

enter image description here

  • Now that is a very, very clever workaround. Kudos to you! I'm now curious as to what your secret formula is :) – Joseph Feb 23 '17 at 9:55

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.