AnsweredAssumed Answered

Problem with near analysis within an update cursor for loop

Question asked by jb257206 on Feb 10, 2014
Latest reply on Feb 18, 2014 by hua17
I am new to python and trying to create a script that will help individuate trees from a LiDAR point cloud.  

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.  An update cursor is then created for the initial feature class and a variable declared as zero to assign a tree ID number.  Using a for loop, each row is assigned a tree ID number based on the results of a near analysis(proximity toolset).  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).  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.

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.  Here is what my script looks like now:



#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)


#create update cursor
Ucur = arcpy.updateCursor(points, "", "", "", "HEIGHT D")


#declare variable used to assign tree ID number
x = 0


for row_u in Ucur:

[INDENT]#find nearest feature within a search distance[/INDENT]
[INDENT]arcpy.Near_analysis(points, ind_trees, row_u.HEIGHT / 4)[/INDENT]

[INDENT]#tree ID assigned where no near feature was found[/INDENT]
[INDENT]if row_u.NEAR_FID == -1:[/INDENT]
[INDENT=2]row_u.TREE_NUM = x[/INDENT]
[INDENT=2]Ucur.updateRow(row_u)[/INDENT]
[INDENT=2]x = x + 1[/INDENT]

[INDENT]#assign tree ID from near feature[/INDENT]
[INDENT]else:[/INDENT]
[INDENT=2]Scur = arcpy.SearchCursor(ind_trees)[/INDENT]
[INDENT=2]for row_s in Scur:[/INDENT]
[INDENT=3]if row_s.OBJECTID == row_u.NEAR_FID:[/INDENT]
[INDENT=4]row_u.TREE_NUM = row_s.TREE_NUM[/INDENT]
[INDENT=4]Ucur.updateRow(row_u)[/INDENT]
[INDENT=2]del row_s[/INDENT]
[INDENT=2]del Scur[/INDENT]

[INDENT]#insert a copy of current update cursor row object to comparison geometry feature class[/INDENT]
[INDENT]Icur = arcpy.InsertCursor(ind_trees)[/INDENT]
[INDENT]Nrow = Icur.newRow()[/INDENT]
[INDENT]Nrow.Shape = row_u.Shape[/INDENT]
[INDENT]Nrow.PointCount = row_u.PointCount[/INDENT]
[INDENT]Nrow.ORIG_FID = row_u.ORIG_FID[/INDENT]
[INDENT]Nrow.Z = row_u.Z[/INDENT]
[INDENT]Nrow.RASTERVALU = row_u.RASTERVALU[/INDENT]
[INDENT]Nrow.HEIGHT = row_u.HEIGHT[/INDENT]
[INDENT]Nrow.TREE_NUM = row_u.TREE_NUM[/INDENT]
[INDENT]Nrow.NEAR_FID = row_u.NEAR_FID[/INDENT]
[INDENT]Nrow.NEAR_DIST = row_u.NEAR_DIST[/INDENT]
[INDENT]Icur.insertRow(Nrow)[/INDENT]
[INDENT]del Icur[/INDENT]


del row_u
del Ucur


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.  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.  Any advice would be a huge help. Thanks!

Outcomes