Here is my code:
I keep getting the error: "FieldMap: Error in adding input field to field map." This is my first time using field mapping, and I'm a little confused.
point="T:/GIS/Projects/GIS/WorkOrderTools/MapDocuments/WorkOrderTesting/Default.gdb/WO"+workOrder+"/point"
consumer="C:/Users/azumwalt.UECOOP/AppData/Roaming/ESRI/Desktop10.4/ArcCatalog/ElectricModel - azumwalt.sde/consumer"
fm_mapname = arcpy.FieldMap()
fm_phasing = arcpy.FieldMap()
fms = arcpy.FieldMappings()
fm_mapname.addInputField(point, "WMMAPNAME")
fm_phasing.addInputField(point, "WMPHASING")
type_mapname = fm_mapname.outputField
type_mapname.name = "esMapName"
fm_mapname.outputField = type_mapname
type_phasing = fm_phasing.outputField
type_phasing.name = "esPhasing"
fm_phasing.outputField = type_phasing
fms.addFieldMap(fm_mapname)
fms.addFieldMap(fm_phasing)
arcpy.Append_management(point, consumer, "", fms, "1")
Solved! Go to Solution.
import arcpy
# Work Order Number
workOrder = "1103612"
# Name the Append and Target Layer (target layer is named in the ArcGIS Pro Map)
append_layer="T:/GIS/Projects/GIS/WorkOrderTools/MapDocuments/WorkOrderTesting/Default.gdb/WO"+workOrder+"/point"
target_layer="Consumer_SQL02"
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(('esMapName', 'WMMAPNAME'))
list_of_fields_we_will_map.append(('esPhasing', 'WMPHASING'))
list_of_fields_we_will_map.append(('esUplineFeeder', 'FEEDER'))
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")
So, after changing my code completely, it turns out I had my field names reversed, needed to add "1" as subtype to the Append_management tool, then it worked! I also finally figured out how to post code using the advanced editor button.
I had a hard time understanding field mapping until someone posted a code.
I use the code below and works great.
import arcpy,sys
from datetime import datetime as d
startTime = d.now()
start_time = time.time()
PT1 = "C:/Temp/Scratchworkspace.gdb/Drain"
FieldMapString = '''
DXF_TEXT "DXF_TEXT" true true false 11 Text 0 0 ,First,#, {0}, DXF_TEXT,-1,-1;
ACCOUNT "ACCOUNT" true true false 11 Text 0 0 ,First,#, {0}, ACCOUNT,-1,-1;
PIN "PIN" true true false 13 Text 0 0 ,First,#, {0}, PIN,-1,-1;
ACRES "ACRES" true true false 4 Double 0 0 ,First,#, {0}, ACRES,-1,-1;
Instrument "Instrument" true true false 10 Text 0 0 ,First,#, {0}, Instrument,-1,-1;
SiteAddres "SiteAddres" true true false 106 Text 0 0 ,First,#, {0}, SiteAddres,-1,-1;
SiteCity "SiteCity" true true false 32 Text 0 0 ,First,#, {0}, SiteZip,-1,-1;
SiteZip "SiteZip" true true false 10 Text 0 0 ,First,#, {0}, SiteZip,-1,-1
'''
def Layers1(PT1):
"""pass the variable and the constant into
the function
"""
fieldmappings = arcpy.FieldMappings()
fieldmappings.loadFromString(FieldMapString)
return fieldmappings
def main(args=None):
if args is None:
args = sys.argv
# Process: Feature Class to Feature Class
arcpy.Append_management(PT1, "C:\Temp\Scratchworkspace.gdb\PT_ALL","NO_TEST", Layers1(PT1))
try:
print ('(Elapsed time: ' + str(d.now() - startTime)[:-3] + ')')
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print ("Line %i" % tb.tb_lineno)
print (e.message)
Thanks! Can you tell me what each field is for in this string?
DXF_TEXT "DXF_TEXT" true true false 11 Text 0 0 ,First,#, {0}, DXF_TEXT,-1,-1;
I am not sure what all of it means but these are the ones i believe to know and i could be wrong. Maybe and hopefully someone else can answer what i am missing.
Dan Patterson helped me update my field mapping, i had an older version of it. Maybe he can shine some light...
Name = DXF_TEXT
Alias = DXF_TEXT
True = Editable
True = Non Editable
False = required
Length = 11
scale = 0
Precision = 0
Merge rule = First
join Delimiter = # (meaning ' ' , Null string)
? = {0}
Data source field = DXF_TEXT
? = -1
? = -1
I think the second "True" (where you have non-editable) is actually "nullable", and the last 2 items (both -1) is the start text and end text positions.
See also:
Thanks! So is this a list of fields for the data being appended, or the data that's being added to?
I found this, which looked easy to understand: Append Tool and Field Mapping ... Help? Examples? But my code is breaking when I go to
field_to_map.addInputField(append_layer, field_map[1])
I think I'm pretty close...any ideas?
# Delete this line
workOrder = "1103612"
append_layer="T:/GIS/Projects/GIS/WorkOrderTools/MapDocuments/WorkOrderTesting/Default.gdb/WO"+workOrder+"/point"
target_layer="T:/GIS/Projects/GIS/WorkOrderTools/MapDocuments/WorkOrderTesting/ElectricModel - azumwalt.sde/ElectricModel.DBO.UEC/ElectricModel.DBO.Consumer"
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 = []
# Lets chuck some tuples into the list we made
list_of_fields_we_will_map.append(('WMMAPNAME', 'esMapName'))
list_of_fields_we_will_map.append(('WMPHASING', 'esPhasing'))
list_of_fields_we_will_map.append(('FEEDER', 'esUplineFeeder'))
for field_map in list_of_fields_we_will_map:
# Find the fields index by name. e.g 'TaxPin'
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
#(THIS IS WHERE MY CODE BREAKS RuntimeError: FieldMap: Error in adding input field to field map)
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
inData = [append_layer]
arcpy.Append_management(inData, target_layer, "NO_TEST", fieldmappings)
import arcpy
# Work Order Number
workOrder = "1103612"
# Name the Append and Target Layer (target layer is named in the ArcGIS Pro Map)
append_layer="T:/GIS/Projects/GIS/WorkOrderTools/MapDocuments/WorkOrderTesting/Default.gdb/WO"+workOrder+"/point"
target_layer="Consumer_SQL02"
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(('esMapName', 'WMMAPNAME'))
list_of_fields_we_will_map.append(('esPhasing', 'WMPHASING'))
list_of_fields_we_will_map.append(('esUplineFeeder', 'FEEDER'))
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")
So, after changing my code completely, it turns out I had my field names reversed, needed to add "1" as subtype to the Append_management tool, then it worked! I also finally figured out how to post code using the advanced editor button.
Thank you so much for this! Solved my problem after about 2 months of trying thanks to this code!
As an alternative to field mapping (and for future reference), look at Xander Bakker's code near the bottom of this thread: FieldMap and FieldMappings arrgh....
Haha I like the title. I'll have to try that, it looks simpler. Thanks!