Select Layer By Attribute User Input

2070
6
Jump to solution
01-08-2013 07:15 AM
ScottMacDonald
New Contributor III
I have been puzzling over a script I have to write and run in an Arc toolbox and I wondered if anyone could tell me where I'm going wrong.

The following code relates to a rivers shapefile called Rio_Panuco in a geodatabase called panuco.gdb.  There is a field in the shape file called "type" and the attribute I want to select is "link".

I was able to create the file I wanted when the script was hard coded to the geodatabase using the code below-
arcpy.SelectLayerByAttribute_management("River_lyr", "NEW_SELECTION", '"type" = \'link\'')

However, I need to write generic code and require the user to input the requirements i.e. Select * from Rio_Panuco where "type" = 'link'

I have written the following code to do this (and variations galore!) but still cannot get it to work properly. I've also put the code in ArcToolbox but when the Select Layer By Attribute box is clicked it is blank.  I would be grateful for any advice.

# extract features by attrtibute

# import system modules
import arcpy
from arcpy import env

# get user supplied path, layers and fields
path is workspace
path = arcpy.GetParameterAsText(0)

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = '"' str(typeField) + " = '" + str(confluence) + "'"
rows = arcpy.SearchCursor(inputLayer, whereClause)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText()

# set overwrite option
# arcp.overwriteOutput = true

# error trapping measures
try:
# make a layer from the feature class
arcpy.MakeFeatureLayer_management(inputLayer, "River_lyr")

# select layer by attribute
arcpy.SelectLayerByAttribute_management("River_lyr", "SelectionRiverLayer", whereClause)

# write selected features to a new featureclass
arcpy.CopyFeatures_management("River_lyr", "SelectionRiverLayer")

except:
print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ScottMacDonald
New Contributor III
Perfect - it's working fine now, thanks a lot for your help

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
What error are you getting?
Why are you using a cursor?
Here is a better example of string substitution creating a query string.
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)
0 Kudos
ScottMacDonald
New Contributor III
Thanks mzcoyle for replying to my thread.
I tried substituting some of my code for the code that you sent (script below) but I couldn't get it to work.  It came up with the following error -
<type 'exceptions.NameError'>: name 'path' is not defined
Failed to execute (tryRiver).

I am also not sure why I was using a cursor - I thought I needed this to search through the fieldname (called type) for rivertypes (called link).  I'm new to scripting and all I want to do is create an Arc tool based on arcpy scripting that lets a user navigate to the feature class (Rio_Panuco) and lets them select "type" = 'link' (fieldname = attribute) and saves the feature to a new layer.

I've noticed that the select by attributes tool required a layer as input and not a dataset so I couldn't get that to work and I tried basic make feature layer commands.  I can make a feature layer from a feature class using user input but I cannot select only fields whose type = link and then save it to a new layer.

I would appreciate any further help or suggestions on this.

Code was -
# extract features by attrtibute

# import system modules
import arcpy
from arcpy import env

# get user supplied path, layers and fields
path is workspace
path = arcpy.GetParameterAsText(0)

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText()

# set overwrite option
# arcp.overwriteOutput = true

# error trapping measures
try:
# make a layer from the feature class
arcpy.MakeFeatureLayer_management(inputLayer, "River_lyr")

# select layer by attribute
arcpy.SelectLayerByAttribute_management("River_lyr", "SelectionRiverLayer", whereClause)

# write selected features to a new featureclass
arcpy.CopyFeatures_management("River_lyr", "SelectionRiverLayer")

except:
print arcpy.GetMessages()
Kind regards

Scottaidh
0 Kudos
MathewCoyle
Frequent Contributor
This line looks like the problem, just delete it.
path is workspace


Also you can accomplish what you want to do with one tool, Select.

Something like this.
# extract features by attrtibute

# import system modules
import arcpy

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0)
arcpy.env.workspace = path

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText(4)

# error trapping measures
try:

    arcpy.Select_analysis(inputLayer, outputLayer, whereClause)

except:
    print arcpy.GetMessages()
0 Kudos
ScottMacDonald
New Contributor III
Thanks again Matthew for your reply.
Unfortunately I'm still having problems.
Firstly, I removed the 'path is workspace' line but it made no difference.

I decided to try your Select_analysis suggestion but I can't seem to get the Where_clause running correctly.
I get the following error message -
<type 'exceptions.NameError'>: name 'typeField' is not defined
Failed to execute (Select).

I'd be grateful for any advice on this as I've been trying to create this layer for days now to no avail!

Scott

code -
# extract features by select analysis
# import system modules
import arcpy

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0)
arcpy.env.workspace = path

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText(4)

# error trapping measures
try:
# make a layer from the feature class
arcpy.Select_analysis(inputLayer, outputLayer, whereClause)
except:
print arcpy.GetMessages()
0 Kudos
MathewCoyle
Frequent Contributor
Oh yes you need to have your whereclause after your typeField parameter assignment.
0 Kudos
ScottMacDonald
New Contributor III
Perfect - it's working fine now, thanks a lot for your help
0 Kudos