0

This question already has an answer here:

I have been trying to create a tool to make a simple select then zoom to selected but I've been running into some issues. I've been using the code from this original posting (https://community.esri.com/thread/217697-select-layer-by-attribute-python-script-tool). It seems like his solution was never solved.

Here is the code that I have:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
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"

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

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(FClass, "C:/ArcMap/COGO.gdb/SelectionSites")
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
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

The tool runs fine and shows no errors but unfortunately no parcel is selected and it doesn't zoom. Something to note as well is it takes about 2 minutes to run.

When I manually select by attribute in the attribute table (select, select by attribute, put in the parcel number, click ok), then run the tool, it zooms to the layer

marked as duplicate by PolyGeo arcpy Jul 31 at 22:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Your script will get simplified by using a layer's definitionQuery property, as in the duplicate's answer. – PolyGeo Jul 31 at 22:02
  • Please do not edit your question to include an answer and give thanks; this breaks the Q/A model used by GIS SE. The Someone Answers page covers this – Vince Aug 2 at 16:45
  • How do I answer my own question though so I don't disrupt the model? – Taylor Kravits Aug 2 at 18:19
1

Flaws in your code are:

  1. You get a handle on a layer called "PARCELS" but then do nothing with it.
  2. You create a variable wrk and do nothing with it.
  3. Your Copy Features is being run on the FEATURECLASS not the FEATURELAYER which has the selection.
  • I've removed the first two and changed the CopyFeature to the FeatureLayer. This is a bit of an add-on question, but is there a way I can select an attribute without having to create a feature class? I've read a bit about using "in_memory", but am unsure. – Taylor Kravits Jul 31 at 21:49
  • @TaylorKravits "add-on questions" should be asked as new questions. – PolyGeo Jul 31 at 21:57
  • In_memory is a sort of temporary geodatabase so yes you could copy features to that location. – Hornbydd Jul 31 at 22:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.