1
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
    Could you paste the code and format it with the {} button instead of attaching a picture? It makes it easier to review and test the code. – Marcelo Villa Apr 7 '18 at 15:19
  • My Expression in the model builder is empty and selecting all the values – Geeta Nakkina Apr 7 '18 at 16:03
  • What is the type of output that you specified in Parameters tab on Script Properties dialog? String? – Kadir Şahbaz Apr 7 '18 at 17:16
  • I specified it as a string – Geeta Nakkina Apr 8 '18 at 14:09
1

I guess double-quotes are making trouble. When you use Query Builder, you see that the field names are added without double quotes.

enter image description here

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.

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.