jmiller121

Using FieldMap to merge feature classes together

Discussion created by jmiller121 on Nov 12, 2011
Latest reply on Nov 14, 2011 by hpbrantley
I have Centerlines from different counties that I want to merge together, but I only want about 25 out of 90 fields available.  I would like to define my field map in an array (as shown) so it's easy to adjust based on different counties field layouts.

Everything in my code works good except when I try to add the FieldMaps from the second feature class.  Instead of having one field named "STR_NAME", there is a "STR_NAME" (from the first FC) and a "STR_NAME_1" (from the second FC)

In the attachment, I've included both input feature classes and the output feature class result.  A look at the field names should give an indication to the problem I'm having... Any advice would be appreciated..

Here is my python code...(viewing in fixed width font is better...)
import arcpy
from arcpy import env

try:

    arcpy.env.overwriteOutput = True

    vt = arcpy.ValueTable()     # holds list of featureclasses to merge
    fieldmappings = arcpy.FieldMappings()

# ---------------------------------------------------
# Start Field Map for first feature class (Centerlines1.shp)
# ---------------------------------------------------
    featureClass = r"C:\Centerlines1.shp"
    fcFieldMaps = []
    # The index for the field maps array is:  0 = InputField, 1 = OutputFieldName, 2 = FieldType, 3 = FieldLength
    # A  !!!  after the field entry indicates that OutputFieldName is not the same as the InputField
    # A field that is commented out indicates that there is no InputField for the field name
    fcFieldMaps.append(["STR_PRE"     , "STR_PRE"     , "String"   , 2])
    fcFieldMaps.append(["STR_NAME"    , "STR_NAME"    , "String"   , 30])
    fcFieldMaps.append(["STR_TYPE"    , "STR_TYPE"    , "String"   , 4])
    fcFieldMaps.append(["STR_SUFFIX"  , "STR_SUFFIX"  , "String"   , 2])
#    fcFieldMaps.append(["STR_SUFIX2"  , "STR_SUFIX2"  , "String"   , 2])
    fcFieldMaps.append(["L_ADD_FROM"  , "L_ADD_FROM"  , "Long"     , 0])
    fcFieldMaps.append(["L_ADD_TO"    , "L_ADD_TO"    , "Long"     , 0])
    fcFieldMaps.append(["R_ADD_FROM"  , "R_ADD_FROM"  , "Long"     , 0])
    fcFieldMaps.append(["R_ADD_TO"    , "R_ADD_TO"    , "Long"     , 0])
    fcFieldMaps.append(["LSN"         , "LSN"         , "String"   , 64])
    fcFieldMaps.append(["L_COMM"      , "L_CITY"      , "String"   , 30])   # !!!
    fcFieldMaps.append(["R_COMM"      , "R_CITY"      , "String"   , 30])   # !!!

    for field in fcFieldMaps:
        fldmap = arcpy.FieldMap()
        fldmap.addInputField(featureClass, field[0])
        fld = fldmap.outputField
        fld.name = field[1]
        fld.type = field[2]
        fld.length = field[3]
        fldmap.outputField = fld
        fieldmappings.addFieldMap(fldmap)

    vt.addRow(featureClass)
# ---------------------------------------------------
# End Field Map for first feature class (Centerlines1.shp)
# ---------------------------------------------------

# ---------------------------------------------------
# Start Field Map for second feature class (Centerlines2.shp)
# ---------------------------------------------------
    featureClass = r"C:\Centerlines2.shp"
    fcFieldMaps = []
    # The index for the field maps array is:  0 = InputField, 1 = OutputFieldName, 2 = FieldType, 3 = FieldLength
    # A  !!!  after the field entry indicates that OutputFieldName is not the same as the InputField
    # A field that is commented out indicates that there is no InputField for the field name
    fcFieldMaps.append(["STR_PRE"     , "STR_PRE"     , "String"   , 2])
    fcFieldMaps.append(["STR_NAME"    , "STR_NAME"    , "String"   , 30])
    fcFieldMaps.append(["STR_TYPE"    , "STR_TYPE"    , "String"   , 4])
    fcFieldMaps.append(["STR_SUFFIX"  , "STR_SUFFIX"  , "String"   , 2])
#    fcFieldMaps.append(["STR_SUFIX2"  , "STR_SUFIX2"  , "String"   , 2])
    fcFieldMaps.append(["L_ADD_FROM"  , "L_ADD_FROM"  , "Long"     , 0])
    fcFieldMaps.append(["L_ADD_TO"    , "L_ADD_TO"    , "Long"     , 0])
    fcFieldMaps.append(["R_ADD_FROM"  , "R_ADD_FROM"  , "Long"     , 0])
    fcFieldMaps.append(["R_ADD_TO"    , "R_ADD_TO"    , "Long"     , 0])
    fcFieldMaps.append(["LSN"         , "LSN"         , "String"   , 64])
    fcFieldMaps.append(["L_COMM"      , "L_CITY"      , "String"   , 30])   # !!!
    fcFieldMaps.append(["R_COMM"      , "R_CITY"      , "String"   , 30])   # !!!

    for field in fcFieldMaps:
        fldmap = arcpy.FieldMap()
        fldmap.addInputField(featureClass, field[0])
        fld = fldmap.outputField
        fld.name = field[1]
        fld.type = field[2]
        fld.length = field[3]
        fldmap.outputField = fld
        fieldmappings.addFieldMap(fldmap)

    vt.addRow(featureClass)
# ---------------------------------------------------
# End Field Map for second feature class (Centerlines2.shp)
# ---------------------------------------------------

    outputFeatureClass = r"C:\CenterlinesMerge.shp"
    arcpy.Merge_management(vt, outputFeatureClass, fieldmappings)

except Exception, err:
    print str(err)

def main():
    pass

if __name__ == '__main__':
    main()

Attachments

Outcomes