<?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 Problem with near analysis within an update cursor for loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536385#M41981</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am new to python and trying to create a script that will help individuate trees from a LiDAR point cloud.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm starting with a point feature class of LiDAR returns and using it as a template for another point feature class for spatial comparison.&amp;nbsp; An update cursor is then created for the initial feature class and a variable declared as zero to assign a tree ID number.&amp;nbsp; Using a for loop, each row is assigned a tree ID number based on the results of a near analysis(proximity toolset).&amp;nbsp; The row is assigned a unique tree number(if no near feature was found) or given the tree ID number of the near feature(if one was returned by the near analysis).&amp;nbsp; Then a copy of the current row is inserted into the comparison feature class and the loop iterates again until all features have been assigned a tree ID number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's the concept, but at the moment my script will simply assign every row a unique tree ID number rather than several rows sharing tree ID numbers.&amp;nbsp; Here is what my script looks like now:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#import required modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#specify workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#specify source geometry&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;points = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb/Extract_BS868661"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#create comparison geometry feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ind_trees = arcpy.CreateFeatureclass_management(env.workspace, "ind_trees", "POINT", points, "DISABLED", "SAME_AS_TEMPLATE", points)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#create update cursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ucur = arcpy.updateCursor(points, "", "", "", "HEIGHT D")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#declare variable used to assign tree ID number&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;x = 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row_u in Ucur:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#find nearest feature within a search distance[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]arcpy.Near_analysis(points, ind_trees, row_u.HEIGHT / 4)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#tree ID assigned where no near feature was found[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]if row_u.NEAR_FID == -1:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]row_u.TREE_NUM = x[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]Ucur.updateRow(row_u)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]x = x + 1[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#assign tree ID from near feature[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]else:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]Scur = arcpy.SearchCursor(ind_trees)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]for row_s in Scur:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=3]if row_s.OBJECTID == row_u.NEAR_FID:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=4]row_u.TREE_NUM = row_s.TREE_NUM[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=4]Ucur.updateRow(row_u)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]del row_s[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]del Scur[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#insert a copy of current update cursor row object to comparison geometry feature class[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Icur = arcpy.InsertCursor(ind_trees)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow = Icur.newRow()[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.Shape = row_u.Shape[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.PointCount = row_u.PointCount[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.ORIG_FID = row_u.ORIG_FID[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.Z = row_u.Z[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.RASTERVALU = row_u.RASTERVALU[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.HEIGHT = row_u.HEIGHT[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.TREE_NUM = row_u.TREE_NUM[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.NEAR_FID = row_u.NEAR_FID[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.NEAR_DIST = row_u.NEAR_DIST[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Icur.insertRow(Nrow)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]del Icur[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del row_u&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del Ucur&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It appears that the update cursor does not recognize the values generated by the near analysis after it was created, causing the loop never to fall to the else statement.&amp;nbsp; Due to my inexperience, I'm sure there are many things wrong with this script and there's probably a more optimal method to accomplish this task.&amp;nbsp; Any advice would be a huge help. Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Feb 2014 18:04:01 GMT</pubDate>
    <dc:creator>JohnBowers</dc:creator>
    <dc:date>2014-02-10T18:04:01Z</dc:date>
    <item>
      <title>Problem with near analysis within an update cursor for loop</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536385#M41981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am new to python and trying to create a script that will help individuate trees from a LiDAR point cloud.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm starting with a point feature class of LiDAR returns and using it as a template for another point feature class for spatial comparison.&amp;nbsp; An update cursor is then created for the initial feature class and a variable declared as zero to assign a tree ID number.&amp;nbsp; Using a for loop, each row is assigned a tree ID number based on the results of a near analysis(proximity toolset).&amp;nbsp; The row is assigned a unique tree number(if no near feature was found) or given the tree ID number of the near feature(if one was returned by the near analysis).&amp;nbsp; Then a copy of the current row is inserted into the comparison feature class and the loop iterates again until all features have been assigned a tree ID number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's the concept, but at the moment my script will simply assign every row a unique tree ID number rather than several rows sharing tree ID numbers.&amp;nbsp; Here is what my script looks like now:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#import required modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#specify workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#specify source geometry&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;points = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb/Extract_BS868661"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#create comparison geometry feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ind_trees = arcpy.CreateFeatureclass_management(env.workspace, "ind_trees", "POINT", points, "DISABLED", "SAME_AS_TEMPLATE", points)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#create update cursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ucur = arcpy.updateCursor(points, "", "", "", "HEIGHT D")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#declare variable used to assign tree ID number&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;x = 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row_u in Ucur:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#find nearest feature within a search distance[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]arcpy.Near_analysis(points, ind_trees, row_u.HEIGHT / 4)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#tree ID assigned where no near feature was found[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]if row_u.NEAR_FID == -1:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]row_u.TREE_NUM = x[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]Ucur.updateRow(row_u)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]x = x + 1[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#assign tree ID from near feature[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]else:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]Scur = arcpy.SearchCursor(ind_trees)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]for row_s in Scur:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=3]if row_s.OBJECTID == row_u.NEAR_FID:[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=4]row_u.TREE_NUM = row_s.TREE_NUM[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=4]Ucur.updateRow(row_u)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]del row_s[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT=2]del Scur[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]#insert a copy of current update cursor row object to comparison geometry feature class[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Icur = arcpy.InsertCursor(ind_trees)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow = Icur.newRow()[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.Shape = row_u.Shape[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.PointCount = row_u.PointCount[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.ORIG_FID = row_u.ORIG_FID[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.Z = row_u.Z[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.RASTERVALU = row_u.RASTERVALU[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.HEIGHT = row_u.HEIGHT[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.TREE_NUM = row_u.TREE_NUM[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.NEAR_FID = row_u.NEAR_FID[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Nrow.NEAR_DIST = row_u.NEAR_DIST[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Icur.insertRow(Nrow)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]del Icur[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del row_u&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del Ucur&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It appears that the update cursor does not recognize the values generated by the near analysis after it was created, causing the loop never to fall to the else statement.&amp;nbsp; Due to my inexperience, I'm sure there are many things wrong with this script and there's probably a more optimal method to accomplish this task.&amp;nbsp; Any advice would be a huge help. Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Feb 2014 18:04:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536385#M41981</guid>
      <dc:creator>JohnBowers</dc:creator>
      <dc:date>2014-02-10T18:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with near analysis within an update cursor for loop</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536386#M41982</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First off, nice script and documentation!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think you're right, the problem is with the near tool.The near tool was running on all your points for each iteration of the loop. This could cause problems (I think only the last run near tool would be maintained). It's also very slow.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I moved the near tool to right before where you set up the update cursor. Since it's outside the for loop I couldn't use the &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;row_u.HEIGHT / 4&lt;/SPAN&gt;&lt;SPAN&gt; for the search radius. Instead I used &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;row_u.HEIGHT / 4&lt;/SPAN&gt;&lt;SPAN&gt; in the loop and compared it to &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;row_u.NEAR_DIST&lt;/SPAN&gt;&lt;SPAN&gt; (the distance to the nearest point).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#import required modules import arcpy from arcpy import env&amp;nbsp; #specify workspace env.workspace = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb"&amp;nbsp; #specify source geometry points = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb/Extract_BS868661"&amp;nbsp; #create comparison geometry feature class ind_trees = arcpy.CreateFeatureclass_management(env.workspace, "ind_trees", "POINT", points, "DISABLED", "SAME_AS_TEMPLATE", points)&amp;nbsp; #find nearest features arcpy.Near_analysis(points, ind_trees)&amp;nbsp; #create update cursor Ucur = arcpy.updateCursor(points, "", "", "", "HEIGHT D")&amp;nbsp; #declare variable used to assign tree ID number x = 0&amp;nbsp; for row_u in Ucur: &amp;nbsp;&amp;nbsp;&amp;nbsp; #tree ID assigned where no near feature was found within search radius &amp;nbsp;&amp;nbsp;&amp;nbsp; if row_u.NEAR_DIST &amp;gt; row_u.HEIGHT / 4: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_u.TREE_NUM = x &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ucur.updateRow(row_u) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = x + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; #assign tree ID from near feature &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Scur = arcpy.SearchCursor(ind_trees) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row_s in Scur: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row_s.OBJECTID == row_u.NEAR_FID: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_u.TREE_NUM = row_s.TREE_NUM &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ucur.updateRow(row_u) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row_s &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del Scur &amp;nbsp;&amp;nbsp;&amp;nbsp; #insert a copy of current update cursor row object to comparison geometry feature class &amp;nbsp;&amp;nbsp;&amp;nbsp; Icur = arcpy.InsertCursor(ind_trees) &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow = Icur.newRow() &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.Shape = row_u.Shape &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.PointCount = row_u.PointCount &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.ORIG_FID = row_u.ORIG_FID &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.Z = row_u.Z &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.RASTERVALU = row_u.RASTERVALU &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.HEIGHT = row_u.HEIGHT &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.TREE_NUM = row_u.TREE_NUM &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.NEAR_FID = row_u.NEAR_FID &amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.NEAR_DIST = row_u.NEAR_DIST &amp;nbsp;&amp;nbsp;&amp;nbsp; Icur.insertRow(Nrow) &amp;nbsp;&amp;nbsp;&amp;nbsp; del Icur&amp;nbsp; del row_u del Ucur&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think this might fix your issue, but I didn't have a chance to test it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know if it works!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;~Josh&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 16:57:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536386#M41982</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-02-11T16:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with near analysis within an update cursor for loop</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536387#M41983</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Josh,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;this was good advice.&amp;nbsp; Unfortunately I do need the near tool to recalculate each iteration of the loop.&amp;nbsp; My tentative solution is to nest the entire script(except the initial variable declaration) within a where loop so that the update cursor can be deleted for the next near analysis to be run.&amp;nbsp; I am still working out the appropriate syntax for the where clause.&amp;nbsp; I appreciate the help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Feb 2014 13:54:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536387#M41983</guid>
      <dc:creator>JohnBowers</dc:creator>
      <dc:date>2014-02-18T13:54:23Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with near analysis within an update cursor for loop</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536388#M41984</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm happy my first post was partially successful. I'm a little unclear as to why you need to run the near tool inside the loop. Would it work if you ran an extra loop to run the near tool and then did everything else in the second loop? This way all the fields would be ready (like &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;NEAR_FID&lt;/SPAN&gt;&lt;SPAN&gt;) before you initiate the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;updateCursor&lt;/SPAN&gt;&lt;SPAN&gt;. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe something like this (which I haven't had a chance to test yet):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#import required modules
