Here is the issue, which is quite simple:
tried to create six individual shp files (six counties in IL) by using Select by Attributes, but got six blank shp files.
Input: IL_counties.shp, NAD83
I tested a python script as follows:
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Users/....."
try:
arcpy.MakeFeatureLayer_management("IL_counties.shp", "lyr")
counties =["LAKE", "COOK", "DUPAGE", "KANE", "MCHENRY", "WILL"]
for k in counties:
qry = "COUNTY_NAM = + 'k' "
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", qry )
arcpy.FeatureClassToFeatureClass_conversion("lyr", "C:/Users/.....", k+"02.shp")
except: print "An error occurred during creation"
I tried both CopyFeatures_management() and FeatureClassToFeatureClass_conversion().
No error was reported, but the outputs are all blank. In particular, all the Extent values are blank (screenshot).
What might be wrong? Many thanks!
Solved! Go to Solution.
As @DanPatterson mentioned, your query looks off. You should be able to use the where_clause parameter of FeatureClassToFeatureClass_conversion(). That way you don't have to make a selection. Also, try using string format instead of the + operator. It's more reliable because it will convert data types on the fly as needed (in case your variable is a number instead of a string).
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Users/....."
try:
counties =["LAKE", "COOK", "DUPAGE", "KANE", "MCHENRY", "WILL"]
for k in counties:
arcpy.FeatureClassToFeatureClass_conversion(
in_features="lyr",
out_path="C:/Users/.....",
out_name="{}02.shp".format(k),
where_clause="COUNTY_NAM = '{}'".format(k)
)
except Exception as e:
print "An error occurred during creation\n{}".format(e)
quick look suggests querys are off
for k in counties:
q = f"COUNTY_NAM = '{k}'"
print(q)
COUNTY_NAM = 'LAKE'
COUNTY_NAM = 'COOK'
COUNTY_NAM = 'DUPAGE'
COUNTY_NAM = 'KANE'
COUNTY_NAM = 'MCHENRY'
COUNTY_NAM = 'WILL'
the single quote thing, I think
SQL reference for query expressions used in ArcGIS—ArcGIS Pro | Documentation
Thanks, Dan. Yes, I just did a test and found that it was the query that failed.
The string should be defined as
qry = "COUNTY_NAM = ' " + k + " ' "
As @DanPatterson mentioned, your query looks off. You should be able to use the where_clause parameter of FeatureClassToFeatureClass_conversion(). That way you don't have to make a selection. Also, try using string format instead of the + operator. It's more reliable because it will convert data types on the fly as needed (in case your variable is a number instead of a string).
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Users/....."
try:
counties =["LAKE", "COOK", "DUPAGE", "KANE", "MCHENRY", "WILL"]
for k in counties:
arcpy.FeatureClassToFeatureClass_conversion(
in_features="lyr",
out_path="C:/Users/.....",
out_name="{}02.shp".format(k),
where_clause="COUNTY_NAM = '{}'".format(k)
)
except Exception as e:
print "An error occurred during creation\n{}".format(e)
Many thanks, Blake. Problem solved!
When using a shp file or a feature class as the input, is it correct that we need to create a feature layer first by using arcpy.MakeFeatureLayer_management() before Select by Attributes? Thanks again.
You could still do the same thing using a feature layer and selection, omitting the where_clause parameter in FeatureClassToFeatureClass_conversion(). However, it's not needed in my example because the "selection" expression is taken care of with the where_clause parameter.
Also, note the change in the except block. I included the actual error message so you can troubleshoot errors rather than getting the same generic message each time.
Thanks a lot!