<?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 nesting UpdateCursor within SearchCursor and more...? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318302#M24717</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to python and I'm in the learning/testing phase. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From borrowing various snipits, I am calculating the azimuth of a line segment then attempting to write that value to the attribute table. Here's my code, then I'll get to the question.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class&amp;nbsp;&amp;nbsp; #angleList []&amp;nbsp; angleMath = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])&amp;nbsp;&amp;nbsp; for row in angleMath: &amp;nbsp;&amp;nbsp;&amp;nbsp; startpt = row[1].firstPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = startpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y1 = startpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; endpt = row[1].lastPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = endpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y2 = endpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; deltax = x2 - x1 &amp;nbsp;&amp;nbsp;&amp;nbsp; deltay = y2 - y1 &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_rad = atan2(deltay,deltax) &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg = angle_rad*180.0/pi &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #angleList.append&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cursor = arcpy.UpdateCursor(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; for upRow in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; upRow.azimuth = angle_deg &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(upRow) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; del cursor&amp;nbsp; del angleMath&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am able to get the angle_deg value and my commented angleList list works but I'm not sure where/how to use it. I know my UpdateCursor is in the wrong spot. It updates the first angle_deg value for all records. Is there a way to use the UpdateCursor to write the value of the angle_deg variable for each record? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or would it be best to try to use the angleList list with an UpdateCursor and if so, how do you use a list to feed an UpdateCursor? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for any help you can provide. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wesley&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Dec 2012 21:40:33 GMT</pubDate>
    <dc:creator>deleted-user-iREoAvyfnLld</dc:creator>
    <dc:date>2012-12-17T21:40:33Z</dc:date>
    <item>
      <title>nesting UpdateCursor within SearchCursor and more...?</title>
      <link>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318302#M24717</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to python and I'm in the learning/testing phase. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From borrowing various snipits, I am calculating the azimuth of a line segment then attempting to write that value to the attribute table. Here's my code, then I'll get to the question.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class&amp;nbsp;&amp;nbsp; #angleList []&amp;nbsp; angleMath = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])&amp;nbsp;&amp;nbsp; for row in angleMath: &amp;nbsp;&amp;nbsp;&amp;nbsp; startpt = row[1].firstPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = startpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y1 = startpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; endpt = row[1].lastPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = endpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y2 = endpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; deltax = x2 - x1 &amp;nbsp;&amp;nbsp;&amp;nbsp; deltay = y2 - y1 &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_rad = atan2(deltay,deltax) &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg = angle_rad*180.0/pi &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #angleList.append&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cursor = arcpy.UpdateCursor(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; for upRow in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; upRow.azimuth = angle_deg &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(upRow) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; del cursor&amp;nbsp; del angleMath&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am able to get the angle_deg value and my commented angleList list works but I'm not sure where/how to use it. I know my UpdateCursor is in the wrong spot. It updates the first angle_deg value for all records. Is there a way to use the UpdateCursor to write the value of the angle_deg variable for each record? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or would it be best to try to use the angleList list with an UpdateCursor and if so, how do you use a list to feed an UpdateCursor? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for any help you can provide. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wesley&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Dec 2012 21:40:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318302#M24717</guid>
      <dc:creator>deleted-user-iREoAvyfnLld</dc:creator>
      <dc:date>2012-12-17T21:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: nesting UpdateCursor within SearchCursor and more...?</title>
      <link>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318303#M24718</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think you can get away with using just an Update cursor, as it has the same methods as a Search cursor in addition to the ones for writing. You also won't need to store the values in a list since you can write within the same &lt;/SPAN&gt;&lt;STRONG&gt;for&lt;/STRONG&gt;&lt;SPAN&gt; loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
from math import atan2, pi
env.workspace = "c:/my_data/test_data.gdb"
fc = "polyline1"

angleMath = arcpy.UpdateCursor(fc, ["OID@", "SHAPE@","azimuth"])
for row in angleMath:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #calc azimuth
&amp;nbsp;&amp;nbsp;&amp;nbsp; startpt = row[1].firstPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = startpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; y1 = startpt.Y
&amp;nbsp;&amp;nbsp;&amp;nbsp; endpt = row[1].lastPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = endpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; y2 = endpt.Y
&amp;nbsp;&amp;nbsp;&amp;nbsp; deltax = x2 - x1
&amp;nbsp;&amp;nbsp;&amp;nbsp; deltay = y2 - y1
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle_rad = atan2(deltay,deltax)
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg = angle_rad*180.0/pi

&amp;nbsp;&amp;nbsp;&amp;nbsp; #write azimuth
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.azimuth = angle_deg
&amp;nbsp;&amp;nbsp;&amp;nbsp; angleMath.updateRow(row)

 del row, angleMath
 &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Might need some tweaks (parameters for the Update cursor), but should be pretty close.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:08:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318303#M24718</guid>
      <dc:creator>MattSayler</dc:creator>
      <dc:date>2021-12-11T15:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: nesting UpdateCursor within SearchCursor and more...?</title>
      <link>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318304#M24719</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the post Matt, it got me going in the right direction. With the path I was going down, I needed to use the arcpy.da.UpdateCursor to access the geometry "SHAPE@". However, this cursor list would not recognize the "azimuth" field for some reason. So, I scrapped the arcpy.da.UpdateCursor to access the geometry and went with another method mentioned in the ArcGIS 10.1 help. I also tweaked my math to get north azimuth. The code below worked! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class&amp;nbsp; desc = arcpy.Describe(fc) shapefieldname = desc.ShapeFieldName&amp;nbsp; angleMath = arcpy.UpdateCursor(fc)&amp;nbsp;&amp;nbsp; for row in angleMath:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.getValue(shapefieldname) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; startpt = feat.firstPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = startpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y1 = startpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; endpt = feat.lastPoint &amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = endpt.X &amp;nbsp;&amp;nbsp;&amp;nbsp; y2 = endpt.Y &amp;nbsp;&amp;nbsp;&amp;nbsp; deltax = x2 - x1 &amp;nbsp;&amp;nbsp;&amp;nbsp; deltay = y2 - y1 &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_rad = atan2(deltay,deltax) &amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg_pre = (((angle_rad*180.0/pi) - 90)*-1)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if angle_deg_pre &amp;lt; 0: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg = (((angle_rad*180.0/pi) - 90)*-1)+360 &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; angle_deg = (((angle_rad*180.0/pi) - 90)*-1) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; row.azimuth = angle_deg &amp;nbsp;&amp;nbsp;&amp;nbsp; angleMath.updateRow(row)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, angleMath&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Dec 2012 13:34:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-updatecursor-within-searchcursor-and-more/m-p/318304#M24719</guid>
      <dc:creator>deleted-user-iREoAvyfnLld</dc:creator>
      <dc:date>2012-12-18T13:34:24Z</dc:date>
    </item>
  </channel>
</rss>

