3

I'm having trouble trying to understand how field mapping works in ArcPy. I'm relatively new to Python.

In the below example, I want to take a shapefile and append it to a feature class but I want to use the field map parameter so the fields from the shapefile go to the correct field names in the feature class.

I successfully built this out in ModelBuilder and exported the Python script (see below) but so much of it doesn't make sense.

For example, what are all these different codes... -1, 0, #, true, false, etc?

There seems to be no documentation anywhere online on the meaning behind these codes.

I read that I can use the fieldmapping() class as a parameter in this script but have been unable to incorporate that?

If you know a much easier way (via python) to field map then using the above methods, I'm open to those suggestions.

The code below is only a simple test that I created, so I can hopefully understand field mapping better but in the long run I want to this to be able to handle a LOT of fields from many different shapefiles.

Source shapefile schema: Name, Code, Date

Target feature class schema: Description, Num, Date

# Import arcpy module
import arcpy


# Local variables:
New_Shapefile_shp = "C:\\New_Shapefile.shp"
New_Featureclass__2_ = New_Shapefile_shp
New_Featureclass = "C:\\Target.gdb\\New_Featureclass"

# Process: Append
arcpy.Append_management("C:\\New_Shapefile.shp", New_Featureclass, "NO_TEST", "DESCRIPTION \"DESCRIPTION\" true true false 150 Text 0 0 ,First,#,C:\\New_Shapefile.shp,Name,-1,-1;NUM \"NUM\" true true false 4 Long 0 0 ,First,#,C:\\New_Shapefile.shp,Code,-1,-1;DATE \"DATE\" true true false 8 Date 0 0 ,First,#,C:\\New_Shapefile.shp,Date,-1,-1", "")
0

2 Answers 2

9

Another way of doing it using da.SearchCursor to read shapefile rows and insert them in the fc using da.InsertCursor:

import arcpy

shapefile = r'C:\folder\shapefile.shp'
fc = r'C:\data.gdb\featureclass'

#The position of the fields must match, Name=Description etc.
shapefields = ['Name','Code','Date']
fcfields = ['Description','Num','Date']

icur = arcpy.da.InsertCursor(fc, fcfields+['SHAPE@'])

with arcpy.da.SearchCursor(shapefile, shapefields+['SHAPE@']) as cursor:
    for row in cursor:
        icur.insertRow(row)

del icur
0
6

Field mappings are painful, and to be avoided where possible. Therefore @BERA's alternative is a good method, and perhaps better than this one, but for the sake of completeness, here's how I use field mappings...

I have written a Python function that will generate a field mapping object simply by passing it the input feature class (or table) and a dictionary of source-field-names as keys and target-field-names as values. The function returns a field mapping object that can be used in an Append, etc:

def fmapForDict(inputDataset, mappingDict):
    fieldMappings = arcpy.FieldMappings()

    for sourceField in mappingDict:
        fMap = arcpy.FieldMap()
        fMap.addInputField(inputDataset, sourceField)
        outField = fMap.outputField
        outField.name = mappingDict[sourceField]
        fMap.outputField = outField
        fieldMappings.addFieldMap(fMap)

    return fieldMappings

NB: If your source fields and target fields all have the same names, then YOU DO NOT HAVE TO SUPPLY ANY FIELD MAPPINGS AT ALL. The arcpy.Append_management() function with a 'NO_TEST' argument, will automatically map any fields with matching field names (where possible).

PS: The field mappings object returned by this function does not include merge rules, so the default merge rules would apply.

2

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.