Skip to main content
4 of 4
deleted 29 characters in body
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Using ArcPy FieldMappings?

This is a follow up to https://gis.stackexchange.com/questions/322653 in which I received no response.

I am trying to convert a shapefile to a feature class, I also have a GeoJSON file with the field names as I would like them to appear in the output feature class. The arcpy.FeatureClassToFeatureClass_conversion functionality accepts a field map, so I tried to build one by looping through a GeoPandas geodataframe of the GeoJSON file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:

import archook
archook.get_arcpy()
import arcpy
import geopandas

file_path = 'damage_results/Culverts/Culverts.shp'
original_fields = arcpy.ListFields(file_path)
original_fields = original_fields[2:]
gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
new_field_names = gdf.columns[:-1].to_list()

fms = arcpy.FieldMappings()
fms.addTable(file_path)

for i,field in enumerate(original_fields):
    print i
    fm = fms.getFieldMap(i)

    out_field = fm.outputField
    out_field.name = new_field_names[i]
    fm.outputField = out_field
    fms.replaceFieldMap(i, fm)

for fm in fms:
    print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
                                            'Culverts', field_mapping = fms)

The second for loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile.

Does the output field attribute in the FieldMap object work?

user32882
  • 3.6k
  • 2
  • 32
  • 71