Lastools in Arcpy

769 views
Skip to first unread message

João Paulo Pereira

unread,
Apr 5, 2014, 8:29:30 AM4/5/14
to last...@googlegroups.com
Hello everyone

My master's thesis' methodology uses both Lastools and ArcGIS to isolate tree crowns. 
I'm trying to implement a python routine to make the process more automated.
My problem is, in Lastools I use to create DSM, DTM and CHM. In ArcGIS I apply the rest of the methodology.
My question is, can I call Lastools in python like I do in ArcGIS?
For example, if I want to perform a Focal Statistics procedure, I type on the ArcGIS' python terminal >>>CHMfs3 = FocalStatistics(CHM,NbrRectangle(3,3,'CELL'),"MEAN","NODATA")
How can I use lastools this way?

Thanks

Elke Erhardt

unread,
Apr 10, 2014, 5:15:55 AM4/10/14
to last...@googlegroups.com
Hi,

I have a simalar problem, which might also help to solve your:
I want to use the tool 'lasclip' within my python skript. Lasclip runs perfectly within ArcGIS and creates the Output I want. So I tried to import the toolbox LAStools with the following python code

>import arcpy

>arcpy.ImportToolbox("R:\\software\\lastools\\ArcGIS_toolbox\\LAStools.tbx")

This did not work so I tried to import the Toolbox LAStools Production, which was no problem with the same modified code.

>arcpy.ImportToolbox("R:\\software\\lastools\\ArcGIS_toolbox\\LAStools Production.tbx")

Now I had to use the tool 'lasclipPro' instead of 'lasclip'. My python skript as well as the tool runned directly in ArcGIs seem to run without problems and ended up with the 'Succeeded' rmessage, BUT there is no output!

To solve my problem I have to either find out how to import the toolbox LAStools or find and fix the mistake within the tool 'lasclipPro'.

Can anybody help me??

Thanks

 


Martin Isenburg

unread,
Apr 10, 2014, 7:20:05 AM4/10/14
to LAStools - efficient command line tools for LIDAR processing, Dörffel Günter
Hello,

can anyone help Joao or Elke? I have been using ArcGIS 9.3 (for maximum compatibility) to create the toolboxes and the Python scripts corresponding to the toolboxes are inside the ArcGIS_toolbox folder together with the toolboxes LAStools.tbx and LAStools Production.tbx of th e 'lastools.zip' distribution. You can edit and modify scripts and toolboxes as you see fit. This way of adding LAStools to ArcGIS was originally one of three possible ways of doing that were suggested to me by Günter Dörffel of ESRI Austria (aka SynerGIS). Can the LAStools.tbx toolbox need to be fixed to allow import into ArcPy? My scripts are still using the geoprocessing framework from 9.3 ...

Please share your experience of LAStools in ESRI with other users. Let me reassure you ... despite my recent trash-talking and pranking of ESRI over their evil "LAZ cloning" experiments (*), this is all done in humor (or with the good-intention of a strict parent) and there is certainly no animosity from my side towards those using LAStools together within the ArcGIS framework ... (-; 

Martin @rapidlasso



Staley, Dennis

unread,
Apr 10, 2014, 10:10:46 AM4/10/14
to last...@googlegroups.com, Dörffel Günter
I use LASTools in combination with ArcPy all of the time, but do not use the toolbox.  Instead, I find the standard command line version of LASTools to be suitable for all my needs.  

I build my LASTools command as a string within the script, and can use python variables to batch through several datasets.  the os.system() function (remember to import os first!) can then be used to pass the LASTools command off to the OS for processing.  

No need to import toolboxes at all.

Below is an example cut from some code of mine.  Please be mindful that this code is an example only, and is not intended to be run, as it will not work!!!!  I just wanted to demonstrate how I build LASTools commands within python. 

laz_file, laz_shp, ascii_name, dem_name and shd_name are all variables created in other parts of the script.

Note that I only used windows.  This may be different for Linux, Unix, Mac...


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


import os
import arcpy
arcpy.CheckOutExtension("3D")
arcpy.CheckOutExtension("spatial")

lasbound_command = 'lasboundary -i '+laz_file+' -o '+laz_shp
os.system(lasbound_command)


las2dem_command = 'las2dem -i '+laz_file+' -o '+ascii_name+' -step 1 -utm 13S -keep_class 2 -no_data -9999'
os.system(las2dem_command)

arcpy.ASCIIToRaster_conversion(ascii_name, dem_name, "FLOAT")

zoutHillshade = Hillshade(dem_name)
zoutHillshade.save(shd_name)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hope this helps.

-DS

João Paulo Pereira

unread,
Apr 10, 2014, 10:38:50 AM4/10/14
to last...@googlegroups.com
Staley,

I used something close. I used the command 'from os import startfile' to run a bat file. Apparently, everything was working fine. The first command ran (lasview) and the second command too (lasinfo). When the lastile command came, everything went wrong. For some reason, when the bat file runs, I get an error. For example, instead of reading -files_are_flightlines', it reads ûfiles_are_flightlines. Since û==- is FALSE, it gives me back an error. I'm still working on it. As soon I get something I'll tell you guys.

See ya
Engº. João Paulo Pereira
Forest Engineer
Master's Student in Forest Engineering
State University of Santa Catarina - UDESC
Researcher in the Silviculture and Forest Management Group 
Computer Technician and Software Support
Cell Phone: +55 049 9986-1751


João Paulo Pereira

unread,
Apr 10, 2014, 2:14:48 PM4/10/14
to last...@googlegroups.com
Elke,

I tried to run the same thing you did. Like you said, importing the Lastools toolbox didn't work. I even thought that could be a path or a special character problem. I was able to import Lastools Professional and Production. But a problem came out. Neither of them worked. Even if I use the toolbox, when I call the tool and I try to open a las or a laz file, nothing shows, like in the image attached. As you can see in the folder, I have 8 files, where 7 are laz and one las file. That's why the python command line returns a error, saying that no file was found. Did that happen to you?

Best regards
lastools-arcpy.jpg

João Paulo Pereira

unread,
Apr 10, 2014, 3:19:51 PM4/10/14
to last...@googlegroups.com
I solved the problem with the bat file. I wrote the code in a word file (my thesis) and copied it to the bat. The character '-' in notepad and office word are different. The only thing I had to do was to erase all the '-' and rewrite them on notepad.
Now my python code is working, calling the bat file to execute the whole lastools processing using the command:

          from os import startfile
          os.startfile('C:\\lastools\\bin\\lastools.bat')

The problem with direct usage of lastools through arcpy is still bugged though. 
I
See ya.

John Prater

unread,
Feb 25, 2015, 6:30:43 PM2/25/15
to last...@googlegroups.com
I'm chiming in a little late on this one but I too was having this issue and found a solution that may help others. I was trying to use lasfilter() with Arc 10.2. Simply importing the toolbox with arcpy.AddToolbox() did not work as it says to do in the readme.txt's. I was getting "encoding" runtime errors. Then, I found this thread in a google search and discovered the answer in Isenburg's reply. He (en)codes in 9.3. So, by creating a 9.3 arcscripting module you can get the tool to execute successfully in arcpy 10. 

import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:\install\LAStools\ArcGIS_toolbox\LAStools.tbx")
arcpy.gp.lasfilter(blah, blah, blah)

The readme was correct in its advice to use AddToolbox() but did not indicate the encoding requirements of LasTools. Is this correct? This method worked for me. Maybe everybody already knows this? Hope this is helpful!
Reply all
Reply to author
Forward
0 new messages