python-3.xlidarlaspylas

How can I get unique "point source ID's" for a Lidar block?


I'm trying to get information about unique flightlines appearing in a block of Lidar data using a laspy.

I have already tried running a lasInfo module for the whole block, but I just get minimum and maximum point_source_ID values opposed to the list of individual flightlines, which I need.

This is what I've tried so far:

import laspy
import glob

las_files_list = glob.glob(r'PATH\*.las')
print(las_files_list)

las_source_id_set = set()

for f in las_files_list:
    las_file = laspy.file.File(f, mode='r')
    las_source_id_list = las_file.pt_src_id

    for i in las_source_id_list:
        las_source_id_set.add(i)

    las_file.close()
    print(las_source_id_set, ' ', f)

print(las_source_id_set)

with open('point_source_id.txt', 'w') as f:
    f.write(las_source_id_set)

Unfortunately, the whole process is rather slow, and with a larger data set I get a stack overflow error and eventually never get to the 'write a file' part.


Solution

  • The process is slower than it could be, because you are doing a loop in Python.

    There is a NumPy function that you can use to make the process faster: numpy.unique

    Your script would become:

    import laspy
    import glob
    import numpy as np
    
    las_files_list = glob.glob(r'PATH\*.las')
    print(las_files_list)
    
    las_source_id_set = set()
    
    for f in las_files_list:
        with laspy.file.File(p) as las:
            las_source_id_set.update(np.unique(las.pt_src_id))
    
        print(las_source_id_set, ' ', f)
    
    print(las_source_id_set)
    
    with open('point_source_id.txt', 'w') as f:
        f.write(las_source_id_set)