I'm performing a Spatial Join with a polygon (target) and a point (joinFeature)- which has 5 fields in it that I need to "sum". I have written a Spatial Join and (after many long days) gotten the field mapping to work! But this time is different because I am using a Merge Rule.
Here is my code, right now it breaks at the spatial join and outputs an empty feature class.
Also, this is in a Python Toolbox and uses Parameters to get the target and joinFeature. Like I said, I have gotten this to work before without a merge rule and with different fields…
# Create a new fieldmappings and add the two input feature classes.
fms = arcpy.FieldMappings()
fms.addTable(target)
fms.addTable(joinFeature)
# Create the required FieldMap and FieldMappings objects
fm_apt_poly_APT_ID = arcpy.FieldMap()
fm_stud_ELEM = arcpy.FieldMap()
fm_stud_INT = arcpy.FieldMap()
fm_stud_MID = arcpy.FieldMap()
fm_stud_HIGH = arcpy.FieldMap()
fm_stud_TOT_STUD = arcpy.FieldMap()
fms = arcpy.FieldMappings()
# Get the field names for both original files
apt_poly_APT_ID = "APT_ID"
stud_ELEM = "ELEM"
stud_INT = "INT"
stud_MID = "MID"
stud_HIGH = "HIGH"
stud_TOT_STUD = "TOT_STUD"
# Add fields to their corresponding FieldMap objects
fm_apt_poly_APT_ID.addInputField(target, "APT_ID")
fm_stud_ELEM.addInputField(joinFeature, "ELEM")
fm_stud_INT.addInputField(joinFeature, "INT")
fm_stud_MID.addInputField(joinFeature, "MID")
fm_stud_HIGH.addInputField(joinFeature, "HIGH")
fm_stud_TOT_STUD.addInputField(joinFeature, "TOT_STUD")
# Set the output field properties for both FieldMap objects
apt_poly_APT_ID = fm_apt_poly_APT_ID.outputField
apt_poly_APT_ID.name = "APT_ID"
fm_apt_poly_APT_ID.outputField = apt_poly_APT_ID
stud_ELEM = fm_stud_ELEM.outputField
stud_ELEM.name = "ELEM"
stud_ELEM.aliasName = "ELEM"
fm_stud_ELEM.outputField = stud_ELEM
stud_INT = fm_stud_ELEM.outputField
stud_INT.name = "INT"
stud_INT.aliasName = "INT"
fm_stud_INT.outputField = stud_INT
stud_MID = fm_stud_MID.outputField
stud_MID.name = "MID"
stud_MID.aliasName = "MID"
fm_stud_MID.outputField = stud_MID
stud_HIGH = fm_stud_HIGH.outputField
stud_HIGH.name = "HIGH"
stud_HIGH.aliasName = "HIGH"
fm_stud_HIGH.outputField = stud_HIGH
stud_TOT_STUD = fm_stud_TOT_STUD.outputField
stud_TOT_STUD.name = "TOT_STUD"
stud_TOT_STUD.aliasName = "TOT_STUD"
fm_stud_TOT_STUD.outputField = stud_TOT_STUD
# Set the merge rule to sum and then replace the old field map in the mappings object with the updated one
fm_stud_ELEM.mergeRule = "SUM"
fm_stud_INT.mergeRule = "SUM"
fm_stud_MID.mergeRule = "SUM"
fm_stud_HIGH.mergeRule = "SUM"
fm_stud_TOT_STUD.mergeRule = "SUM"
fms.addFieldMap(fm_apt_poly_APT_ID)
fms.addFieldMap(fm_stud_ELEM)
fms.addFieldMap(fm_stud_INT)
fms.addFieldMap(fm_stud_MID)
fms.addFieldMap(fm_stud_HIGH)
fms.addFieldMap(fm_stud_TOT_STUD)
# Spatial Join Sample Code
# SpatialJoin_analysis (target_features, join_features, out_feature_class, {join_operation}, {join_type}, {field_mapping}, {match_option}, {search_radius}, {distance_field_name})
# Process: Spatial Join
arcpy.SpatialJoin_analysis(target, joinFeature, output, "JOIN_ONE_TO_ONE", "KEEP_ALL", fms, "", "INTERSECT", "")
arcpy.AddMessage("Just performed Spatial Join")
Best Answer
The solution to my problem is a frustrating one but it works so i'm ok with it. I ended up deleting all of the fields in my target and join feature except the ones that I needed to keep. Then field mapped those with a merge rule of "Sum" (see below) and then performed a table join, two field calculations and changed Null values to 0. Here is my code that works: