import sys, arcpy
arcpy.env.workspace = "C:\Scratch"
Environment_Index = arcpy.GetParameterAsText(0)
Measure = arcpy.GetParameterAsText(1)
if Environment_Index == "Percentage of Environmental Hazard":
if Measure == "0-25%":
Expression = '"per_Envi_H" >0 AND "per_Envi_H" <25'
elif Measure == "25-50%":
Expression = '"per_Envi_H" >25 AND "per_Envi_H" <50'
elif Measure == "50-75%":
Expression = '"per_Envi_H" >50 AND "per_Envi_H" <75'
elif Measure == "75-100%":
Expression = '"per_Envi_H" >75 AND "per_Envi_H" <100'
else:
Expression = "0"
print Expression
arcpy.SetParameter(2, Expression)
elif Environment_Index == "Percentage of Flooded Area":
if Measure == "0-25%":
Expression = '"Per_Flood" > 0 AND "Per_Flood" <25'
elif Measure == "25-50%":
Expression = '"Per_Flood" > 25 AND "Per_Flood" <50'
elif Measure == "50-75%":
Expression = '"Per_Flood" > 50 AND "Per_Flood" <75'
elif Measure == "75-100%":
Expression = '"Per_Flood" > 75 AND "Per_Flood" <100'
else:
Expression = "0"
print Expression
arcpy.SetParameter(2, Expression)
else:
if Measure == "0-50kgs/yr":
Expression = '"pest_kg" >0 AND "pest_kg" <50'
elif Measure == "50-100kgs/yr":
Expression = '"pest_kg" >50 AND "pest_kg" <100'
elif Measure == "100-250kgs/yr":
Expression = '"pest_kg" >100 AND "pest_kg" <250'
elif Measure == "250-500kgs/yr":
Expression = '"pest_kg" >250 AND "pest_kg" <500'
else:
Expression = "0"
print Expression
arcpy.SetParameter(2, Expression)
I am trying to get a string as an output parameter from the script tool and use the string as the expression for the select by attribute tool.
My user wanted to have a range of values to be selected from a field, I thought it was an easy process by writing a python code.
I tried using the set parameter for the output parameter. I don't understand where the problem is. This is my script tool in the model builder enter image description here
1 Answer 1
I guess double-quotes are making trouble. When you use Query Builder, you see that the field names are added without double quotes.
Use your code like that:
import arcpy, sys
arcpy.env.workspace = "C:/Scratch"
environment_index = arcpy.GetParameterAsText(0)
measure= arcpy.GetParameterAsText(1)
if environment_index == "Percentage of Environmental Hazard":
if measure== "0-25%":
expression = 'per_Envi_H > 0 AND per_Envi_H < 25'
elif measure== "25-50%":
expression = 'per_Envi_H > 25 AND per_Envi_H < 50'
elif measure== "50-75%":
expression = 'per_Envi_H > 50 AND per_Envi_H < 75'
elif measure == "75-100%":
expression = 'per_Envi_H > 75 AND per_Envi_H < 100'
else:
expression = "0"
elif environment_index == "Percentage of Flooded Area":
if measure == "0-25%":
expression = 'Per_Flood > 0 AND Per_Flood < 25'
elif measure == "25-50%":
expression = 'Per_Flood > 25 AND Per_Flood < 50'
elif measure == "50-75%":
expression = 'Per_Flood > 50 AND Per_Flood < 75'
elif measure == "75-100%":
expression = 'Per_Flood > 75 AND Per_Flood < 100'
else:
expression = "0"
else:
if measure == "0-50kgs/yr":
expression = 'pest_kg > 0 AND pest_kg < 50'
elif measure == "50-100kgs/yr":
expression = 'pest_kg > 50 AND pest_kg < 100'
elif measure == "100-250kgs/yr":
expression = 'pest_kg > 100 AND pest_kg < 250'
elif measure == "250-500kgs/yr":
expression = 'pest_kg > 250 AND pest_kg < 500'
else:
expression = "0"
arcpy.SetParameter(2, expression)
if
statement doesn't define a scope in Python. So, you don't have to use arcpy.SetParameter()
in every if
branch.
Explore related questions
See similar questions with these tags.
{}
button instead of attaching a picture? It makes it easier to review and test the code.