How to read many lidar files (.las) at once and combine them into one dataframe in R

904 views Asked by At

I have folder where many lidar(.las) files. It looks like

library(rgdal)
library(raster)
library(tmaptools)
library(tmap)

library(lidR)
library(RStoolbox)
las=readLAS("C:/1/078-638.las")
las1=readLAS("C:/1/082-628.las")
las2=....

so if more than 100 files It's hard to write in every line. Is there a way to read all these files at once, but in format data.frame? .las file has such structure

las=payload(las)

las=structure(list(X = c(638238.76, 638238.76, 638239.29, 638235.39, 
638233.86, 638233.86, 638235.55, 638231.97, 638231.91, 638228.41
), Y = c(6078001.09, 6078001.09, 6078001.15, 6078001.15, 6078001.07, 
6078001.07, 6078001.02, 6078001.08, 6078001.09, 6078001.01), 
    Z = c(186.64, 186.59, 199.28, 189.37, 186.67, 186.67, 198.04, 
    200.03, 199.73, 192.14), gpstime = c(319805734.664265, 319805734.664265, 
    319805734.67875, 319805734.678768, 319805734.678777, 319805734.678777, 
    319805734.687338, 319805734.701928, 319805734.701928, 319805734.701945
    ), Intensity = c(13L, 99L, 5L, 2L, 20L, 189L, 2L, 11L, 90L, 
    1L), ReturnNumber = c(2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 
    3L), NumberOfReturns = c(2L, 1L, 3L, 2L, 1L, 1L, 3L, 1L, 
    1L, 4L), ScanDirectionFlag = c(1L, 1L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L), EdgeOfFlightline = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), Classification = c(1L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), Synthetic_flag = c(FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), Keypoint_flag = c(FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
    ), Withheld_flag = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE), ScanAngleRank = c(-12L, -12L, 
    -12L, -12L, -12L, -12L, -12L, -13L, -13L, -13L), UserData = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), PointSourceID = c(16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), Pulse.width = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-10L))

structure of las1

las1=structure(list(X = c(628800.68, 628800.75, 628801.43, 628801.47, 
628802.13, 628802.19, 628800.19, 628800.24, 628799.57, 628799.58
), Y = c(6082001.07, 6082001.08, 6082001.19, 6082001.2, 6082001.3, 
6082001.31, 6082001.21, 6082001.22, 6082001.12, 6082001.12), 
    Z = c(163.16, 162.96, 163.09, 162.97, 163.12, 162.98, 163.29, 
    163.16, 163.02, 162.99), gpstime = c(319799021.884921, 319799021.884921, 
    319799021.884929, 319799021.884929, 319799021.884938, 319799021.884938, 
    319799021.889375, 319799021.889375, 319799021.889384, 319799021.889384
    ), Intensity = c(12L, 99L, 14L, 112L, 14L, 121L, 17L, 167L, 
    20L, 189L), ReturnNumber = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), NumberOfReturns = c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), ScanDirectionFlag = c(1L, 1L, 1L, 1L, 1L, 
    1L, 0L, 0L, 0L, 0L), EdgeOfFlightline = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), Classification = c(1L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L), Synthetic_flag = c(FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), 
    Keypoint_flag = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE), Withheld_flag = c(FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), 
    ScanAngleRank = c(17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
    17L, 17L), UserData = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L), PointSourceID = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L), Pulse.width = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L)), class = "data.frame", row.names = c(NA, -10L))

then after we read all .las files , we combine it in one dataset, with indicating that these rows belong to the first las file, and these to the second, something like this

X              Y           Z    gpstime        Intensity    ReturnNumber            number las
638238.76   6078001.09  186.64  319805734.664265    13         2                          1
638238.76   6078001.09  186.59  319805734.664265    99         1                          1
638239.29   6078001.15  199.28  319805734.67875      5         1                          1
638235.39   6078001.15  189.37  319805734.678768    2          2                          1
638233.86   6078001.07  186.67  319805734.678777    20         1                          1
638233.86   6078001.07  186.67  319805734.678777    189        1                          1
638235.55   6078001.02  198.04  319805734.687338    2          2                          1
638231.97   6078001.08  200.03  319805734.701928    11         1                          1
638231.91   6078001.09  199.73  319805734.701928    90         1                          1
638228.41   6078001.01  192.14  319805734.701945    1          3                          1
628800.68   6082001.07  163.16  319799021.884921    12         1                          2
628800.75   6082001.08  162.96  319799021.884921    99         1                          2
628801.43   6082001.19  163.09  319799021.884929    14         1                          2
628801.47   6082001.2   162.97  319799021.884929    112        1                          2
628802.13   6082001.3   163.12  319799021.884938    14         1                          2
628802.19   6082001.31  162.98  319799021.884938    121        1                          2
628800.19   6082001.21  163.29  319799021.889375    17         1                          2
628800.24   6082001.22  163.16  319799021.889375    167        1                          2
628799.57   6082001.12  163.02  319799021.889384    20         1                          2
628799.58   6082001.12  162.99  319799021.889384    189        1                          2

So How can i read all .las files from folder C:/1, then for all of them get structure format like i provided above, and them combine it into 1 dataset with las file number. Thanks for any of your valuable help.

*Edit now next error list_df <- filenames %>%

  • purrr::map(., ~readLAS(.x) %>% mutate(filenumber = match(.x, filenames))) Error in UseMethod("mutate") : no suitable method for 'mutate' applied to object of las I think it becauase las format has such structure

    las class : LAS (v1.2 format 1) memory : 308.7 Mb extent : 637999, 638240.5, 6077999, 6079999 (xmin, xmax, ymin, ymax) coord. ref. : NA area : 409328 unitsĀ² points : 3.68 million points density : 8.99 points/unitsĀ²

but dataframe from las must be using payload function which i indicated.

but list_df <- filenames %>%

  • purrr::map(., ~readLAS(.x) %>% payload(filenumber = match(.x, filenames))) provide error.
2

There are 2 answers

6
Julian On BEST ANSWER

Maybe somthing like this

filenames <- list.files(path <- "C:/1/", pattern="*.las", full.names=TRUE)
    
list_df <- filenames %>%
  purrr::map(., ~read.LAS(.x) %>% payload() %>% mutate(filenumber = match(.x, filenames))) 


# If all data has the same structure, you can easily bind them together, i.e.
list_df %>% bind_rows()
0
JRR On

No need to loop. rlas already natively support reading multiples files

rlas::read.las(filenames)