tmakibbin

Append Feature Class to blank file GDB FC using field mapping control

Discussion created by tmakibbin on Mar 4, 2011
We have existing personal geodatabases and we have decided that the FCs in them need fields updated by either removing unused field or adding additional fields.  I have created a blank file GDB with the same blank FC names but with the new updated field. I am trying to write a script that will take the name of the existing FC and apply the correct fieldmappings for the Append_managment tool.  In the example below, the existing FC had a field named "AREA".  In the new GDB the field name is "ACRES".  Additionally, the new GDB FC adds two new fields (NOTES, CURRENT_).  Does this code look right?  Also, how do I field map  fields that are no longer used?  Do I simply use the "fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("<name of field>")?

import arcpy, os
Path = arcpy.GetParameterAsText(0)
fcName = arcpy.GetParameterAsText(1)
Source = arcpy.GetParameterAsText(2)
outfc = Source + Path + "/NEW_SGL.gdb/" + fcName
schemaType = "NO_TEST"
subtype = ""
#
fc1 = Source + Path + "/SGL.mdb/" + fcName
emptyFC = Source + Path + "/NEW_SGL.gdb/" + fcName

# Create a new fieldmappings and add the two input feature classes.
#
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(emptyFC)
fieldmappings.addTable(fc1)

if fcName == "Abandoned_Mine":
# First get the ACRES fieldmap. Then add the AREA field
#   from Blocks2 as an input field. Then replace the fieldmap within
#   the fieldmappings object.
#
fldmap_NOTES = arcpy.FieldMap()
fldmap_CURRENT_ = arcpy.FieldMap()
fieldmap = fieldmappings.getFieldMap(fieldmappings.findFieldMapIndex("ACRES"))
fieldmap.addInputField(emptyFC, "AREA")
fieldmappings.replaceFieldMap(fieldmappings.findFieldMapIndex("ACRES"), fieldmap)
# Add new fields.
fldmap_NOTES.addInputField(fc1)
fldmap_CURRENT_.addInputField(fc1)
fld_NOTES = fldmap_NOTES.outputField
fld_NOTES.name = "NOTES"
fldmap_NOTES.outputField = fld_NOTES
fld_CURRENT_ = fldmap_CURRENT_.outputField
fld_CURRENT_.name = "CURRENT_"
fldmap_CURRENT_.outputField = fld_CURRENT_

# Add the new fieldmap object to fieldmappings object.
fieldmappings.addFieldMap(fldmap_NOTES)
fieldmappings.addFieldMap(fldmap_CURRENT_)

# Remove the AREA fieldmap.
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("AREA"))
#
# Create a value table that will hold the inputs for Merge.
arcpy.Append_management(fc1, outfc, schemaType, fieldMappings, subtype)

Outcomes