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:
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!
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:
#find nearest feature within a search distance
arcpy.Near_analysis(points, ind_trees, row_u.HEIGHT / 4)
#tree ID assigned where no near feature was found
if row_u.NEAR_FID == -1:
row_u.TREE_NUM = x
Ucur.updateRow(row_u)
x = x + 1
#assign tree ID from near feature
else:
Scur = arcpy.SearchCursor(ind_trees)
for row_s in Scur:
if row_s.OBJECTID == row_u.NEAR_FID:
row_u.TREE_NUM = row_s.TREE_NUM
Ucur.updateRow(row_u)
del row_s
del Scur
#insert a copy of current update cursor row object to comparison geometry feature class
Icur = arcpy.InsertCursor(ind_trees)
Nrow = Icur.newRow()
Nrow.Shape = row_u.Shape
Nrow.PointCount = row_u.PointCount
Nrow.ORIG_FID = row_u.ORIG_FID
Nrow.Z = row_u.Z
Nrow.RASTERVALU = row_u.RASTERVALU
Nrow.HEIGHT = row_u.HEIGHT
Nrow.TREE_NUM = row_u.TREE_NUM
Nrow.NEAR_FID = row_u.NEAR_FID
Nrow.NEAR_DIST = row_u.NEAR_DIST
Icur.insertRow(Nrow)
del Icur
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!