Select to view content in your preferred language

Python Tool - User input then Select by Attribute

5228
21
02-06-2017 01:22 PM
DevinUnderwood2
Regular Contributor

Any ideas why I am having problems by trying to have user input a string value for a feature, then have the feature selected. 

#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
#os.chdir(r'C:\Users\dunderwood\Documents\MY_TEMPLATES\WASTEWATER_EDIT_TEMPLATE')
# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

whereClause = FACILITYID == InputValue"
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereClause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

21 Replies
DanPatterson_Retired
MVP Emeritus

arcpy.AddMessage("Same as print when running tools")  # for print equivalent in arctoolbox

0 Kudos
DevinUnderwood2
Regular Contributor

Thanks. What messages would I want to troubleshoot my syntax ?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Your where clause is ever-so-slightly different from mine - missing a ')' after InputField.

0 Kudos
DevinUnderwood2
Regular Contributor

I have the following yet it is not working. Something still wrong with the expression.

Does it matter whether it is a .sde feature class .or file geodatabase feature ?

#Import Modules
import os,arcpy
#Set to current mxd and dataframe
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

# Set the tool parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
# SQL expression used in selecting a feature
whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
#Select feature by facilityid (InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
0 Kudos
DarrenWiens2
MVP Honored Contributor

Try passing the full path to your feature class for InputFeatureClass. Otherwise, what's the exact error message?

0 Kudos
DevinUnderwood2
Regular Contributor

I placed the full path and still no luck. Here is the following error message.

Traceback (most recent call last):

File "C:\Users\...Scripts\QueryMxdFeatureClass.py", line 18, in <module>

arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)

File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute

raise e

ExecuteError: ERROR 000358: Invalid expression

Failed to execute (SelectLayerByAttribute).

 

Failed to execute (QueryMxdFeatureClass).

Failed at Tue Feb 07 09:06:24 2017 (Elapsed Time: 0.47 seconds)

0 Kudos
DarrenWiens2
MVP Honored Contributor

What do you get when you print the whereclause?

arcpy.AddMessage(whereclause)
0 Kudos
DevinUnderwood2
Regular Contributor

Interesting, I used arcpy.AddMessage(whereclause) and get  the following results.

I am assuming that it successfully received the Inputvalue., but can't select the feature.

Executing: QueryMxdFeatureClass "SWR Layers\Manholes" FACILITYID 5564

Start Time: Tue Feb 07 09:19:10 2017

Running script QueryMxdFeatureClass...

"FACILITYID" = 5564

Failed script QueryMxdFeatureClass...

0 Kudos
DarrenWiens2
MVP Honored Contributor

Can you try moving the "Manholes" layer outside of the "SWR Layers" group and try it just as "Manholes"?

0 Kudos
DevinUnderwood2
Regular Contributor

I removed it from the group and same error.

0 Kudos