I'm trying to program a simple Select by Attributes Python script tool. I want to be able to find a specific building using a field and zoom in to the location. Also, my tool needs to allow the user to input any field or any value. So far this is what I got.
import arcpy
#Set to current mxd and dataframe
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
# Set overwrite option
arcpy.env.overwriteOutput = True
arcpy.env.workspace ="C:\Users\pierrej\Desktop\GIS Data"
try:
# Get the input parameters for the Selection Tool
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(FClass,"FclassLayer")
where_clause = """{} = {}""".format(arcpy.AddFieldDelimiters("FclassLayer", Field),Feature)
arcpy.AddMessage(where_clause)
# Select the site
arcpy.SelectLayerByAttribute_management("FclassLayer","NEW_SELECTION", where_clause)
# write selected features to a new featureclass
arcpy.CopyFeatures_management("FclassLayer", "SelectionSites")
#Zooming to a selection set for the specific layer
df.zoomToSelectedFeatures()
df.scale = 2500000
arcpy.RefreshActiveView()
# Report a success message
arcpy.AddMessage("All done!")
except:
# Report an error messages
arcpy.AddError("Could not complete")
# Report any error messages that the tool might have generated
arcpy.AddMessage(arcpy.GetMessages())
My parameters are the following:
InputFeatureClass = Data Type: Feature Layer, Required, Input, No Multivalue
InputField = Data Type: Field, Required, Input, No Multivalue, Obtained from Input Feature Class
InputValue = DataType: String, Required, No Multivalue
The script runs and I don't have any error message but the tool doesn't select any thing. How can I fix this?
Jonathan....
from Select Layer By Attribute—Data Management toolbox | ArcGIS Desktop
FclassLayer should be a variable not a string … dump the quotes
arcpy.SelectLayerByAttribute_management(FclassLayer,"NEW_SELECTION", where_clause)
I believe you can reference feature layers as strings - they created a feature layer using MakeFeatureLayer. To use as variable, you'd need to set the variable to the output of MakeFeatureLayer.
Jonathan, can you print the where clause, an example of 'Feature', and show the attribute table with a feature that should match the query. The query is likely just not matching anything.
Hello Darren, 'Feature' would be the FID #, or the province name for example.
The user enters his feature class layer, specifies the field and enters the field value for the feature we're looking for.
# Get the input parameters for the Selection Tool
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)
# Make a layer from the feature class
FclassLayer = arcpy.MakeFeatureLayer_management(FClass,"FclassLayer")
where_clause = """{} = {}""".format(arcpy.AddFieldDelimiters(FclassLayer, Field),Feature)
arcpy.AddMessage(where_clause)
# Select the site
arcpy.SelectLayerByAttribute_management(FclassLayer,"NEW_SELECTION", where_clause)
# write selected features to a new featureclass
arcpy.CopyFeatures_management(FclassLayer, "SelectionSites")
I attributed the layer to a variable called FclassLayer to get rid of the quotation marks. The script still ran but didn't select anything.
I would suggest to Add feature class to Map (Table of content), which will be added as feature layer. than use that layer as input for your tool and skip the following line of code. it works fine.
FclassLayer = arcpy.MakeFeatureLayer_management(FClass,"FclassLayer")
Please review following code.
import arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
arcpy.env.overwriteOutput = True
arcpy.env.workspace ="D:/00-Awork/01-GIS Work/01-ArcGIS/"
wrk=arcpy.env.workspace
arcpy.AddMessage("Workspace added")
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)
where_clause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(FClass, Field),Feature) # I have added extra pair of single quotes for selecting strings
arcpy.AddMessage(where_clause)
arcpy.SelectLayerByAttribute_management(FClass,"NEW_SELECTION", where_clause)
arcpy.CopyFeatures_management(FClass, "SelectionSites")
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
arcpy.AddMessage("All done!")
The extra pair of single quotes gave me a SQL syntax error, so I removed them. This is the code that ended up working but only for numeric fields:
try:
# Get the input parameters for the Selection Tool
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)
where_clause = """{} = {}""".format(arcpy.AddFieldDelimiters(FClass, Field),Feature) # I have added extra pair of single quotes for selecting strings
arcpy.AddMessage(where_clause)
# Select the site
arcpy.SelectLayerByAttribute_management(FClass,"NEW_SELECTION", where_clause)
# write selected features to a new featureclass
arcpy.CopyFeatures_management(FClass, "SelectionSites")
#Zooming to a selection set for the specific layer
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
# Report a success message
arcpy.AddMessage("All done!")
However, if I try the tool using a String type field I get an error message. (Invalid expression, and invalid SQL statement was used)