Python Laspy: How to Append .las Files Efficiently

arcgis-prolaslaspylidarpython

I am attempting to append .las files to another .las file using laspy. Basically, I have a collection of .las files that I need to merge into one large .las file (it's for input into a different software program). I have found the lasappender.py document, but am having a hard time parsing through the code. Does anyone know how to do this function? Any code snippets would be helpful.

Here is the code I have so far:

import os
import numpy as np
import laspy
import arcpy
import glob

try:
    #folder with las files
    workspace = 'C:/LidarServe/testing/python/input'
    
    #get list of .las files
    lasFiles = glob.glob(os.path.join(workspace, '*.las'))  
    print(lasFiles)
    
except:
    print (arcpy.GetMessages())

#open las files list
with laspy.open(lasFiles[0], mode='a') as outlas:
    with laspy.open(lasFiles) as inlas:
        for points in inlas.chunk_iterator(2_000_000):
            #append las files to lasFile[0]
            outlas.append_points(points)

Here is the error message I am getting at this point in time:
AttributeError: 'list' object has no attribute 'read'

Best Answer

This code will append all of the las files to an existing las file. Be sure not to store the receiving las in the same directory as the folder of las files.

Here I have taken one of my las file and renamed the file to all_point.las and stored that file in another location. Then I append all of the las files in the directory to all_points.las. Tested on Python 3.6 on Ubuntu Linux (but it should run fine on Windows if Python is configured correctly).

#!/usr/bin/env python3

import sys
import traceback
import laspy
import os
try:
    print('Running Merge LAS')

    #This is the las file to append to.  DO NOT STORE THIS FILE IN THE SAME DIRECTORY AS BELOW...
    out_las = '/home/gerry/all_points.las'
    #this is a directory of las files
    inDir = '/home/gerry/lidarcloud/'    


    def append_to_las(in_laz, out_las):
        with laspy.open(out_las, mode='a') as outlas:
            with laspy.open(in_las) as inlas:
                for points in inlas.chunk_iterator(2_000_000):
                    outlas.append_points(points)


    for (dirpath, dirnames, filenames) in os.walk(inDir):
        for inFile in filenames:
            if inFile.endswith('.las'):
                in_las = os.path.join(dirpath, inFile)
                append_to_las(in_las, out_las)
        
        
    print('Finished without errors - merge_LAS.py')
except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    print('Error in append las')
    print ("PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError     Info:\n" + str(sys.exc_info()[1]))