0

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
Jonny Phelps
  • 2,687
  • 1
  • 11
  • 20
Purple_Ad
  • 65
  • 6
  • Does this answer your question? [Extract data from an object from lidar data in r](https://stackoverflow.com/questions/74572171/extract-data-from-an-object-from-lidar-data-in-r) – JRR Dec 10 '22 at 12:43

1 Answers1

0

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
Jonny Phelps
  • 2,687
  • 1
  • 11
  • 20