# 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)+'"')
Solved! Go to Solution.
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()
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.
# 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()
You need to print out the actual value of the variable the tool is reading to make sure it is a valid query.
input = "\"CITY_NAME\" = 'Chicago'"
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
# 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 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)
selection = int(arcpy.GetCount_management("layer").getOutput(0)) arcpy.AddMessage("%s features selected" % (selection)) arcpy.RefreshActiveView()