pierrejonathan.jp

Select Layer By Attribute Python Script Tool

Discussion created by pierrejonathan.jp on Jul 11, 2018
Latest reply on Jul 11, 2018 by pierrejonathan.jp

Hello everyone,

 

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

 

import arcpy


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

 

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

 

try:
    # Get the input parameters for the Selection Tool
    FClass = arcpy.GetParameterAsText(0)
    Field = arcpy.GetParameterAsText(1)
    Feature = arcpy.GetParameterAsText(2)

 

    # Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(FClass,"FclassLayer")

 

    where_clause = """{} = {}""".format(arcpy.AddFieldDelimiters("FclassLayer", Field),Feature)
    arcpy.AddMessage(where_clause)
    
    # Select the site
    arcpy.SelectLayerByAttribute_management("FclassLayer","NEW_SELECTION", where_clause)
   
    # write selected features to a new featureclass
    arcpy.CopyFeatures_management("FclassLayer", "SelectionSites")
    
    #Zooming to a selection set for the specific layer
    df.zoomToSelectedFeatures()
    df.scale = 2500000
    arcpy.RefreshActiveView()
   
    # Report a success message    
    arcpy.AddMessage("All done!")


except:
     # Report an error messages
     arcpy.AddError("Could not complete")
 
     # Report any error messages that the tool might have generated    
     arcpy.AddMessage(arcpy.GetMessages())

 

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?

Outcomes