1

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'

2 Answers 2

2
+100

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]))    
9
  • Apparently laspy does not have an attribute open. AttributeError: module 'laspy' has no attribute 'open' Sep 3, 2021 at 20:00
  • 1
    That code executed without error for me. Laspy.open is in the documentation: laspy.readthedocs.io/en/latest/basic.html. Maybe try to reinstall laspy? Confirm that pip is installing laspy where you expect it to be? Include your code for inspection.
    – GBG
    Sep 5, 2021 at 1:51
  • This works well for appending two las files together, but I have a folder full of them. Do you know how that could be done? Sep 6, 2021 at 19:09
  • 1
    I have edited my answer but you should head over to stackoverflow for advice on using Python to get all of the files of a certain type.
    – GBG
    Sep 7, 2021 at 15:52
  • The glob line in the script I wrote grabs all of the las files, so that should work. I'm trying to find out how to iterate through the files during the append function, since the append function only takes in two las files at this point. Sep 7, 2021 at 16:36
0

I don't have enough reputation to comment but for those who are looking to perform some manipulation on different .las files and then merge the result in a single cloud, in order to use .append_points(), you first need to create the file and the easier way is by copying the header and points of the first chunk (the alternative would be to use laspy.create() but it can be messy very quickly if you have a lot of extra dimensions as it was for me).

Here's some minimal code:

def some_function_working_on_a_list_of_las_files(las_list, merged_output):
    for las_file in las_list:
        las = laspy.read(las_file)
        # manipulate your las and end up with a sub_las
        sub_las = ...
        if not merged_output.is_file():
            # For the first iteration we need to create the file as
            # a copy of the first subsample points
            # in order to make append_points() to work
            las_merged = laspy.LasData(sub_las.header)
            las_merged.points = sub_las.points.copy()
            las_merged.write(merged_output)
        else:
            with laspy.open(merged_output, mode="a") as dst:
                dst.append_points(sub_las.points)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.