I'm trying to automate importing census boundary shapefiles to feature class in a Geodatabase File, but somehow could not get it to work with this code:

### ======================== load boundary map ===============================

outLocation = "C:/python/RA506Final/RA506FinalPro.gdb"
outFeatureClass = "RamBoundaryMap"
inputTable = "tl_2010_27123_tract10.shp"

if arcpy.Exists(inputTable):
    # List of field headers to convert
    inFields = ['FID', 'Shape', 'GEOID10']
    fm= arcpy.FieldMappings()
    fieldMap= arcpy.FieldMap()
    for field in inFields:
        fieldMap.addInputField(inputTable, field)
    #creates a fieldmap with name of input field
        vars()[field] = arcpy.FieldMap()

    #convert it now
    arcpy.FeatureClassToFeatureClass_conversion(inputTable, outLocation, outFeatureClass,field_mapping=fm)

    # Confirm Ramsey/Hennepin Boundary map shapefiles imported into Geodatabase file
    x = arcpy.ListFeatureClasses("*", "polygon")
    for i in x:
        print i
    print inputTable + " does not exist"

print arcpy.GetMessage

Here is the error message:

FieldMappings: Error in adding a field map to field mappings <function GetMessage at 0x0C4E8330>

  • 4
    Are you getting an error message, if so what is it? – artwork21 Jul 30 '14 at 12:34
  • Here is the error message: FieldMappings: Error in adding a field map to field mappings <function GetMessage at 0x0C4E8330> – Abdillahi Jibril Jul 30 '14 at 23:50

Your FieldMap objects need to be created independently for each unique field (otherwise you'll end up with three output fields with the same name). Your script was using the same variable fieldMap each time through the loop and writing it to the main FieldMappings object, and then initiating but not defining a unique new FieldMap for each field.

inputTable = "tl_2010_27123_tract10.shp"
inFields = ['FID', 'Shape', 'GEOID10'] #case-sensitive

fm = arcpy.FieldMappings()
for field in inFields:
    vars()[field] = arcpy.FieldMap()
    vars()[field].addInputField(inputTable, field)

I personally hate working with FieldMap/FieldMappings objects, as I find them very tedious to construct correctly. I would normally do this by calling:

  1. CreateFeatureclass_management(),
  2. AddField_management() for the fields you want (just "GEOID10" in this case, since the OID and geometry are initialized with the feature class), being careful to use the same field type/length as the original, and
  3. Append_management() with schema_type='NO_TEST'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.