I have created an ArcPy script tool that finds depressions from an input DEM, and then generates closed contours (both polyline and polygon outputs) as well as regions (raster and polygon outputs). I need my tool to be able to output a final layer that provides summary attributes of the closed contour polygons that fall within each region from the regions polygon feature class.
To do this, I know I need to perform a spatial join with the closed contour (cc) polygons and the regions polygons to obtain statistics from the attributes of the cc polygons within the regions; however, I want multiple statistics for each attribute field, such as 'Max','Min',and 'Range'. I can successfully do this using the 'Spatial Join' within ArcMap 10.6's user interface, but I want to do this using ArcPy - I think I need to use fieldmappings to do this and set up merge rules, but I'm not sure how to do this step. I have used fieldmappings to add both tables, and to remove fields that I don't want to retain, but I am not sure where to proceed from there.
I have included a code snippet of my current fieldmappings before performing the spatial join below:
outFeatures = os.path.join(outLoc,outElim+ "_Final")
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(outRegions)
fieldmappings.addTable(outElim)
keepers = ["area_sqft","perim_ft","Min_Z","Max_Z","Range_Z","circularityIndex","solidity","gridcode","length_ft","MBG_Width","MBG_Length","MBG_Orientation"]
#remove all output fields that aren't desired, then perform spatial joins
for field in fieldmappings.fields:
if field.name not in keepers:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))
#set merge rules here to obtain min, max, and range for kept attribute fields
arcpy.SpatialJoin_analysis(outRegions,outElim,outFeatures,"JOIN_ONE_TO_ONE","KEEP_COMMON",fieldmappings,"CONTAINS","","")