Skip to main content
deleted 130 characters in body
Source Link
GISGe
  • 9.7k
  • 19
  • 42

The FieldInfo() object seems to me to be more appropriate than the FieldMappings if you want to drop fields from your input. It allows you to modify some properties of your input fields when passing them to the output. One of these properties is visibility (if a field is not visible, it won't be passed to the output). It is used among other by the Make Feature Layer tool. I would copy your input to a feature layer with a FieldInfo specifying which fields have to be kept (visible) or notdropped (hidden), and then use Feature Class to Feature Class without FieldMappings:

def subset_attributes(origFC, fieldList, outFC):

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

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

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

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

    # Export the layer to a feature class
    arcpy.FeatureClassToFeatureClass_conversion("temp", scratchWS, outFC)

This code will work even if you drop all atrributes from the input.

The FieldInfo() object seems to me to be more appropriate than the FieldMappings if you want to drop fields from your input. It allows you to modify some properties of your input fields when passing them to the output. One of these properties is visibility (if a field is not visible, it won't be passed to the output). It is used among other by the Make Feature Layer tool. I would copy your input to a feature layer with a FieldInfo specifying which fields have to be kept (visible) or not (hidden), and then use Feature Class to Feature Class without FieldMappings:

def subset_attributes(origFC, fieldList, outFC):

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

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

    # Iterate over input fields, add them to the FieldInfo and make them visible 
    # if they are in the list of fields to be kept, hide them otherwise
    for field in fields:
        if field.name in fieldList:
            fieldinfo.addField(field.name, field.name, "VISIBLE", "")
        else:
            fieldinfo.addField(field.name, field.name, "HIDDEN", "")

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

    # Export the layer to a feature class
    arcpy.FeatureClassToFeatureClass_conversion("temp", scratchWS, outFC)

This code will work even if you drop all atrributes from the input.

The FieldInfo() object seems to me to be more appropriate than the FieldMappings if you want to drop fields from your input. It allows you to modify some properties of your input fields when passing them to the output. One of these properties is visibility (if a field is not visible, it won't be passed to the output). It is used among other by the Make Feature Layer tool. I would copy your input to a feature layer with a FieldInfo specifying which fields have to be dropped (hidden), and then use Feature Class to Feature Class without FieldMappings:

def subset_attributes(origFC, fieldList, outFC):

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

    # 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
    arcpy.MakeFeatureLayer_management(origFC, "temp", "", "", fieldinfo)

    # Export the layer to a feature class
    arcpy.FeatureClassToFeatureClass_conversion("temp", scratchWS, outFC)

This code will work even if you drop all atrributes from the input.

Source Link
GISGe
  • 9.7k
  • 19
  • 42

The FieldInfo() object seems to me to be more appropriate than the FieldMappings if you want to drop fields from your input. It allows you to modify some properties of your input fields when passing them to the output. One of these properties is visibility (if a field is not visible, it won't be passed to the output). It is used among other by the Make Feature Layer tool. I would copy your input to a feature layer with a FieldInfo specifying which fields have to be kept (visible) or not (hidden), and then use Feature Class to Feature Class without FieldMappings:

def subset_attributes(origFC, fieldList, outFC):

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

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

    # Iterate over input fields, add them to the FieldInfo and make them visible 
    # if they are in the list of fields to be kept, hide them otherwise
    for field in fields:
        if field.name in fieldList:
            fieldinfo.addField(field.name, field.name, "VISIBLE", "")
        else:
            fieldinfo.addField(field.name, field.name, "HIDDEN", "")

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

    # Export the layer to a feature class
    arcpy.FeatureClassToFeatureClass_conversion("temp", scratchWS, outFC)

This code will work even if you drop all atrributes from the input.