import arcpy
from arcpy import env


#specify workspace
env.workspace = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb"


#specify source geometry
points = "C:/Columbus_OH_LiDAR_2011/Canopy.gdb/Extract_BS868661"


#create comparison geometry feature class
ind_trees = arcpy.CreateFeatureclass_management(env.workspace, "ind_trees", "POINT", points, "DISABLED", "SAME_AS_TEMPLATE", points)

#declare variable used to assign tree ID number
x = 0

sCur2=arcpy.SearchCursor(points, "", "", "", "HEIGHT D")
#get nearest for each point
for row in Scur2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Create feature layer for current point:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(points, "TargetPoint_lyr", '"FID" = '+str(row.FID))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #find nearest feature within a search distance (for current point only)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Near_analysis("TargetPoint_lyr", ind_trees, row.HEIGHT / 4)
del row
del Scur2

#create update cursor
Ucur = arcpy.updateCursor(points, "", "", "", "HEIGHT D")

for row_u in Ucur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #tree ID assigned where no near feature was found
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row_u.NEAR_FID == -1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_u.TREE_NUM = x
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ucur.updateRow(row_u)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = x + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; #assign tree ID from near feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Scur = arcpy.SearchCursor(ind_trees)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row_s in Scur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row_s.OBJECTID == row_u.NEAR_FID:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_u.TREE_NUM = row_s.TREE_NUM
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ucur.updateRow(row_u)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row_s
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del Scur
&amp;nbsp;&amp;nbsp;&amp;nbsp; #insert a copy of current update cursor row object to comparison geometry feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp; Icur = arcpy.InsertCursor(ind_trees)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow = Icur.newRow()
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.Shape = row_u.Shape
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.PointCount = row_u.PointCount
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.ORIG_FID = row_u.ORIG_FID
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.Z = row_u.Z
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.RASTERVALU = row_u.RASTERVALU
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.HEIGHT = row_u.HEIGHT
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.TREE_NUM = row_u.TREE_NUM
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.NEAR_FID = row_u.NEAR_FID
&amp;nbsp;&amp;nbsp;&amp;nbsp; Nrow.NEAR_DIST = row_u.NEAR_DIST
&amp;nbsp;&amp;nbsp;&amp;nbsp; Icur.insertRow(Nrow)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del Icur

del row_u
del Ucur&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know about any updates!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:17:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-near-analysis-within-an-update-cursor/m-p/536388#M41984</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T23:17:47Z</dc:date>
    </item>
  </channel>
</rss>

