<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Loop through features, select by location, populate new field? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449102#M35184</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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'.&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;# 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!!!")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 20:03:20 GMT</pubDate>
    <dc:creator>deleted-user-AyQ-Iok8btBJ</dc:creator>
    <dc:date>2021-12-11T20:03:20Z</dc:date>
    <item>
      <title>Loop through features, select by location, populate new field?</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449102#M35184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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'.&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;# 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!!!")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:03:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449102#M35184</guid>
      <dc:creator>deleted-user-AyQ-Iok8btBJ</dc:creator>
      <dc:date>2021-12-11T20:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through features, select by location, populate new field?</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449103#M35185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/183546"&gt;Neel&lt;/A&gt;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &amp;nbsp;Take a look at this example:&amp;nbsp;&lt;A class="link-titled" href="https://gis.stackexchange.com/questions/251639/creating-spatial-join-which-will-calculate-sum-of-values-in-certain-field-behind" title="https://gis.stackexchange.com/questions/251639/creating-spatial-join-which-will-calculate-sum-of-values-in-certain-field-behind"&gt;arcgis desktop - Creating spatial-join which will calculate sum of values in certain field behind attribute in ArcMap?&amp;nbsp;&lt;/A&gt;Additional info about spatial join is available at&amp;nbsp;&lt;A class="link-titled" href="http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/spatial-join.htm" title="http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/spatial-join.htm"&gt;Spatial Join—Help | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;or &lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/tool-reference/analysis/spatial-join.htm" title="https://pro.arcgis.com/en/pro-app/tool-reference/analysis/spatial-join.htm"&gt;Spatial Join—Help | ArcGIS Pro&lt;/A&gt;&amp;nbsp;including the python code format. &amp;nbsp;Experiment with the tool in Desktop or Pro.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pertaining to your code, good job if you are&amp;nbsp;brand new to Python. &amp;nbsp;I am on my phone at the moment so it is not the easiest to view. &amp;nbsp;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. &amp;nbsp;Then in line 33, simply set the value of NexradValue (row[1]) equal to the sum and perform an updateRow on the cursor. &amp;nbsp;Take a look at&amp;nbsp;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm"&gt;UpdateCursor—Data Access module | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Aug 2019 19:48:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449103#M35185</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2019-08-15T19:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through features, select by location, populate new field?</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449104#M35186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Lance! This was exactly what I needed.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Sep 2019 15:02:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-features-select-by-location-populate/m-p/449104#M35186</guid>
      <dc:creator>deleted-user-AyQ-Iok8btBJ</dc:creator>
      <dc:date>2019-09-09T15:02:08Z</dc:date>
    </item>
  </channel>
</rss>

