Performing a spatial join to obtain attribute field summaries of nested polygons that are located within polygon regions using ArcPy

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()


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 not in keepers:

#set merge rules here to obtain min, max, and range for kept attribute fields


Geographic Information Systems Asked on November 12, 2021

1 Answers

One Answer

I was able to answer my own question, after quite a bit of reading and looking for examples using merge rules in ArcPy; I've included my updated code snippet below. I used 'CalculateField_management' to find the range values after the spatial join was completed, which worked fine for me.

#Set up fieldmappings for spatial join 
fieldNamesToMin = ["Min_Z","circularityIndex_min","solidity_min","MBG_Width_min","MBG_Length_min","MBG_Orientation_min"]
fieldNamesToMax = ["area_sqft","length_ft","Max_Z","circularityIndex_max","solidity_max","MBG_Width_max","MBG_Length_max","MBG_Orientation_max"]

fieldmappings = arcpy.FieldMappings()


#remove undesired fields in final output
drops = ["perim_ft","gridcode"]

for field in fieldmappings.fields:
        if in drops:

#loop through field names for different merge rules (min and max values), and update field map with new merge rule, then perform spatial join
for fieldName in fieldNamesToMin:
        fieldIndexMin = fieldmappings.findFieldMapIndex(fieldName)
        fieldMap = fieldmappings.getFieldMap(fieldIndexMin)
        fieldMap.mergeRule = "MIN"


for fieldName in fieldNamesToMax:
        fieldIndexMax = fieldmappings.findFieldMapIndex(fieldName)
        fieldMap2 = fieldmappings.getFieldMap(fieldIndexMax)
        fieldMap2.mergeRule = "MAX"


outFeatures = os.path.join(outLoc,outElim+ "_Final")


Answered by AKubes on November 12, 2021

Add your own answers!

Related Questions

Wrong coordinates in layout

1  Asked on January 23, 2021 by alexandru


ArcGIS Pro and ArcMap refuse to split a polygon

1  Asked on January 23, 2021 by matt-wilkie


Creating a dynamic KML using a websites API with Python

1  Asked on January 23, 2021 by failuretostop


Layers Panel Disappeared Completely

1  Asked on January 23, 2021 by mcgeoff


Published ESRI layer scale dependency problems

1  Asked on January 23, 2021 by scottfree


Setting transparency color in Expression Builder

0  Asked on January 23, 2021 by necrohiero


R in QGIS not working after change of computers

0  Asked on January 23, 2021 by clive-coetzee


Prediction of Sentinel images by SVM classifier

0  Asked on January 23, 2021 by hashalluring


Assign NTA’s to lat longs using R?

1  Asked on January 22, 2021 by naomi-tinga


Run GDAL on android and perform operations

0  Asked on January 22, 2021 by user9322960


QGIS 3 won’t open in Windows 10 pro, how to fix?

1  Asked on January 22, 2021 by bryan-ziga-chacn


Providing GDAL with right encoding

0  Asked on January 22, 2021 by aneta-musli


Ask a Question

Get help from others!

© 2021 All rights reserved.