AnsweredAssumed Answered

Append and field mapping with Python

Question asked by WF_Trebesch on Sep 22, 2020
Latest reply on Oct 5, 2020 by rvburton

 

 

I  am learning to append and use fieldmap, flieldmappings to get my fields squared away.

basically I'm appending data into a "shell" featureclass that is empty.  There's only a few fields I'm appending and trying to match.

 

I'm using this post as a reference:

Append Tool and Field Mapping ... Help? Examples? 

 

I'm able to add ONE field change with this script:

(append and target layers are defined earlier in the script as well as importing the arpy library

# field map and append

append_layer = outputclip
target_layer = out_name


#This is like defining an empty grid of fields you see when you run it manually in the toolbox
fieldmappings = arcpy.FieldMappings()

#Add the target datasets fields to the field map table  (Like when you manually choose a tagert layer in the toolbox)
fieldmappings.addTable(target_layer)

#Add the append datasets fields to the field map table  (Like when you manually choose a tagert layer in the toolbox)
fieldmappings.addTable(append_layer)

#At this point, you have a grid like when you run it manually saved in your field mappings.




#####Lets map a field that have different names!


#Find which "Index" the field has as we cant refer to them by name when editing the data only index
field_to_map_index = fieldmappings.findFieldMapIndex("PropertyAddress")  #Field name that exists in the target layer but not append data source!

#Grab "A copy" of the field map object for this particular field
field_to_map = fieldmappings.getFieldMap(field_to_map_index)

#Update its data source to add the input from the the append layer
field_to_map.addInputField(append_layer, "Addy_Concat")



#####Lets update the master field map using this updated copy of a field

fieldmappings.replaceFieldMap(field_to_map_index, field_to_map)


#Create a list of append datasets and run the the tool
arcpy.Append_management(append_layer, target_layer, "NO_TEST", fieldmappings, "1")

So, this works for me.  I end up appending the "Addy_Concat" field into the "PropertyAddress" field.   Great.

 

But I have 3 other fields to do this with.  So I used this:

# field matching for append
# Name the Append and Target Layer (target layer is named in the ArcGIS Pro Map)
append_layer = outputclip
target_layer = out_name

fieldmappings = arcpy.FieldMappings()

# Like when you manually choose a layer in the toolbox and it adds the fields to grid
fieldmappings.addTable(target_layer)
fieldmappings.addTable(append_layer)

# Lets map fields that have different names!
list_of_fields_we_will_map = []

# list_of_fields_we_will_map.append(( append layer field name, target layer field name)
list_of_fields_we_will_map.append(('Addy_Concat', 'PropertyAddress'))
#list_of_fields_we_will_map.append(('CMUNITY', 'PropertyCity'))
#list_of_fields_we_will_map.append(('ZipCode', 'PropertyZip'))

print(*list_of_fields_we_will_map, sep = ", ")


for field_map in list_of_fields_we_will_map:
    # Find the fields index by name.
    field_to_map_index = fieldmappings.findFieldMapIndex(field_map[0])
    # Grab "A copy" of the current field map object for this particular field
    field_to_map = fieldmappings.getFieldMap(field_to_map_index)
    # Update its data source to add the input from the the append layer
    field_to_map.addInputField(append_layer, field_map[1])
    # We edited a copy, update our data grid object with it
    fieldmappings.replaceFieldMap(field_to_map_index, field_to_map)

# Create a list of append datasets and run the the tool - the "1" at the end is for the subtype.
inData = [append_layer]
arcpy.Append_management(inData, target_layer, "NO_TEST", fieldmappings, "1")

 

I keep on getting this error:

Traceback (most recent call last):
File "<string>", line 119, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\arcobjects\arcobjects.py", line 666, in addInputField
return convertArcObjectToPythonObject(self._arc_object.AddInputField(*gp_fixargs(args)))
RuntimeError: FieldMap: Error in adding input field to field map

 

Specifically, it's this line (119) that's causing issues:

field_to_map.addInputField(append_layer, field_map[1])

 

If you noticed, I remarked out my multi fields to let the list just take the one field match combo that worked in the simpler, non-list script.

Still throws this error.

 

I'm confused how this list loops works.   Not sure how it's figuring out position 0,1 with the  case like

('Addy_Concat', 'PropertyAddress')

 

Any ideas what I'm doing wrong here? Or what I can do to try accomplish what I want to do?

There must be an easier way to field match when appending.

Outcomes