Search code examples
rlidar

Failure in adding an element to a list


Good morning to everybody! Me and a colleague have some issues with a simple script. We want to extract trees' crown geometries from LiDAR data. We're using the itcSegment library. Considering that we are working with a large dataset we created a cycle so that the model is working with a low load of data. Every cycle the output should be added to a list, however, when the cycle stops the list shows an element only in the last position, all the others are empty... We have no clue why, here's our code:

library(itcSegment)
library(dplyr)

dataset=read.table(file ="dataset_test5.txt",header = T,sep = "\t")

k=1
i=1
j=round(nrow(dataset)/10000)
output.list=list()
for (i in j) { 
      subdata<-slice(dataset,k:(k+9998))
      se1<-itcLiDAR(subdata$X,subdata$Y,subdata$Z,epsg = 32632,resolution = 1,
         MinSearchFilSize = 5, MaxSearchFilSize = 11, TRESHSeed = 0.55,
         TRESHCrown = 0.6, minDIST = 5, maxDIST = 40, HeightThreshold = 10,
         cw = 1)
      k=(k+9999)
      output.list[[i]]=se1
      plot(se,axes=T,add=T)
      }

It should be very simple but we're still R beginners. Thank you for any suggestion


Solution

  • I tried the below code with a R inbuilt dataset lasData

    library(itcSegment)
    library(dplyr)
    
    dataset <- lasData # Using R inbuilt data
    
    Seq_Len <- round(seq(1, nrow(dataset) + 1, length.out = round(nrow(dataset) / 4227))) # Here instead of 4227 you may use 10000 as needed
    
    output.list=list()
    for (i in 1:(length(Seq_Len) - 1)) { 
      print(paste0(i, 'th iteration started')) # To monitor the iteration
      subdata <- slice(dataset, Seq_Len[i]:(Seq_Len[i+1] - 1))
      se1 <- itcLiDAR(subdata$X, subdata$Y, subdata$Z, epsg = 32632, resolution = 1, 
                      MinSearchFilSize = 5, MaxSearchFilSize = 11, TRESHSeed = 0.55, 
                      TRESHCrown = 0.6, minDIST = 5, maxDIST = 40, HeightThreshold = 10, cw = 1)
      output.list[[i]]=se1
    }
    
    output.list
    
    [[1]]
    class       : SpatialPolygonsDataFrame 
    features    : 6 
    extent      : 676769.2, 676787.9, 5091691, 5091719  (xmin, xmax, ymin, ymax)
    crs         : +init=epsg:32632 
    variables   : 5
    names       :         X,          Y, Height_m, ID,            CA_m2 
    min values  : 676772.33, 5091693.65,    29.98,  1, 27.8797000015035 
    max values  : 676784.46, 5091715.39,    40.42,  6, 72.9472500010449 
    
    [[2]]
    class       : SpatialPolygonsDataFrame 
    features    : 5 
    extent      : 676764.1, 676787.7, 5091691, 5091719  (xmin, xmax, ymin, ymax)
    crs         : +init=epsg:32632 
    variables   : 5
    names       :         X,          Y, Height_m, ID,            CA_m2 
    min values  : 676768.28, 5091693.56,    23.82,  1, 3.53379999986505 
    max values  : 676785.18, 5091714.86,    40.56,  5, 63.4798499996493 
    
    [[3]]
    class       : SpatialPolygonsDataFrame 
    features    : 5 
    extent      : 676762.4, 676780.6, 5091691, 5091719  (xmin, xmax, ymin, ymax)
    crs         : +init=epsg:32632 
    variables   : 5
    names       :         X,          Y, Height_m, ID,            CA_m2 
    min values  : 676766.47, 5091693.16,    30.21,  1, 26.2325500003377 
    max values  : 676778.09, 5091715.51,    33.85,  5, 81.5636000017628