Home > Software engineering >  running a while loop to get number of points in lidar data in r
running a while loop to get number of points in lidar data in r

Time:11-26

I have segmented trees in r using lidR packages. The segmented trees have an id associated with them. i want to know how many points are there in each tree. I am using while loop to get the points for each tree but i am only getting the points from only first treeId.

las <-
  segment_trees(las, watershed(
    chm,
    th_tree = 1,
    tol = 0.5,
    ext = 2
  ))

pointlist <- list()
i = 1
while (i < 1000) {
  las <- filter_poi(las, treeID == i)
  x <- header(las)
  y <- x@PHB
  points <- y$`Number of point records`
  pointlist <- append(pointlist, points)
  i <- i   1
}
pointlist

CodePudding user response:

You're overwriting your original las in the while loop with las <- filter_poi(las, treeID == i). Does it work if you assign this to something else? e.g.

pointlist <- list()
i = 1
while (i < 1000) {
  # las_i instead of las
  las_i <- filter_poi(las, treeID == i)
  x <- header(las_i)
  y <- x@PHB
  points <- y$`Number of point records`
  pointlist <- append(pointlist, points)
  i <- i   1
}
pointlist
  • Related