Search code examples
python-3.xlidarlaspylas

How to get unique "point source ID's" for 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 what I get is just a min and max point_source_ID values opposed to 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)

Unfortuanetelly the whole process is rather slow, and with a larger dataset 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)