The field mapping object fails to return the correct ouput in the Spatial Join tool if both inputs contain field names with the same name specified in the field mapping object.
Last Published: August 25, 2014ArcGIS for Desktop
Bug ID Number
NIM071958
Submitted
August 24, 2011
Last Modified
June 5, 2024
Applies to
ArcGIS for Desktop
Version found
10.0
Status
Duplicate
The issue is a duplicate of an existing issue. See the issue's Additional Information section for details. Customers associated with the duplicate issue are automatically attached to the open issue.
Additional Information
NIM071321
Workaround
Create an in_memory copy of the input polygon feature class, delete the identical field used in the field mapping object, and then pass it into the spatial join.Code example below:# import modulesimport arcpy, os# Data: Specify location of test datahome = arcpy.GetParameterAsText(0) #r"<a href="file:C:/ArcGIS" target="_blank">C:\ArcGIS</a> Testing\Python Testing\957244 - Field Mapping"# Data: Local VariablespolygonFC = home + os.sep + "Polygons.shp" pointFC = home + os.sep + "Points.shp"idFieldName = "regID"scratchSJshape = "temp_sj.shp"scratchDirectory = home + os.sep + "Scratch" + os.sep# Process: Check if scratch folder exists and delete itif arcpy.Exists(scratchDirectory[:-1]): arcpy.Delete_management(scratchDirectory[:-1])# Process: Create scratch folderarcpy.CreateFolder_management(home, "Scratch") # Process: Create Field Mappings ObjectfieldMappings = arcpy.FieldMappings()index = fieldMappings.findFieldMapIndex(idFieldName)# Process: Populate Field Mapping ObjectnewFieldMap = arcpy.FieldMap()newFieldMap.addInputField(pointFC, idFieldName)newField = arcpy.Field()newField.type = "String"<a href="http://newField.name" target="_blank">newField.name</a> = "TestField"newField.aliasName = "TestField"newField.editable = 1newField.isNullable = 1newField.length = 250newFieldMap.mergeRule = "Join"newFieldMap.joinDelimiter = ", "newFieldMap.outputField = newFieldfieldMappings.addFieldMap(newFieldMap)# Process: Create polygon layer in memory and remove id fieldmemPoly = r"in_memory\polyFeature"arcpy.CopyFeatures_management(polygonFC, memPoly)arcpy.DeleteField_management(memPoly, idFieldName)# Process: Spatial join the point and polgon featuresarcpy.SpatialJoin_analysis(memPoly, pointFC, scratchDirectory + scratchSJshape, "JOIN_ONE_TO_ONE", "KEEP_ALL", fieldMappings, "INTERSECTS")# Process: Return output to model/displayarcpy.SetParameter(1, scratchDirectory + scratchSJshape)# Process: Delete in_memory featurearcpy.Delete_management(memPoly)