Arcpy Field Mappings

I always get frustrated with the extra long field list in arcpy tools, such as this one:

arcpy.TableToTable_conversion(SourceTable, 
                              TargetDB, 
                              "xxOutputTable", 
                              "", 
                              "RecordID \"RecordID\" true false false 8 Double 0 19 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordID,-1,-1;Descript \"Descript\" true true false 65 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Descript,-1,-1;Status \"Status\" true true false 30 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Status,-1,-1;RecordName \"RecordName\" true true false 50 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordName,-1,-1;Owner \"Owner\" true true false 2000 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Owner,-1,-1;", 
                              "")

I struggle with them when exporting or joining or well anything that requires listing fields in the arcpy tools.  It was suggested to me to look into arcpy.fieldmappings()  to try to find a solution, and this is what I’ve come up with (in this case for a Table to Table but can be used in same/similar way for any other tool that requires field maps.

What it does is take each component of a field, in a very easy to read list, and passes it through a short module to create a field map of all the required fields.  It may seem a bit long winded, but it is much easier to read and makes it straightforward if you actually want to modify any of your fields.

import arcpy
 
def generateFieldMapping(fms, fieldName, fieldType, fieldLength, fieldPrecision, fieldScale, fieldAlias):
    # Create the FieldMap object
    fm = arcpy.FieldMap()
    fm.addInputField(SourceTable, fieldName)
    fm_field = fm.outputField
 
    # Set the different properties of the field
    fm_field.name = fieldName
    fm_field.aliasName = fieldAlias
    fm_field.type = fieldType
    if fieldType == "Text":
        fm_field.length = fieldLength
    if fieldType == "Integer" or fieldType == "Double":
        fm_field.precision = fieldPrecision
    if fieldType == "Double":
        fm_field.scale = fieldScale
 
    fm.outputField = fm_field
 
    # Add FieldMap to the FieldMappings object
    fms.addFieldMap(fm)
 
    return fms
 
SourceDB = "Database Connections\\GISDatabase.sde"
TargetDB = "Database Connections\\GISDatabase.sde"
 
# Local variables:
SourceTable = SourceDB + "\\GISDatabase.dbo.v_InputTable"
 
fields = [
          # [Field Name, Field type, Length, Precision, Scale, Field Alias]
            ["RecordID","Double",0,19,0,"RecordID"],
            ["Descript","Text",65,0,0,"Descript"],
            ["Status","Text",30,0,0,"Status"],
            ["RecordName","Text",50,0,0,"RecordName"],
            ["Owner","Text",2000,0,0,"Owner"],
         ]
 
# Create the FieldMappings objects
fms = arcpy.FieldMappings()
 
for field in fields:
    generateFieldMapping(fms, field[0], field[1], field[2], field[3], field[4], field[5])
 
arcpy.TableToTable_conversion(SourceTable, TargetDB, "xxOutputTable", "", fms, "")

Leave a Reply

%d bloggers like this: