Select to view content in your preferred language

Select from user input variable

2924
25
Jump to solution
09-10-2012 08:27 AM
NoahHuntington
Deactivated User
Please help.  Unable to make selection.

Here is what I have....

# Model selects parcel from user input import arcpy  # Assign variable to the feature class featureClass = "G:\\PRAD\\PRAD_v10.1.gdb\\tax_acct"  # Assign variable to users input input = arcpy.GetParameterAsText(0)  # Make a feature layer from feature class arcpy.MakeFeatureLayer_management(featureClass, "layer")       # Apply a selection to the feature layer  arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", '"'+str(input)+'"') 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
NoahHuntington
Deactivated User
Well first of all you don't have feature classes in your TOC, you have layers pointing to your feature classes on disk. These are the objects you make selections on, you cannot make selections on feature classes.

Try running this as your script. You could also add another input parameter to select the layer manually.
import arcpy  def main():     arcpy.AddMessage("Starting")      # Get and set variables     job_num = arcpy.GetParameterAsText(0)     layer_name = "tax_acct"     df_name = "Layers"      arcpy.AddMessage(job_num)      # Define mxd, df and lyr objects     mxd = arcpy.mapping.MapDocument("CURRENT")     df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]     lyr = arcpy.mapping.ListLayers(mxd, layer_name, df)[0]      arcpy.AddMessage(lyr.name)      # Create where clause for selection     query = "Job_Num = '%s'" % (job_num)      arcpy.AddMessage(query)      arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)      # Get number of selected features     selection = int(arcpy.GetCount_management(layer_name).getOutput(0))      arcpy.AddMessage("%s features selected" % (selection))      # Set df extent to layer selection     df.extent = lyr.getSelectedExtent()     arcpy.RefreshActiveView()      arcpy.AddMessage("Completed")  main()



That was perfect!!!  Your fast too!  Just a couple questions?  What was going on with the addmessages methods? Also some of these features have 1, 2, or 3 possible job numbers associated with them, as in fields Job_Num2, Job_Num3, so on.... Would there be a way add multiple fields to the query?  Job_Num, or Job_Num2, or Job_Num3..

View solution in original post

0 Kudos
25 Replies
MathewCoyle
Honored Contributor
The last line shouldn't be indented. Are you getting a syntax error or some other error? Also, what is the value of your input variable? Try using AddMessage to see if it is passing how you would expect it to.
0 Kudos
NoahHuntington
Deactivated User
The last line shouldn't be indented. Are you getting a syntax error or some other error? Also, what is the value of your input variable? Try using AddMessage to see if it is passing how you would expect it to.


The input is a 6 or 7 digit text field found in the feature class.  The script is run as a tool where the input parameter is set to string.  Here is what I currently have...

# Selects from user input in tool dialog.  

import arcpy

# Get the layer.
featureClass = "G:\\PRAD\\PRAD_v10.1.gdb\\tax_acct"
input = arcpy.GetParameterAsText(0)

try:
    # Make a feature layer from feature class
    arcpy.MakeFeatureLayer_management(featureClass, "layer")
    
    # Apply a selection to the feature layer
    arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", '"'+str(input)+'"')

except:
    print arcpy.GetMessages()
   
0 Kudos
MathewCoyle
Honored Contributor
You need to print out the actual value of the variable the tool is reading to make sure it is a valid query.
0 Kudos
NoahHuntington
Deactivated User
You need to print out the actual value of the variable the tool is reading to make sure it is a valid query.


Sorry I must be slow.  I am not sure I am following you.  Can I see an example?
0 Kudos
MathewCoyle
Honored Contributor
Depending on your data type it could look something like this.
input = "\"CITY_NAME\" = 'Chicago'"

You can see more examples here.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000
0 Kudos
NoahHuntington
Deactivated User
Depending on your data type it could look something like this.
input = "\"CITY_NAME\" = 'Chicago'"

You can see more examples here.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000


Right. I can see now that something is missing.  What does the syntax look like for whereclause when field name is Job_Num and variable is input.  Similar to...

..."Job_Num = " + str(input))
0 Kudos
MathewCoyle
Honored Contributor
This should work. You may need to add quotes around your field name, or use the add field delimiters tool to create them for dynamic data sources.

# Model selects parcel from user input
import arcpy

# Assign variable to the feature class
featureClass = "G:\\PRAD\\PRAD_v10.1.gdb\\tax_acct"

# Assign variable to users input
input = arcpy.GetParameterAsText(0)

# Make a feature layer from feature class
arcpy.MakeFeatureLayer_management(featureClass, "layer")

# Build selection query
query = "Job_Num = '%s'" % (input)  # May need change to "\"Job_Num\" = '%s'"

# Apply a selection to the feature layer
arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", query)
0 Kudos
NoahHuntington
Deactivated User
This should work. You may need to add quotes around your field name, or use the add field delimiters tool to create them for dynamic data sources.

# Model selects parcel from user input
import arcpy

# Assign variable to the feature class
featureClass = "G:\\PRAD\\PRAD_v10.1.gdb\\tax_acct"

# Assign variable to users input
input = arcpy.GetParameterAsText(0)

# Make a feature layer from feature class
arcpy.MakeFeatureLayer_management(featureClass, "layer")

# Build selection query
query = "Job_Num = '%s'" % (input)  # May need change to "\"Job_Num\" = '%s'"

# Apply a selection to the feature layer
arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", query)


This runs without error but nothing gets selected?  I have tested with attributes in the Job_Num field that I have checked to exist. What do you think?  Thanks again.
0 Kudos
MathewCoyle
Honored Contributor
Try adding these lines at the end of your script. This will print how many features are selected, and then refresh the view to make those selections visible.

selection = int(arcpy.GetCount_management("layer").getOutput(0))
arcpy.AddMessage("%s features selected" % (selection))
arcpy.RefreshActiveView()
0 Kudos