import arcpy, os workspace = "C:/Temp" print "Workspace set to:", workspace arcpy.AddMessage("Workspace set to: "+workspace) arcpy.env.overwriteOutput=True 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)[0].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)) return whereClause def outName(input,post="Out",fileExtension="shp"): """Returns output name.""" outName=os.path.basename(input).split(".")[0]+post+"."+fileExtension return outName #Make Feature Layer from County Boundary Feature Class InputFC = "C:\Project\ToolData\CountyBoundary.shp" OutputFL = outName(InputFC,"_Layer","lyr") try: arcpy.MakeFeatureLayer_management(InputFC, OutputFL) except: arcpy.GetMessages() #Select Attributes from County Boundary Layer Field = "CO_NAME" Counties = arcpy.GetParameterAsText(0) SQL = whereClause(OutputFL, Field, Counties) try: arcpy.SelectLayerByAttribute_management(OutputFL,"NEW_SELECTION", SQL) count = int(arcpy.GetCount_management(OutputFL).getOutput(0)) print '\n',"There are", count, "counties selected from County Boundary layer.",'\n' except: print arcpy.GetMessages() #Copy Selected Feature Layer to new Feature Class OutputFC = outName(InputFC,"_Selection","shp") try: arcpy.CopyFeatures_management(OutputFL, OutputFC) except: print arcpy.GetMessages()Solved! Go to Solution.
import arcpy, os
workspace = "C:/Temp"
print "Workspace set to:", workspace
arcpy.AddMessage("Workspace set to: "+workspace)
arcpy.env.overwriteOutput=True
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)[0].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))
return whereClause
def outName(input,post="Out",fileExtension="shp"):
"""Returns output name."""
outName=os.path.basename(input).split(".")[0]+post+"."+fileExtension
return outName
#Make Feature Layer from County Boundary Feature Class
InputFC = "C:\Project\ToolData\CountyBoundary.shp"
# Try running again - I commented out and replaced your suspect line of code:
#OutputFL = outName(InputFC,"_Layer","lyr")
OuputFL = os.path.splitext(outName(InputFC,"_Layer","lyr"))[0]
try:
arcpy.MakeFeatureLayer_management(InputFC, OutputFL)
except:
arcpy.GetMessages()
#Select Attributes from County Boundary Layer
Field = "CO_NAME"
Counties = arcpy.GetParameterAsText(0)
SQL = whereClause(OutputFL, Field, Counties)
try:
arcpy.SelectLayerByAttribute_management(OutputFL,"NEW_SELECTION", SQL)
count = int(arcpy.GetCount_management(OutputFL).getOutput(0))
print '\n',"There are", count, "counties selected from County Boundary layer.",'\n'
except:
print arcpy.GetMessages()
#Copy Selected Feature Layer to new Feature Class
OutputFC = outName(InputFC,"_Selection","shp")
try:
arcpy.CopyFeatures_management(OutputFL, OutputFC)
except:
print arcpy.GetMessages()
import arcpy, os
workspace = "C:/Temp" #Change to Set Parameter
print "Workspace set to:", workspace
arcpy.AddMessage("Workspace set to: "+workspace)
arcpy.env.overwriteOutput=True
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)[0].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))
return whereClause
def outName(input,post="_output"):
"""Returns output name."""
outName=os.path.basename(input).split(".")[0]+post
return outName
#Make Feature Layer from County Boundary Feature Class
InputFC = "C:\Project\ToolData\CountyBoundary.shp"
OutputFL = outName(InputFC,"_Layer")
try:
arcpy.MakeFeatureLayer_management(InputFC, OutputFL)
except:
arcpy.GetMessages()
#Select Attributes from County Boundary Layer
Field = "CO_NAME"
Counties = arcpy.GetParameterAsText(0)
SQL = whereClause(OutputFL, Field, Counties)
try:
arcpy.SelectLayerByAttribute_management(OutputFL,"NEW_SELECTION", SQL)
count = int(arcpy.GetCount_management(OutputFL).getOutput(0))
print '\n',"There are", count, "counties selected from County Boundary layer.",'\n'
except:
print arcpy.GetMessages()
#Copy Selected Feature Layer to new Feature Class
OutputFC = outName(InputFC,"_Selection.shp")
try:
arcpy.CopyFeatures_management(OutputFL,OutputFC)
print "Selected counties were exported to feature class",'\n'
except:
print arcpy.GetMessages()