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?

  • 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. – ed.hank Jul 11 '18 at 15:57
  • @ed.hank the data is in a specified GIS folder – Jonathan P Jul 11 '18 at 16:17
  • I would take out your try/except so that any helpful error messages are not being masked. – PolyGeo Jul 11 '18 at 20:16
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) 
  • 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. – Jonathan P Jul 11 '18 at 16:15
  • updated my answer – KHibma Jul 11 '18 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... – Jonathan P Jul 11 '18 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. – Bjorn Jul 11 '18 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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