Select By Attribute, then Zoom Toolbox

499
3
Jump to solution
07-31-2019 09:38 AM
TaylorKravits
New Contributor II

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 (https://community.esri.com/thread/217697-select-layer-by-attribute-python-script-tool) 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
arcpy.AddMessage(where_clause)
# selects the attribute using the string the user input
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", where_clause)
# zooms to selected feature
df.zoomToSelectedFeatures()
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!

0 Kudos
1 Solution

Accepted Solutions
TaylorKravits
New Contributor II

Updated my code, the original question now contains a working product

View solution in original post

0 Kudos
3 Replies
JoelThompson1
New Contributor II

Without any error messages it's hard to say but I think there are few problems here. On line 15, FClassLayer is probably a result object but your feature layer is "FclassLayer". Then on line 20 you have FCLassLayer as the input to select. I'm surprised this isn't giving an error. On line 21 did you mean to copy the selected feature or the original input?

I didn't fully test this but I've been using something similar in my scripts. I modified your original code and tried to simplify some things:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
# unless these are necessary elsewhere
#df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#lyr = arcpy.mapping.ListLayers(mxd, "PARCELS", df)[0]

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/ArcMap/COGO_Template"

# unless used elsewhere
#wrk = arcpy.env.workspace
arcpy.AddMessage("Workspace added")
FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)

# what is FClassLayer? a result object?
#FClassLayer = arcpy.MakeFeatureLayer_management(FClass, "FclassLayer")
#arcpy.AddMessage(FClassLayer)

FClassLayer = "FclassLayer"

where_clause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(FClass, Field),
                                      Feature)  # I have added extra pair of single quotes for selecting strings
arcpy.AddMessage(where_clause)

# you can use the where clause when making the feature layer
#arcpy.SelectLayerByAttribute_management(FClassLayer, "NEW_SELECTION", where_clause)
arcpy.MakeFeatureLayer_management(FClass, FClassLayer, where_clause)
arcpy.CopyFeatures_management(FClassLayer, "C:/ArcMap/COGO.gdb/SelectionSites")

mxd.activeDataFrame.zoomToSelectedFeatures()
#df.zoomToSelectedFeatures()

arcpy.RefreshActiveView()
arcpy.AddMessage("All done!")
0 Kudos
JoelThompson1
New Contributor II

Actually the zoom to selected might not work the way I coded that, this might be better:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/ArcMap/COGO_Template"

FClass = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Feature = arcpy.GetParameterAsText(2)

FClassLayer = "FclassLayer"
arcpy.MakeFeatureLayer_management(FClass, FClassLayer)

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(FClassLayer, "NEW_SELECTION", where_clause)
arcpy.CopyFeatures_management(FClassLayer, "C:/ArcMap/COGO.gdb/SelectionSites")

mxd.activeDataFrame.zoomToSelectedFeatures()

arcpy.RefreshActiveView()
arcpy.AddMessage("All done!")
0 Kudos
TaylorKravits
New Contributor II

Updated my code, the original question now contains a working product

0 Kudos