how do i use values from a multiple value parameter into a function

3821
7
12-27-2017 11:09 AM
stashavint
New Contributor II

I want to allow the user to choose several value from a list of values then later i want to use these values to iterate through a raster data set. But very time i keep getting errors saying the data set is not correct. My code is here:

import arcpy,os
inputraster = arcpy.GetParameterAsText(0)
sea_level = arcpy.GetParameterAsText(1)
fc = arcpy.GetParameterAsText(2)
clipraster = arcpy.GetParameterAsText
valuelist = [x.strip() for x in sea_level.split(";")]
for value in valuelist:
        arcpy.Clip_management(inputraster,"#",clipraster,fc,"#","ClippingGeometry","MAINTAIN_EXTENT")
        clip_raster = arcpy.Raster(clipraster)
        floodedarea = os.path.join(outputfolder + "floods" + str(value))
        areasbelow = clip_raster <= value
        areasbelow.save(floodedarea)
        arcpy.AddMessage(areasbelow)‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

a multivalue parameter is often returned as a semi-colon delimited string as you have shown, however, I suspect you are only getting layer names and not paths to files which may be what is needed.

Perhaps you could throw in a few print/arcpy.AddMessage lines in so that you get some output from the tool to examine

XanderBakker
Esri Esteemed Contributor

Your value is a string. You will need to convert it to make it numerical by using int or float.

DanPatterson_Retired
MVP Emeritus

another argument for using sys.argv instead...

stashavint
New Contributor II

which variable do i change to int or float is it the valuelist or the value? and I am confused on how to do that

0 Kudos
XanderBakker
Esri Esteemed Contributor

In case the values are integers you can do this:

valuelist = [int(x.strip()) for x in sea_level.split(";")]
XanderBakker
Esri Esteemed Contributor

Remember when you obtain a parameter with GetParameterAsText you get it as text, when you use GetParameter you will directly have a list of numeric values. To explain this a little more, please see the example below:

In my tool I collect a list of values:

The script below contains this:

def main():
    import arcpy

    sea_level = arcpy.GetParameterAsText(0)

    arcpy.AddMessage("\nGetParameterAsText(0)")
    arcpy.AddMessage("sea_level={0}".format(sea_level))
    arcpy.AddMessage("type={0}".format(type(sea_level)))

    lst_sea_level = sea_level.split(";")
    arcpy.AddMessage("lst_sea_level={0}".format(lst_sea_level))
    arcpy.AddMessage("type of first element={0}".format(type(lst_sea_level[0])))

    lst_values = [int(v) for v in lst_sea_level]
    arcpy.AddMessage("lst_values={0}".format(lst_values))
    arcpy.AddMessage("type of first element={0}".format(type(lst_values[0])))


    lst_sea_level = arcpy.GetParameter(0)

    arcpy.AddMessage("\nGetParameter(0)")
    arcpy.AddMessage("lst_sea_level={0}".format(lst_sea_level))
    arcpy.AddMessage("type of first element={0}".format(type(lst_sea_level[0])))

if __name__ == '__main__':
    main()

In the first part it reads the parameter as text and you can see how it is processed to get a list of values. In the second part starting on line 19, I read the same parameter but now directly as a list without converting it to string. The result is this:

In your case it will be much easier to use GetParameter then using GetParameterAsText.

stashavint
New Contributor II

Thanks let me put in my codes and i will tell you the output

0 Kudos