AnsweredAssumed Answered

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

Question asked by deleted-user-AyQ-Iok8btBJ on Aug 15, 2019
Latest reply on Sep 9, 2019 by deleted-user-AyQ-Iok8btBJ

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

# Create Feature Layers
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 ('Field Calculated!')
print ("Done!!!")