[GIS] How to create a new feature class from an existing FC and a WHERE clause

arcgis-10.1arcpyerror-000800

I have a feature class. I want to copy the FC and use a WHERE clause to limit the rows copied. I tried using the CopyFeatures_management but it throws an error on the CopyFeatures_management() line.

Here's the code:

import arcpy

arcpy.env.workspace = 'C:/.../localFile.gdb'

inFC = 'inputFeatureClassName'
outFC = 'tmp_JUNK'

arcpy.CopyFeatures_management(inFC, outFC, '"FIELD_NAME" = "VALUE"')

The error thrown:

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000800: The value is not a member of  | DEFAULTS | TEXT_UTF16 | MAX_FILE_SIZE_4GB | MAX_FILE_SIZE_256TB | GEOMETRY_OUTOFLINE | BLOB_OUTOFLINE | GEOMETRY_AND_BLOB_OUTOFLINE | TERRAIN_DEFAULTS | MOSAICDATASET_DEFAULTS | MOSAICDATASET_INLINE.
Failed to execute (CopyFeatures).

Any ideas what I'm doing wrong and how to fix it?

Update #1 – This code works but I question whether it's really the best approach. If I understand this, I create a feature layer, perform the selection on the feature layer, and then write the selection to a new feature class. Is that correct? Is this the most efficient approach?

    # Import system modules
    import arcpy
    from arcpy import env

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Put in error trapping in case an error occurs when running tool
    try:

       # Make a layer from the feature class
       arcpy.MakeFeatureLayer_management("C:/.../localFile.gdb/tmp_FeatureClass","tmpOMG")

       arcpy.SelectLayerByAttribute_management("tmpOMG", "NEW_SELECTION", """"FIELD_NAME" = 'VALUE'""")

       # Write the selected features to a new featureclass
       arcpy.CopyFeatures_management("tmpOMG", "C:/.../localFile.gdb/tmpOutFromLayer")

except:
   print arcpy.GetMessages()

source: ArcGIS Help

Best Answer

You can't use a where_clause with Copy Features. Looking at the help page, the syntax is (with optional parameters in {} brackets):

CopyFeatures_management (in_features, out_feature_class, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})

So you're passing '"FIELD_NAME" = "VALUE"' as a config_keyword, which is confusing it and causing the error. You can only limit the features copied by starting off with a selection, and selections can only happen on layers (as in your second snippet).


I generally use Feature Class to Feature Class (arcpy.FeatureClassToFeatureClass_conversion), which can accept a where_clause.

FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

It's slightly annoying since you have to specify both output workspace and output feature class name, and I don't know why that's the case, but there you go. It's faster than Select_analysis (which does the same operation). For your case, the code would be something like:

arcpy.FeatureClassToFeatureClass_conversion(inFC, outGDB, outFC, '"FIELD_NAME" = "VALUE"')