0

I want to convert a script into a toolbox and one of the parameters is selecting cities with a population > than 500000 hab. I'm using the function MakeFeatureLayer_management.

How can I do to introduce the value of the {where_clause} ('"POP_MAX" > 500000') using arcpy.GetParameterAsText?

import arcpy    
from arcpy import env    
env.overwriteOutput = True

wsp = env.workspace = arcpy.GetParameterAsText(0)    
wsp_result = arcpy.GetParameterAsText(1)

try:    
  arcpy.MakeFeatureLayer_management ("cities" + "layer_cities",'"POP_MAX" > 500000')

1 Answer 1

5

You should create a new parameter of the SQL Expression data type and then set the Obtained from option to point to the feature class parameter. This will let user see all the attributes of the feature class he/she has chosen (much like Select By Attribute dialog window).

enter image description here

If you don't want to expose the feature classes's attributes, then just create a String parameter that will contain the SQL query (user will enter the query POP_MAX" > 500000 and this implies that they know the names of the fields) or a Long parameter (integer) to specify the value only and then you can use the value in the query in your code:

userThreshold = arcpy.GetParameterAsText(2)
query = """ "POP_MAX" > {0} """.format(userThreshold)

If you don't know which data source your users will be working with, it might be good to use the AddFieldDelimiters function which will let you use correct syntax for specifying the field name.

sql_exp = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters('c:/data', field_name),state_value)

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.