Copy feature class for selected field

2937
3
08-02-2012 05:58 AM
AmlanRoy
New Contributor
Hi,

I have an existing code to copy feature class from sde to file geodatabase (using arcpy.FeatureClassToGeodatabase_conversion).     Recently I have added a new field  feature classes in sde. My requirement is new field should not come to file geodatabase. Is it possible to copy a featureclass from sde to file geodatabase having selected fields only?

Thanks in advance
Tags (2)
0 Kudos
3 Replies
ChristopherThompson
Occasional Contributor III
if you first use arcpy.MakeFeatureLayer to reference the data you want to copy to the gdb then you can do this by making use of the last optional argument called 'field info'.. the help for this tool says this:
A subset of fields can be made unavailable in the new layer by using the Field Info control's visible property. The third column in the control provides a dropdown option to specify whether a field will be visible or hidden in the new layer. The default is TRUE. Selecting FALSE will hide that field. You cannot use the hidden fields in a workflow if the newly created layer is input to a subsequent process or tool.   If the output is saved to disk, only the fields listed as visible will appear in the new data.


Depending on how many fields are involved, setting up the fields data in a digestible fashion into the 'field info' part of this tool can be very cumbersome. A work around for this is to set up a dummy modelbuilder tool to do this for you, manage the fields through the GUI then export the model to a script and then copy/past the MakeFeatureLayer command and arguments into your script. A bit of a kludge but given my typing skills this works fairly well.

I'm not sure if the tool you are using accepts a feature layer as an input or wants a reference to a file on disk so that might alter your workflow a little. Is there a reason you are using the FeatureClassToGeodatabase_conversion tool rather than CopyFeatures?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can also you the Feature Class to Feature Class (conversion) tool.  This allows you to use the field map option:

You can add, rename, or delete output fields as well as set properties such as data type and merge rule.
0 Kudos
JessiePechmann2
New Contributor II

I found this example and made it even simpler. FC equals your feature class, fieldList equals what fields you want to keep. My code is below.



#Define function to take an input layer and output a layer with only a defined field list
def filter_fields(FC, fieldList):

# List input fields
fields= arcpy.ListFields(FC)

# Create a fieldinfo objects
fieldinfo = arcpy.FieldInfo()

# Iterate over input fields, add them to the FieldInfo and hide them if
# they aren't in the list of fields to be kept
for field in fields:
if not field.name in fieldList:
fieldinfo.addField(field.name, field.name, "HIDDEN", "")

# Copy features to a layer using the FieldInfo
temp = "temp"
arcpy.MakeFeatureLayer_management(FC, temp, "", "", fieldinfo)

#Call the function
outFeature= "admin3_AllJoined"
filter_fields(outFeature,["Admin3_admin1Name","Admin3_admin3Name","Education_Table_Education_Percent","Education_Table_Education","Food_Table_Food_Percent","Food_Table_Food_Fraction","Healthcare_Table_Healthcare_Percent","Healthcare_Table_Healthcare_Fraction","Hygiene_Table_Hygiene_Percent","Hygiene_Table_Hygiene_Fraction","NFI_Table_NFI_Percent","NFI_Table_NFI_Fraction","Nutrition_Table_Nutrition_Percent","Nutrition_Table_Nutrition_Fraction","Protection_Table_Protection_Percent","Protection_Table_Protection_Fraction","Shelter_Table_Shelter_Percent","Shelter_Table_Shelter_Fraction","Water_Table_count","Water_Table_Water_Percent","Water_Table_Water_Fraction"])

#arcpy.CopyFeatures_management(layerName,outFeature)
arcpy.CopyFeatures_management("temp",outFeature2)

0 Kudos