Calling LAStools from R

596 views
Skip to first unread message

Bastien

unread,
May 26, 2019, 2:38:38 PM5/26/19
to LAStools - efficient tools for LiDAR processing
Hi all,

I would like to work with some LAStools's app within the R environment to also get access to the LidR package 'tools.
As LAStools is working with command lines I guess it's possible to call these command lines within R but I don't know exactly how to do it.

For example, I've got a list of .txt files that I want to convert to .las using txt2las within R environment.

The command line from LAStools is :     txt2las -i "C:\LAStools\data\tree1.txt" -parse xyz -odir "C:\LAStools\data" -o "tree1.las"


If someone has an exemple of R script that calls LAStools 's command lines I would be greatfull.

Cheers,

Bastien

Luiz Carlos Estraviz Rodriguez

unread,
May 26, 2019, 4:35:30 PM5/26/19
to lastools
Hi, Bastien.

   I have used the "call-from-inside-R approach" for a long time". It expands immensely the way you can handle the analysis, explore data, graph outcomes etc.

   One very flexible R script that implements the approach could be presented as follows (taking lasinfo as example, but would work with any lastools function):
----------
# Define the path for lastools executables
LAStoolsDir = "C:/LAStools/bin/"
# Define the R function that calls lastools executables
LAStool = function(tool, inputFile, ...){
        cmd = paste(paste(LAStoolsDir, tool, sep=''), '-i', inputFile , ...)
        cat(cmd)
        system(cmd)
        return(cmd)
}
# Define the directory for the ALS project
cloudPath  = "C:/ALS/PRJ_lidar/CLOUDS/"
# Define las/laz files to be processed
inFiles  = paste(cloudPath, '*.laz', sep='')
# Define output directory (and creates it ... if doesn't exist)
outDir = paste(cloudPath, 'info', sep='')
dir.create(outDir, showWarnings = F)
# Call the lastools function
#   (takes lasinfo as example ... any other lastool works fine too!)
cores = 3
LASrun = LAStool('lasinfo', inDir,
                 '-cores', cores,
                 '-cd',
                 '-histo gps_time 10',
                 '-histo intensity 20',
                 '-histo point_source 1',
                 '-histo z 10',
                 '-odir', outDir,
                 '-odix _info',
                 '-otxt -v')
# The rest of the code depends on what your want to do with the output in outDir
...
----------

    Add to it your own R expertise and it will work like "a charm".
    Have fun ...
 
Luiz C. E. Rodriguez
Department of Forest Sciences
Univeristy of Sao Paulo, Brazil
Chairman of Silvilaser 2019


--
Download LAStools at
http://lastools.org
http://rapidlasso.com
Be social with LAStools at
http://facebook.com/LAStools
http://twitter.com/LAStools
http://linkedin.com/groups/LAStools-4408378
Manage your settings at
http://groups.google.com/group/lastools/subscribe
---
You received this message because you are subscribed to the Google Groups "LAStools - efficient tools for LiDAR processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lastools+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lastools/094f6de8-4678-48f8-8760-c7c787a72157%40googlegroups.com.

Luiz Carlos Estraviz Rodriguez

unread,
May 26, 2019, 4:35:43 PM5/26/19
to lastools
Oops ... sorry for one glitch:
replace
LASrun = LAStool('lasinfo', inDir,
with
LASrun = LAStool('lasinfo', inFiles,  

Bastien

unread,
May 27, 2019, 3:29:44 AM5/27/19
to LAStools - efficient tools for LiDAR processing
Hi Luiz,

I just give it a try and it works pretty well!
Thank you very much, it will give me a good start for pushing my analysis further within R!


Cheers,
To unsubscribe from this group and stop receiving emails from it, send an email to last...@googlegroups.com.

Tim Ritter

unread,
May 27, 2019, 3:35:29 AM5/27/19
to processing LAStools - efficient tools for LiDAR, bast....@gmail.com
Hi Bastien,
running LAStools from within R works quite easy using system() and
paste() commands, please find a short example below.
There is only one little trap that you have to avoid: Do not use
linebreaks within the paste() command, otherwise LAStools will not
work...

Best,
Tim

#### Begin: R Example###

# Rotate, reoffset, thin out and change to las
system(paste("C:/Users/tim/Documents/LAStools/bin/txt2las -i
D:/faro/original_txt/Abt30k.xyz -rotate_xy -8.25 0 0 -reoffset 0 0 0
-parse xyz -rescale 0.00001 0.00001 0.00001 -o
D:/faro/ALL_POINTS/temp_all/30k_rot.laz -olaz"))

# translate
system(paste("C:/Users/tim/Documents/LAStools/bin/las2las -i
D:/faro/ALL_POINTS/temp_all/30k_rot.laz -translate_xyz -3329.372
282345.7 0 -reoffset -3329.372 282345.7 0 -o
D:/faro/ALL_POINTS/30k_proj.laz -olaz"))

# lasindex for faster processing
system(paste("C:/Users/bokuadmin/Documents/LAStools/bin/lasindex -i
D:/faro/ALL_POINTS/30k_proj.laz"))

#### End: R Example###

--
Dr. Tim Ritter
Senior Scientist

Fon: +43-1-47654-91414
Fax: +43-1-47654-2991414

Universität für Bodenkultur
Institut für Waldwachstum
Peter-Jordan-Str. 82
1190 Wien
ÖSTERREICH

University of Natural Resources and Life Sciences
Institute of Forest Growth
Peter-Jordan-Str. 82
1190 Vienna
AUSTRIA
--
Download LAStools at
http://lastools.org
http://rapidlasso.com
Be social with LAStools at
http://facebook.com/LAStools
http://twitter.com/LAStools
http://linkedin.com/groups/LAStools-4408378
Manage your settings at
http://groups.google.com/group/lastools/subscribe
---
You received this message because you are subscribed to the Google
Groups "LAStools - efficient tools for LiDAR processing" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to lastools+u...@googlegroups.com.

Michael Treglia

unread,
May 27, 2019, 2:15:04 PM5/27/19
to last...@googlegroups.com, bast....@gmail.com
I'll put out there a GitHub repository with some code for calling lastools from R from a few years back. It's not something I actively maintain, and it's not particularly elegant, but putting it out there in case it's useful for others. 

Hope this helps!
Best regards,
Mike Treglia

Please pardon any typos, this message was sent from a mobile device.

Reply all
Reply to author
Forward
0 new messages