CopyFeatures_management() and FeatureClassToFeatureClass_conversion() return blank shp files

264
6
Jump to solution
01-10-2021 08:53 AM
YanningWei_LoyolaChicago
New Contributor II

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!  

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

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)

 

View solution in original post

6 Replies
DanPatterson
MVP Honored Contributor

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


... sort of retired...
YanningWei_LoyolaChicago
New Contributor II

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 + " ' " 

0 Kudos
BlakeTerhune
MVP Regular Contributor

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)

 

View solution in original post

YanningWei_LoyolaChicago
New Contributor II

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.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

YanningWei_LoyolaChicago
New Contributor II

Thanks a lot!

0 Kudos