Selecting by Attribute - help

1887
1
08-21-2012 02:30 AM
IlanGoichman
New Contributor III
Hello,

Im trying to write a code that will be used as a tool on arcgis. The user will have to insert the number of a Block and the Number of a Parcel in 1 shapefile and the tool will create a JPEG map of the Block and Parcel.

The 1st thing i need to do is to select the correct Parcel and Block (Gush = Block, Helka = Parcel). The field for Gush in the shapefile is "GUSH_NUM", and the field for Helka is "Parcel"

So both Gush and Helka will have to be arcpy.GetParameterAsText, and using a variable in the where clause of the select by attribute tool.

But how do i write it in the where clause of the select by attribute tool? 

I tried using this post for help: http://forums.arcgis.com/threads/20459-Select-By-Attributes-using-a-variable-in-the-Where-clause?hig...

So i tried writing the where clause as the post suggests but it still doesnt work. I get an error - invalid expression.

Here is my code so far, i tried only on the Gush for now. 

Will apriciate any help. 🙂

# Import the arcgisscripting module. import arcpy  # Setting the Variabls.  # Input gush and Helka. Gush = arcpy.GetParameterAsText(0) Helka = arcpy.GetParameterAsText(1)  # Output Location (the folder where the resualt will be saved). Workspace = arcpy.GetParameterAsText(2)  arcpy.env.workspace = Workspace   hel_openland_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\hel_openland.shp" Output_Layer = "hel_openland_Layer"  arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)  arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = " + "'" + Gush + "'")
Tags (2)
0 Kudos
1 Reply
IlanGoichman
New Contributor III
Ok, i feel a little foolish.

I looked in another post: http://forums.arcgis.com/threads/65128-ArcPy-script-in-toolbox-what-do-I-do-about-optional-parameter...

and found the answer and it works! 🙂

the correct code is:

# Import the arcgisscripting module.
import arcpy

# Setting the Variabls.

# Input gush and Helka.
Gush = arcpy.GetParameterAsText(0)
Helka = arcpy.GetParameterAsText(1)

# Output Location (the folder where the resualt will be saved).
Workspace = arcpy.GetParameterAsText(2)

arcpy.env.workspace = Workspace


hel_openland_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\hel_openland.shp"
Output_Layer = "hel_openland_Layer"

arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)

arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = "+ Gush)
0 Kudos