0

Using ArcGIS 10.4, arcpy

I am having trouble understanding field mappings and the esri examples aren't helping at all. When I use model builder the field mappings make sense, but I need to customize them more. I can successfully take the ObjectID field,add it to my new feature class with the input data from the old, but that is the only field that comes in. Is there a way to add all the others instead of going one by one? It seems like it should be so simple, I tried the (addtable) but that is where I am lost.

My end game is to keep ObjectID in my new feature class, I want everything else the same (Type, Length, etc). Right now on my 'FeatureClasstoFeatureClass' all I can keep with the field mappings is ObjectID. I could join it up and rexport but field mapping seems like it should be the easiest.

http://pro.arcgis.com/en/pro-app/arcpy/classes/fieldmappings.htm

I am looking at the examples here. They only mention adding two fields, but what about every field.

1
  • 1
    Field mappings in arcpy are confusing. What do you mean by "My end game is to keep ObjectID in my new feature class, I want everything else the same (Type, Length, etc)."? If you want everything the same you dont need to use field mapping. It is optional and no field mapping = no changes
    – Bera
    Commented Aug 16, 2018 at 5:25

1 Answer 1

1

This is an example how you can use field mapping in a function to return a feature layer with only specified fields visible:

import arcpy

def get_fl_with_optional_fields(in_fc, fieldlist):
    fc_desc = arcpy.Describe(in_fc)
    fl = arcpy.MakeFeatureLayer_management(in_fc,fc_desc.baseName)
    src_field_info = arcpy.Describe(fl).fieldInfo
    for field in [f.name for f in arcpy.ListFields(in_fc)]:
        field_idx = src_field_info.findfieldbyname(field)
        if field in fieldlist:
            src_field_info.setVisible(field_idx,'VISIBLE')
        else:
            src_field_info.setVisible(field_idx,'HIDDEN')
    newfl = arcpy.MakeFeatureLayer_management(in_fc,in_fc+'_fl',field_info=src_field_info)
    return newfl

arcpy.FeatureClassToFeatureClass_conversion(in_features=get_fl_with_optional_fields(r'C:\data.gdb\polygon123',['SomeFieldName','SomeFieldName2']), out_path=r'C:\data.gdb', 
                                           out_name='newpolygons')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.