AnsweredAssumed Answered

Select By Attribute, then Zoom Toolbox

Question asked by taylor.kravits on Jul 31, 2019
Latest reply on Aug 2, 2019 by taylor.kravits

I have been trying to create a tool to make a simple select then zoom to selected. I used the code from this original posting ( to initially set this up. It seems like his solution was never solved but here is a working code! I added comments because as someone that is pretty new to Python/ArcPy, it helped me understand it quite a bit.


Here is the code that I have:

import arcpy

# uses the mxd that is running this code
mxd = arcpy.mapping.MapDocument("CURRENT")
# df is the dataframe, Layers is used to run through all the layers within the mxd. Leave Layers as is
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
# lyr sets the layer, needs to be spelt exactly as the layer sits in ArcMap
lyr = arcpy.mapping.ListLayers(mxd, "PARCELS.PARCELS", df)[0]
arcpy.env.overwriteOutput = True

# sets the parameters, this must be setup in the toolbox script
arcpy.AddMessage("Workspace added")
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)

# sets the Feature parameter equal to the user input string
where_clause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(FClass, Field),
                                      Feature)  # I have added extra pair of single quotes for selecting strings
# selects the attribute using the string the user input
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", where_clause)
# zooms to selected feature
arcpy.AddMessage("All done!")


My parameters are as follows:

InputFeatureClass = Data Type: Feature Class, Required, Input, No Multivalue, Default: Database Connections\Connection to gis.sde\PARCELS


InputField = Data Type: Field, Required, Input, No Multivalue, Obtained from Input Feature Class


InputValue = DataType: String, Required, No Multivalue


Thank you everyone for your help!