In QGIS geoprocessing scripts, default values must be provided for input numbers and strings, according to http://docs.qgis.org/2.8/en/docs/user_manual/processing/console.html#creating-scripts-and-running-them-from-the-toolbox like:

##mynumer=number 3.1415329

Since I often have to process layers with all the same field names (unfortunately not assurable in any case), I would like to pass a default value for a field name to the geoprocessing script, following a logic like 'take the default field name if it exists in the layer referred to, otherwise let the user select one'.

Is this possible somehow, perhaps with a bit of python?

1 Answer 1


Not sure how to modify the interface of such scripts but you could use an example script like the following where the Enter_field parameter has a default string of ID and if a field name matches this, it prints this field name otherwise it will print the one that is selected in the Or_select_field parameter:

##Enter_field_name=string ID
##Or_select_field=field Input_layer

layer = processing.getObject(Input_layer) 
field_names = [ field.name() for field in layer.pendingFields() ]

if Enter_field_name in field_names:
    print Enter_field_name
    print Or_select_field

You would replace the print statements and use your code for both if, else statements while changing the Enter_field and Or_select_field parameters accordingly.

Side note:

If that number is supposed to be pi, should it be 3.1415926? :)

  • 1
    Funny coincedence... I've accidentally typing something only ~6*10**-5 away from pi!!! Jun 29, 2016 at 11:22
  • But to be serious: Besides blowing up the gui when having some more fields, very helpful bit of python! Jun 29, 2016 at 11:36
  • @JochenSchwarze - Glad it was helpful! Yes, the GUI can get a bit messy if you use a lot of parameters. Always wondered if the GUI can be modified within the script itself... =)
    – Joseph
    Jun 29, 2016 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.