FeatureClassToFeatureClass_conversion with only certain fields

3007
3
Jump to solution
12-13-2013 01:05 PM
Emilbrundage
New Contributor III
Greetings,

I am using FeatureClassToFeatureClass_conversion to export a new feature based on a selection. I would only like to carry over certain fields. Is this something that can be done?

It looks like it is done through the FieldMap parameter but I have never worked with FieldMap objects...

EDIT: I've written this...

fieldmappings = arcpy.FieldMap ()
fieldmappings.addInputField(thePoint, "POINTGUIDt")
fieldmappings.addInputField(thePoint, "S_FILE")
fieldmappings.addInputField(thePoint, "LINE_NBR")

then try to use it here:

arcpy.FeatureClassToFeatureClass_conversion (thePoint, outDatabase, "PPMD_04V1i_" + envelopePolyTrim,"",fieldmappings)

and get this:

Traceback (most recent call last):
  File "C:\E1B8\ScriptTesting\SelectbyAttributeAndExport\Actual\ActualSelectbyAttributeAndExport.py", line 61, in <module>
    arcpy.FeatureClassToFeatureClass_conversion (thePoint, outDatabase, "PPMD_04V1i_" + envelopePolyTrim,"",fieldmappings)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass
    raise e
RuntimeError: Object: Error in executing tool

Am I close? Thanks 🙂
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
NobbirAhmed
Esri Regular Contributor
Your field mappings is not properly populated. There is no .addInputField(....) method on a FieldMappings object.

There are two ways you can populate an empty FieldMappings object:

1. .addTable() method of FieldMappings object - this is a wholesale course-grain approach - it adds all the fields of the table.

2. Create FieldMap object individually and then add the field map to FieldMappings object.

Code sample here:
http://resources.arcgis.com/en/help/main/10.1/#/FieldMappings/018z00000078000000/

See this image to find the difference between FieldMap and FieldMappings objects:

[ATTACH=CONFIG]29918[/ATTACH]

I would strongly encourage you to explore the field mapping mechanism as it is a "very powerful" tool to manipulate data. Once you spend some time you'll feel how easy it is to use 🙂

View solution in original post

0 Kudos
3 Replies
WilliamCraft
MVP Regular Contributor
I think it would be a bit easier (or at least, just as thorough) to do this with a Make Feature Layer followed by a Select By Attribute followed by a Copy Features.  Try something like this:

# Make a feature layer of your input data, specifying only the fields you care about; this feature layer is stored in memory
arcpy.MakeFeatureLayer_management(thePoint, "thePoint_selection", "", "", "POINTGUIDt POINTGUIt VISIBLE;S_FILE S_FILE VISIBLE;LINE_NBR LINE_NBR VISIBLE; OTHER_FIELD1 OTHER_FIELD1 HIDDEN; OTHER_FIELD2 OTHER_FIELD2 HIDDEN")

# Create your selection set (assuming an attribute-based selection); the selection will persist in memory for subsequent tools while the script is still running
arcpy.SelectLayerByAttribute_management("thePoint_selection", "NEW_SELECTION", "SOME_FIELD > 10000")

# Write the in-memory feature layer to a physical feature class in an output file geodatabase, which should copy over only the fields you specified from the Make Feature Layer tool
arcpy.CopyFeatures_management("thePoint_selection", "C:/E1B8/ScriptTesting/SelectbyAttributeAndExport/Actual/output.gdb/selection_set")
0 Kudos
NobbirAhmed
Esri Regular Contributor
Your field mappings is not properly populated. There is no .addInputField(....) method on a FieldMappings object.

There are two ways you can populate an empty FieldMappings object:

1. .addTable() method of FieldMappings object - this is a wholesale course-grain approach - it adds all the fields of the table.

2. Create FieldMap object individually and then add the field map to FieldMappings object.

Code sample here:
http://resources.arcgis.com/en/help/main/10.1/#/FieldMappings/018z00000078000000/

See this image to find the difference between FieldMap and FieldMappings objects:

[ATTACH=CONFIG]29918[/ATTACH]

I would strongly encourage you to explore the field mapping mechanism as it is a "very powerful" tool to manipulate data. Once you spend some time you'll feel how easy it is to use 🙂
0 Kudos
Emilbrundage
New Contributor III
Thanks for the tips guys. I fumbled through it and got it to work with this:

fms = arcpy.FieldMappings()
POINTGUIDt = arcpy.FieldMap ()
POINTGUIDt.addInputField(thePoint, "POINTGUIDt")
fms.addFieldMap(POINTGUIDt)
S_FILE = arcpy.FieldMap ()
S_FILE.addInputField(thePoint, "S_FILE")
fms.addFieldMap(S_FILE)
LINE_NBR = arcpy.FieldMap ()
LINE_NBR.addInputField(thePoint, "LINE_NBR")
fms.addFieldMap(LINE_NBR)

arcpy.FeatureClassToFeatureClass_conversion (thePoint, outDatabase, "PPMD_04V1i_" + envelopePolyTrim,"",fms)

I'm not sure if it's the prettiest way to do it but it worked! Thanks again.
0 Kudos