I am trying to write a python script and create a script tool in ArcGIS that will let a user choose the value or values that will be used to extract a shapefile. These selected value(s) will then need to pass through the select tool. I'm sure this involves writing a WHERE clause but I'm new to python and am a little lost on this one.
Ok, I took another swing at it now that I think I have more of an idea of what you're looking for. It isn't perfect, as you can't select specific existing values, but you CAN select a field and enter as many values as you want into the query. Here's the code:
import arcpy from arcpy.analysis import * #Define input/output input = arcpy.GetParameterAsText(0) fieldName = arcpy.GetParameterAsText(1) values = arcpy.GetParameterAsText(2) output = arcpy.GetParameterAsText(3) #Build the where clause queryJoin = "' OR " + fieldName + " = '" whereClause = fieldName + " = '" + queryJoin.join(values) + "'" Select(input, output, whereClause)
When you set up your parameters, make sure that the field parameter is 'obtained from' "input". Also make sure you select "yes" for multi value for the values parameter.
This will give you something very similar to the example you linked to. I'm sure the query building could be done a better way but my brain is fried. Let me know if this is more along the lines of what you were looking for.
The following arcpy-based script which uses select layer by attributes is a good starting point. I retrofitted the script to work as a script tool in ArcGIS. You can easily substitute polygon FCs for point FCs in the Select Layers section to tailor the script to your specific needs.
# Import system modules import arcpy from arcpy import env # Set the workspace env.workspace = arcpy.GetParameterAsText(0) Dir = env.workspace # Local variables input = arcpy.GetParameterAsText(1) polygon = arcpy.GetParameterAsText(2) expression = arcpy.GetParameterAsText(3) name = arcpy.GetParameterAsText(4) # Make a layer from the input feature class arcpy.MakeFeatureLayer_management(input, "lyr") # Select all points or polygons which overlap the polygon/s of interest arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection") # Within selected features, further select based on a SQL query within the script tool arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression) # Write the selected features to a new featureclass arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))
Using the following parameters:
In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.
OK - so this is what I've come up with so far. It kind of works. Its going through the whole process but only giving me back the first county that I list as an argument. For example, when I type in county names in the arguments dialogue box I've tried typing it "ASHE";"AVERY" as well as "ASHE" "AVERY" and the script only takes in ASHE. I'm assuming this might be a syntax issue? Here is my current code:
def whereClause(table, field, values): """Takes a semicolon-delimited list of values and constructs a SQL WHERE clause to select those values within a given field and table."""
# Add field delimiters fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field) # Split multivalue at semicolons and strip quotes valueList = [value[1:-1] if (value.startswith("'") and value.endswith("'")) else value for value in values.split(';')] # Determine field type fieldType = arcpy.ListFields(table, field).type # Add single-quotes for string field values if str(fieldType) == 'String': valueList = ["'%s'" % value for value in valueList] # Format WHERE clause in the form of an IN statement whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(valueList))a return whereClause
def outName(input,post="Out",fileExtension="shp"): """Returns output name.""" outName=os.path.basename(input).split(".")+post+"."+fileExtension return outName
InputFC = "C:/Project/ToolData/CountyBoundary.shp" Field = "CO_NAME" Counties = arcpy.GetParameterAsText(0) #user selects counties from choice list SQL=whereClause(InputFC,Field,Counties) OutputFC=outName(InputFC,"_Select") arcpy.Select_analysis(InputFC,OutputFC,SQL) count=int(arcpy.GetCount_management(OutputFC).getOutput(0))
print OutputFC, " was created, and contains the following", count,"counties:"
sc = arcpy.SearchCursor(OutputFC)
for line in sc: print line.CO_NAME