2

This is a follow up to Output field name not showing up in spatial join result 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?

4
  • 1
    From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.
    – FelixIP
    Commented May 15, 2019 at 4:28
  • have you ever tried it directly on a physical file?
    – user32882
    Commented May 15, 2019 at 4:37
  • Nyet. Just found I didn't even use mapping. It was fieldInfo.
    – FelixIP
    Commented May 15, 2019 at 5:26
  • I'm surprised this code even executes as file_path is not a valid full path to a shapefile?
    – Hornbydd
    Commented May 17, 2019 at 9:14

1 Answer 1

4

In your second loop where you

print fm.outputfield.name

add another line to

print fm.inputfield.name

to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.

The documentation has some good examples on this idea, fieldmapping docs.

Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.

address_fms = arcpy.FieldMappings()
try:
    # Start loop to make and add fieldmaps to fieldmappings object
    for i in range(61):
        str_i = str(i)
        ## Assign a fieldmap object to a variable
        str_i = arcpy.FieldMap()
        ## Attribute the object's InputField
        str_i.addInputField(intable, inflds[i])
        ## Assign the outputField object to a variable
        fm_name = str_i.outputField
        ## Attribute the outputField object's name attribute
        fm_name.name = outflds[i]
        ## Assign the field object with a populated name value
        ## to the fieldmap object
        str_i.outputField = fm_name
        # Add them to fieldmappings object so it works
        address_fms.addFieldMap(str_i)

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.