In the script editor in QGIS, when choosing string parameter type, a default value must be added according to https://docs.qgis.org/2.8/en/docs/user_manual/processing/scripts.html

What happen is that the parameter line in the GUI will be populated with the default value. 'average' in my case.

enter image description here

What I want to do is to add several different values so that the user can choose from. In my case, I want to add other statistics, so the user will have a drop-down list containing several options.

The solution in Letting user choose from 2 options for QGIS Processing script input parameter? does not work for me since I'm using QGIS 2.18.2, also the solution for check-box is not relevant.

Is it possible to create a drop-down list for the user in QGIS 2.18.2 las palmas?

In case it is needed, the full code is here:

##Cell Statistics=name
##statistic_name=string average
##Output_raster=Output raster
import glob, os
from PyQt4.QtCore import QFileInfo
from qgis.core import QgsRasterLayer, QgsRectangle

raster_list = []
extent = QgsRectangle()

statistic_dic = {'average':0, 'count':1, 'median':2,'mode':3, 'minimum':4, 'min_raster':5,'maximum':6, 'max_raster':7, 'std':8, 'range':9}

for raster in glob.glob("*.tif"):
    fileInfo = QFileInfo(raster)
    baseName = fileInfo.baseName()
    rlayer = QgsRasterLayer(raster, baseName)
    # Combine raster layers to list
    raster_list .append(rlayer)
    # Combine raster extents

# Get extent    
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()
# Run algorithm and set relevant parameters
                {"input":raster_list ,
                "GRASS_REGION_PARAMETER":"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax), 
  • 1
    Your title says 2.8 but your body says 2.18.2, which is it?
    – PolyGeo
    Jan 19 '17 at 8:45
  • As @PolyGeo mentioned, could you please confirm which version it is. I am assuming it is 2.18 as this is not possible in 2.8. In your script, you can use the selection parameter which allows you to create a drop-down list containing your terms. So in your case, you can replace ##statistic_name=string average with ##statistic_name=selection average;count;median;mode;minimum;min_raster;maximum;max_raster;std;range.
    – Joseph
    Jan 19 '17 at 12:34
  • Thank Joseph, I am indeed using 2.18.2, I got it wrong since the document says 2.8.. (sorry,, I'm new to that..). The "selection" does work, I now realize that the "selection" parameter (in my case "statistic_name") gets an integer value and not a string value. Now it works.
    – user88484
    Jan 21 '17 at 8:35
  • @user88484 - Great stuff! I have voted to reopen your question as you are wanting a solution for QGIS 2.18 and not QGIS 2.8. You should post an answer on how you solved your problem :)
    – Joseph
    Jan 23 '17 at 12:49

To create the drop-list I used the "selection" option and separated the variables by ";". Then I used the correspond number as variable for the tool I was using (e.g., average is the first in the drop list so it's corresponded number is 0, count is the second so it's number is 1 ..).

##first_statistic_name=selection average;count;median;mode;minimum;min_raster;maximum;max_raster;stddev;range;sum;variance;diversity;slope;offset;detcoeff;tvalue

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.