1

I'm trying to script another Select by Attributes tool. I want to be able to find a specific building using a field and zoom in to the location. However, my tool needs to allow a user to input any field or any value. So far this is what I got.

#Import Modules
import arcpy

arcpy.env.workspace = "C:\Users\pierrej\Desktop\GIS Data"
arcpy.env.overwriteOutput = True


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

try:
    # Set the tool parameters
    InputFeatureClass = arcpy.GetParameterAsText(0)
    InputField = arcpy.GetParameterAsText(1)
    InputValue = arcpy.GetParameterAsText(2)

    # SQL expression used in selecting a feature 
    #Create the query
    qry = "\"InputField\" = InputValue"

    arcpy.MakeFeatureLayer_management(InputFeatureClass, "Layer")

    #Call the Select Layer by Attribute tool
    arcpy.SelectLayerByAttribute_management("Layer", "NEW_SELECTION", qry)

    #Zooming to a selection set for the specific layer
    df.zoomToSelectedFeatures()
    df.scale = 2500000
    arcpy.RefreshActiveView()

    # Report a success message    
    arcpy.AddMessage("All done!")


#Print an error message in the event of a problem:
    except:
    print "An error occurred during selection"

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?

4
  • is the data in a SDE or a file geodatabase? If i had to guess I would say you have a problem with your query format. Commented Jul 11, 2018 at 15:57
  • @ed.hank the data is in a specified GIS folder Commented Jul 11, 2018 at 16:17
  • I would take out your try/except so that any helpful error messages are not being masked. Commented Jul 11, 2018 at 20:16
  • Use AddFieldDelimiters when constructing query Commented Feb 5, 2020 at 7:23

3 Answers 3

2

When you say the "tool doesnt select anything", I assume you mean you don't see a selection in the map.

It doesn't appear to select anything because you've run Make Feature Layer and and performed a selection on that layer -- that is, a layer that's local to the tool instead of the Map. You may have a layer in your map that's source points to the same input as the featureclass you provided to the tool, but you basically have two instances of these layers now.

Fix: Change the input parameter type from feature class to feature layer. Remove the Make Feature Layer call and have Select Layer by Attribute work against the input layer

Also a problem I see - how your constructing your WHERE_CLAUSE. Swap in the code block below. As right now, you're not using the inputs from the variables you've created. Right now you're using strings named "InputField" and "inputvalue"

InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

#SQL expression used in selecting a feature 
#Create the query
qry = "'{}'= {}".format(InputField, InputValue) 
4
  • Hi, so the input parameter type is already Feature Layer. I did remove the Make Feature Layer call and switched the input for the Select Layer by Attribute but I still have nothing. Commented Jul 11, 2018 at 16:15
  • updated my answer Commented Jul 11, 2018 at 16:33
  • Tried it and I even added quotation marks in case but still nothing. When ever I got to show the selected records, no records are showing and same thing when I look at the map. It's like that with any data set I'll input. What I don't understand is that the SQL query works perfectly when its prompted in the Python window but as a Python script tool, nothing happens... Commented Jul 11, 2018 at 16:49
  • qry = "\"{}\"= '{}'".format(InputField, InputValue) if InputField is defined as text, or qry = "\"{}\"= {}".format(InputField, InputValue) if InputField is defined as a number. Commented Jul 11, 2018 at 18:55
0

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)   
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) I'd like to know what happens if I want to select more than one site and how to I edit my script so that it accepts String fields.

1
  • where_clause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(FClass, Field),Feature) should work if the field is type String Commented Jul 11, 2018 at 20:01
0

I had a similar issue where in i wanted a particular input(integer) to be entered by the user. On selection of the parcular input feature, further processing would happen and the final output would be a excel file.

Select by attribute was giving and issue as it would create a temporary file of the selection and I was not able to locate it. thus, the final table would be of the original file.

Hence i altered my code and used make feature layer, and applied the query in this command. the code works fine now.


arcpy.env.workspace = r"D:\arcPY\output"
arcpy.env.overwriteOutput = True
FClass = arcpy.GetParameterAsText(0)
Feature = arcpy.GetParameterAsText(1)
qry = "PARCEL_KEY = {}".format(Feature)

output_buffer = r"D:\arcPY\output\O_buffer3.shp"
output_clip = r"D:\arcPY\output\O_clip3.shp"
plots = r"D:\arcPY\output\plots4.xls"
Parcel_layer = r"D:\arcPY\output1\Parcel_copy"

arcpy.MakeFeatureLayer_management(FClass, Parcel_layer, qry )
arcpy.Buffer_analysis(Parcel_layer, output_buffer, 0.0001)
arcpy.Clip_analysis(FClass, output_buffer, output_clip)
arcpy.TableToExcel_conversion(output_clip, plots)



Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.