0

I'm trying to spatial join two features. One polygon layer and one point layer. The point layer contains one field of values that I want the Mean and Min of. Im trying to use the fieldmaper object to set the merge rules for two fields ("Mean" and "Max").

I get this error "RuntimeError: FieldMappings: Error in adding table to field mappings" while running it in python for ArcMap. How can I fix this?

falling_tree = arcpy.env.workspace+'\\'+ 'Falling_tree_3_meter.shp'

buffer_area = arcpy.env.workspace+'\\'+'Buffer_erased'

arcpy.AddField_management(buffer_area,"Max", "DOUBLE")
arcpy.AddField_management(buffer_area,"Mean", "DOUBLE")

targetFeatures = buffer_area 
joinFeatures = falling_tree
outputFeatures = arcpy.env.workspace+'\\'+'Joine_tre'


fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(buffer_area)
fieldmappings.addTable(falling_tree)

MaxFieldIndex = fieldmappings.findFieldMapIndex("Max")
fieldmap1 = fieldmappings.getFieldMap(MaxFieldIndex)
fieldmap1.mergeRule = 'MIN'

MeanFieldIndex = fieldmappings.findFieldMapIndex("Mean")
fieldmap2 = fieldmappings.getFieldMap(MeanFieldIndex)
fieldmap2.mergeRule = 'MEAN'

fieldmappings.addFieldMap(fieldmap1)
fieldmappings.addFieldMap(fieldmap2)

arcpy.SpatialJoin_analysis(buffer_area,falling_tree, 
arcpy.env.workspace+'\\'+'Joine_tre', "JOIN_ONE_TO_ONE", "KEEP_ALL", fieldmappings, "INTERSECT")
  • 1
    Field mappings are difficult to get right. As an alternative you could Intersect-Summary Statistics-Join results back to the polygons – BERA Jun 20 at 9:25
  • 1
    Thank you as always @BERA . I ended up using that method, even though I should learn the fieldmappings... – birks Jun 20 at 11:43

Your Answer

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

Browse other questions tagged or ask your own question.