Loop through features, select by location, populate new field?

1895
2
Jump to solution
08-15-2019 12:08 PM
deleted-user-AyQ-Iok8btBJ
New Contributor II

I have two polygon feature classes: Basins, and Nexrad. Nexrad contains a field called 'value' which contains floating point numbers. I am trying to populate a new field with the sum of 'value' for all intersecting nexrad polygons for each basin feature. I am trying to loop through each basin, select all nexrad polygons that intersect the selected basin, calculate the sum of the 'value field in nexrad, and add that sum to a new field in the basins. Right now my script loops through correctly but only adds the sum from the last 'select by location'. 

# Import Modules
import arcpy
import os
import numpy
from arcpy import env

# Set environment and workspace
env.workspace = r"W:\Citrix\34000s\34624\001\GIS\RainfallDepthTool\RainfallDepth_NK.gdb"
arcpy.env.overwriteOutput = True
print ("Environment set!")

# Set ariables
basins = r"W:\Citrix\34000s\34624\001\GIS\RainfallDepthTool\RainfallDepth_NK.gdb\Basins"
nexrad = r"W:\SAN\34000s\34624\001\DataIn\NEXRAD_9_22\shp\hourly\KEWX_N1P_20180922_075800.shp"

# Add a field to the basins to be populated with Nexrad polygon value
arcpy.AddField_management(basins,"NexradValue","FLOAT")

# Create Feature Layers
arcpy.MakeFeatureLayer_management(basins,'basinLyr')
arcpy.MakeFeatureLayer_management(nexrad,'nexradLyr')
print ("Layers created!")
field = 'Shape@'
with arcpy.da.SearchCursor('basinLyr', field) as cursor:
    for row in cursor:
        # Get geometry to use in select by location
        geom = row[0]
        # Select nexrad polygon that intersect the selected basin
        x = arcpy.SelectLayerByLocation_management('nexradLyr', 'INTERSECT', geom, '', 'NEW_SELECTION')
        # Calculate the sum of the 'value' field in the selected nexrad polygons
        field = arcpy.da.TableToNumPyArray (x, 'value')
        sum = field['value'].sum()
        print(sum)
        arcpy.CalculateField_management('basins','NexradValue',float(sum))
        print ('Field Calculated!')
print ("Done!!!")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
LanceCole
MVP Regular Contributor

Neel,

You may want to look at using a spatial join with the merge rule set to sum for the particular field you are working with.  Take a look at this example: arcgis desktop - Creating spatial-join which will calculate sum of values in certain field behind at...Additional info about spatial join is available at Spatial Join—Help | ArcGIS Desktop or Spatial Join—Help | ArcGIS Pro including the python code format.  Experiment with the tool in Desktop or Pro.

Pertaining to your code, good job if you are brand new to Python.  I am on my phone at the moment so it is not the easiest to view.  In line 23, consider using an update cursor rather than a search cursor and including the field NexradValue in addition to the Shape@ in the field list.  Then in line 33, simply set the value of NexradValue (row[1]) equal to the sum and perform an updateRow on the cursor.  Take a look at UpdateCursor—Data Access module | ArcGIS Desktop 

View solution in original post

2 Replies
LanceCole
MVP Regular Contributor

Neel,

You may want to look at using a spatial join with the merge rule set to sum for the particular field you are working with.  Take a look at this example: arcgis desktop - Creating spatial-join which will calculate sum of values in certain field behind at...Additional info about spatial join is available at Spatial Join—Help | ArcGIS Desktop or Spatial Join—Help | ArcGIS Pro including the python code format.  Experiment with the tool in Desktop or Pro.

Pertaining to your code, good job if you are brand new to Python.  I am on my phone at the moment so it is not the easiest to view.  In line 23, consider using an update cursor rather than a search cursor and including the field NexradValue in addition to the Shape@ in the field list.  Then in line 33, simply set the value of NexradValue (row[1]) equal to the sum and perform an updateRow on the cursor.  Take a look at UpdateCursor—Data Access module | ArcGIS Desktop 

deleted-user-AyQ-Iok8btBJ
New Contributor II

Thanks Lance! This was exactly what I needed. 

0 Kudos