3

I am trying to use FieldMap in arcpy to merge only a few of the same fields from 8 feature classes. There are many different fields in all of the different feature classes, but I have identified the 9 fields I want in my final merged feature class. Instead of merging all the fields I identify it only chooses the first field in my list and I end up with a merged file with one field "COUNTY_F". What am I missing?

fClasses = []
i=0

#Loop through MyGDB.gdb and add each feature class name to array
fcs = arcpy.ListFeatureClasses()

for fc in fcs:   
    fClasses.append(fc)
    i = i+1

print fClasses
#Shows as "Clallam_WorkingForest_2013, GraysHarbor_WorkingForest_2013, Jefferson_WorkingForest_2013, King_WorkingForest_2013, Kittitas_WorkingForest_2013, Mason_WorkingForest_2013, Pierce_WorkingForest_2013, Snohomish_WorkingForest_2013"

#Map Fields for Merge
inFields= ["COUNTY_F", "UNIQUE_ID", "TAXPAYER", "PARCEL_ID", "CURRENTUSE_CODE", "CURRENTUSE_DESC", "PRESENTUSE_CODE", "PRESENTUSE_DESC", "ACRES_ASSES"]

fm = arcpy.FieldMappings()
fm_type = arcpy.FieldMap()

for field in inFields:
    fm_type.addInputField( "Clallam_WorkingForest_2013", field.name)
    fm_type.addInputField( "GraysHarbor_WorkingForest_2013", field.name)
    fm_type.addInputField( "Jefferson_WorkingForest_2013", field)
    fm_type.addInputField( "King_WorkingForest_2013", field)
    fm_type.addInputField( "Kittitas_WorkingForest_2013", field)
    fm_type.addInputField( "Mason_WorkingForest_2013", field)
    fm_type.addInputField( "Pierce_WorkingForest_2013", field)
    fm_type.addInputField( "Snohomish_WorkingForest_2013", field)

fm.addFieldMap(fm_type)
print "Fields mapped!"

arcpy.Merge_management(fClasses, "Merged", fm)
print "Merge: SUCCESS."
1
  • 1
    It's because you only use field.name for your first two iterations of fm_type.addInputField, and field for the rest perhaps? Commented Jan 8, 2015 at 0:27

1 Answer 1

1

Is probably your understanding about FieldMap is wrong. Because one field relation with one FieldMap object,so you need create nine FieldMap object in your case.In addition,inFields is a list,you can not use field.name.

Modify the code as follows,I think that the result you want to obtain.

import arcpy
fClasses = []
i=0

#Loop through MyGDB.gdb and add each feature class name to array
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    fClasses.append(fc)
    i = i+1

print fClasses
#Shows as "Clallam_WorkingForest_2013, GraysHarbor_WorkingForest_2013, Jefferson_WorkingForest_2013, King_WorkingForest_2013, Kittitas_WorkingForest_2013, Mason_WorkingForest_2013, Pierce_WorkingForest_2013, Snohomish_WorkingForest_2013"

#Map Fields for Merge
inFields= ["COUNTY_F", "UNIQUE_ID", "TAXPAYER", "PARCEL_ID", "CURRENTUSE_CODE", "CURRENTUSE_DESC", "PRESENTUSE_CODE", "PRESENTUSE_DESC", "ACRES_ASSES"]

fm = arcpy.FieldMappings()
fm_types =[]
for i in range(9):
    fm_types.append(arcpy.FieldMap())

for fm_type in fm_types:
    for fClass in fClasses:
        for filed in inFields:
            fm_type.addInputField(fClass,field)
            outField=fm_type.outputField
            outField.name=field
            fm_type.outputField=outField
            #Sequentially add nine FieldObject
            fm.addFieldMap(fm_type)

print "Fields mapped!"

arcpy.Merge_management(fClasses, "Merged", fm)
print "Merge: SUCCESS."

I am sorry,I was wrong to write loop.

import arcpy
fClasses = []
i=0

#Loop through MyGDB.gdb and add each feature class name to array
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    fClasses.append(fc)
    i = i+1

print fClasses
#Shows as "Clallam_WorkingForest_2013, GraysHarbor_WorkingForest_2013, Jefferson_WorkingForest_2013, King_WorkingForest_2013, Kittitas_WorkingForest_2013, Mason_WorkingForest_2013, Pierce_WorkingForest_2013, Snohomish_WorkingForest_2013"

#Map Fields for Merge
inFields= ["COUNTY_F", "UNIQUE_ID", "TAXPAYER", "PARCEL_ID", "CURRENTUSE_CODE", "CURRENTUSE_DESC", "PRESENTUSE_CODE", "PRESENTUSE_DESC", "ACRES_ASSES"]

fm = arcpy.FieldMappings()
for i in range(9):
    fm_type=arcpy.FieldMap()
    for fClass in fClasses:
        fm_type.addInputField(fClass,inFields[i])
        outField=fm_type.outputField
        outField.name=inFields[i]
        fm_type.outputField=outField
    #Sequentially add nine FieldObject
    fm.addFieldMap(fm_type)

print "Fields mapped!"

arcpy.Merge_management(fClasses, "Merged", fm)
print "Merge: SUCCESS."
2
  • I tried to play around with this code, but it just keeps creating many fields for the one field "Count_F". Commented Jan 8, 2015 at 21:47
  • @TJ if one of your answers is wrong, you should just remove it.
    – ianbroad
    Commented Jan 9, 2015 at 6:13

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.