germ813

Issue with basic field mappings for spatial join

Discussion created by germ813 on Sep 10, 2013
Latest reply on Sep 10, 2013 by germ813
Hi all,

My script is working except the field mappings. When I remove that parameter from the spatial join, it runs fine (but doesn't include the desired field in the output). When I add the fieldMappings parameter to the spatial join, the script throws the beloved ERROR 999999: Error executing function.

Thus, I know the problem isolated to how I am imputing the fieldMappings parameter which also makes me believe it is a basic misunderstanding on my part regarding how to utilize field mappings.

Here is a code snippet of the issue:

mapFields = ["ITEM_ID", "PRIME_GEO"]
fieldMappings = arcpy.FieldMappings()
for mapField in mapFields:
    fieldMap = arcpy.FieldMap()
    fieldMap.addInputField(aoi, mapField)
    fieldMappings.addFieldMap(fieldMap)

arcpy.SpatialJoin_analysis(aoi, "buildingLyr", buildingOutput, "JOIN_ONE_TO_ONE", "KEEP_COMMON", fieldMappings, "COMPLETELY_WITHIN", "", "")


PS. Of the two tables involved in the join, I only really need to retain the ITEM_ID (and resulting count) field after the join. If there is a better way to do this than field mapping, please share your idea!

Outcomes