简体   繁体   中英

Selecting multiple files for input and getting respective output

So I have this bit of code, which clips out a shapefile of a tree out of a Lidar Pointcloud. When doing this for a single shapefile it works well.

What I want to do: I have 180 individual tree shapefiles and want to clip every file out of the same pointcloud and save it as a individual .las file. So in the end I should have 180 .las files. Eg Input_shp: Tree11.shp -> Output_las: Tree11.las

I am sure that there is a way to do all of this at once. I just dont know how to select all shapefiles and save the output to 180 individual .las files.

Im really new to Python and any help would be appreciated.

I already tried to get this with placeholders (.format()) but couldnt really get anywhere.

from WBT.whitebox_tools import WhiteboxTools

wbt = WhiteboxTools()

wbt.work_dir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.clip_lidar_to_polygon(i="Pointcloud_to_clip.las", polygons="tree_11.shp", output="Tree11.las")

I don't have the plugin you are using, but you may be looking for this code snippet:

from WBT.whitebox_tools import WhiteboxTools

wbt = WhiteboxTools()

workDir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.work_dir = workDir

# If you want to select all the files in your work dir you can use the following.
# though you may need to make it absolute, depending on where you run this:
filesInFolder = os.listDir(workDir)
numberOfShapeFiles = len([_ for _ in filesInFolder if _.endswith('.shp')])

# assume shape files start at 0 and end at n-1
# loop over all your shape files.
for fileNumber in range(numberOfShapeFiles):
    wbt.clip_lidar_to_polygon(
        i="Pointcloud_to_clip.las",
        polygons=f"tree_{fileNumber}.shp",
        output=f"Tree{fileNumber}.las"
    )

This makes use of python format string templates .
Along with the os.listdir function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM