<?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 ArcPy- iterating through select by location, adding values to table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-iterating-through-select-by-location-adding/m-p/318179#M24708</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;I have 2 layers-- polygons of isochrones for bike share stations and land use parcels. I am calculating square footage of residential parcels (as well as non-residential parcels in another layer) that are within each isochrone. There are overlaps in the shapes, so I can't do a spatial join, as I am interested in the total parcels in each shape&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;For each bike share&amp;nbsp;station, I need to:&lt;BR /&gt;1. select the parcels that have their centroid within the isochrone&lt;BR /&gt;2. get the sum of building square footage&amp;nbsp;from a column in the parcels layer&lt;BR /&gt;3. assign that sum value to the station in the isochrone layer&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;My code keeps failing because I need the outstats table to append a row for each station/iteration, or to just directly have the summed value go into the SF_Res field in the "rows" variable/table.&amp;nbsp;SF_Res is an existing field in the rows table and I want to add the Stats output value to that field for each row.&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;Here is what I have:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;arcpy
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;from &lt;/SPAN&gt;arcpy &lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;env

env.workspace = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C:\Users\folder"
&lt;/SPAN&gt;env.overwriteOutput = &lt;SPAN style="color: #000080; font-weight: bold;"&gt;True
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;
&lt;/SPAN&gt;rows = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C:\Users\folder\bikeshare&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;.shp"
&lt;/SPAN&gt;layername = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"res_parcels5"
&lt;/SPAN&gt;outtable = "&lt;SPAN style="color: #008000; font-weight: bold;"&gt;C:\Users\folder\bikes.gdb\statsout
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;arcpy.MakeFeatureLayer_management(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;C:\Users\folder&lt;/SPAN&gt;\Parcels_Res.shp", layername)

&lt;SPAN style="color: #808080;"&gt;#for each Station in rows
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;row &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;rows:
&lt;SPAN style="color: #808080;"&gt;# select all parcels with centroid in rows
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt; &lt;/SPAN&gt;arcpy.SelectLayerByLocation_management(layername, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"have_their_center_in"&lt;/SPAN&gt;, rows)
&lt;SPAN style="color: #808080;"&gt;# sum bldg_area field in parcels layer
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt; &lt;/SPAN&gt;expression = arcpy.Statistics_analysis(layername, outtable, [[&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"bldg_area"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"SUM"&lt;/SPAN&gt;]])
 arcpy.CalculateField_management (rows, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"SF_Res"&lt;/SPAN&gt;, expression, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"PYTHON"&lt;/SPAN&gt;)


&lt;/PRE&gt;&lt;IMG alt="This is an example of the selection that I want to iterate through" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/345209_iso.png" style="width: 620px; height: 554px;" /&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:07:37 GMT</pubDate>
    <dc:creator>CorinneJachelski</dc:creator>
    <dc:date>2021-12-11T15:07:37Z</dc:date>
    <item>
      <title>ArcPy- iterating through select by location, adding values to table</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-iterating-through-select-by-location-adding/m-p/318179#M24708</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;I have 2 layers-- polygons of isochrones for bike share stations and land use parcels. I am calculating square footage of residential parcels (as well as non-residential parcels in another layer) that are within each isochrone. There are overlaps in the shapes, so I can't do a spatial join, as I am interested in the total parcels in each shape&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;For each bike share&amp;nbsp;station, I need to:&lt;BR /&gt;1. select the parcels that have their centroid within the isochrone&lt;BR /&gt;2. get the sum of building square footage&amp;nbsp;from a column in the parcels layer&lt;BR /&gt;3. assign that sum value to the station in the isochrone layer&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;My code keeps failing because I need the outstats table to append a row for each station/iteration, or to just directly have the summed value go into the SF_Res field in the "rows" variable/table.&amp;nbsp;SF_Res is an existing field in the rows table and I want to add the Stats output value to that field for each row.&lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt; &lt;/DIV&gt;&lt;DIV style="color: #222222; background-color: #ffffff; font-size: 12.8px;"&gt;Here is what I have:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;arcpy
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;from &lt;/SPAN&gt;arcpy &lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;env

env.workspace = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C:\Users\folder"
&lt;/SPAN&gt;env.overwriteOutput = &lt;SPAN style="color: #000080; font-weight: bold;"&gt;True
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;
&lt;/SPAN&gt;rows = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C:\Users\folder\bikeshare&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;.shp"
&lt;/SPAN&gt;layername = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"res_parcels5"
&lt;/SPAN&gt;outtable = "&lt;SPAN style="color: #008000; font-weight: bold;"&gt;C:\Users\folder\bikes.gdb\statsout
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;arcpy.MakeFeatureLayer_management(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;C:\Users\folder&lt;/SPAN&gt;\Parcels_Res.shp", layername)

&lt;SPAN style="color: #808080;"&gt;#for each Station in rows
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;row &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;rows:
&lt;SPAN style="color: #808080;"&gt;# select all parcels with centroid in rows
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt; &lt;/SPAN&gt;arcpy.SelectLayerByLocation_management(layername, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"have_their_center_in"&lt;/SPAN&gt;, rows)
&lt;SPAN style="color: #808080;"&gt;# sum bldg_area field in parcels layer
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt; &lt;/SPAN&gt;expression = arcpy.Statistics_analysis(layername, outtable, [[&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"bldg_area"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"SUM"&lt;/SPAN&gt;]])
 arcpy.CalculateField_management (rows, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"SF_Res"&lt;/SPAN&gt;, expression, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"PYTHON"&lt;/SPAN&gt;)


&lt;/PRE&gt;&lt;IMG alt="This is an example of the selection that I want to iterate through" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/345209_iso.png" style="width: 620px; height: 554px;" /&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:07:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-iterating-through-select-by-location-adding/m-p/318179#M24708</guid>
      <dc:creator>CorinneJachelski</dc:creator>
      <dc:date>2021-12-11T15:07:37Z</dc:date>
    </item>
  </channel>
</rss>

