jblindsay / whitebox-tools

An advanced geospatial data analysis platform
https://www.whiteboxgeo.com/
MIT License
964 stars 160 forks source link

process multple files with lidar_digital_surface_model in WhiteboxTools via Python #274

Closed sehHeiden closed 8 months ago

sehHeiden commented 2 years ago

I was using the lidar_digital_surface_model to compute lidar into raster files. It worked very well for single files. Besides that the lidar file did hat a crs and hence the raster file, too. Therefore it would be nice, to add the crs in whitebox.

The real problem I had is with multiprocessing. Following the documentation the the function can also work an multiple files at once.

I used an example folder with 9 .laz files (instead of 3000+ for the real project). I tried to:

I would have liked, when the first version were the default for multiple files.

I am using 3.9 with Pycharm.

A hot fix for me was to use multy processing in Python.

Afrancioni commented 8 months ago

Hi @sehHeiden

You certainly can process files in "batch" mode in either WhiteboxRunner or using Python where you can run a tool on multiple files from a working directory.

Previously I have done so in python where I create a function to gather files from an input directory with similar files types. In this example I am collecting all files with the .laz extension.

def find_files (input_directory, processed_files): files = os.listdir(input_directory) file_names = [] for f in files: if f.endswith(".las") or f.endswith(".laz") and f not in processed_files: #if filename is a .las or .zlidar file and not already processed, append the file to the list file_names.append(f) return(file_names)

I call the function in my python script file_names = find_files(input_directory, processed_files)

and then I process all input files

` if flag : for i in range (len(file_names)): in_file = os.path.join(input_directory, file_names[i]) # creates the input file name by joining the path with the file name

        pre, ext = os.path.splitext(file_names[i])

        out_file = os.path.join(output_directory, file_names[i].replace(ext, "_classified.las"))  # creates the out file name by joining the path with the file name... change the file type to either .tif depending on the analysis 
        print("Processing LiDARTins {} OF {} (total classified = {})".format(i+1, len(file_names), num_classified))

        wbt.classify_lidar(
            i=in_file, 
            output=out_file, 
            radius=1.5, 
            grd_threshold=0.1, 
            oto_threshold=2.0, 
            planarity_threshold=0.85, 
            linearity_threshold=0.70, 
            iterations=30, 
            facade_threshold=0.5
        )
    else:
        flag = False   

print("Complete")`