繁体   English   中英

选择多个文件进行输入并获得相应的输出

[英]Selecting multiple files for input and getting respective output

因此,我有这段代码,可以从Lidar Pointcloud中裁剪出树的shapefile。 对单个shapefile执行此操作时,效果很好。

我想做的是:我有180个单独的树shapefile,并希望将每个文件从同一个pointcloud中裁剪出来,并将其另存为单独的.las文件。 所以最后我应该有180个.las文件。 例如Input_shp:Tree11.shp-> Output_las:Tree11.las

我敢肯定,有一种方法可以一次完成所有这些操作。 我只是不知道如何选择所有shapefile并将输出保存到180个单独的.las文件。

我对Python真的很陌生,任何帮助将不胜感激。

我已经尝试使用占位符(.format())来实现此目的,但实际上并没有到达任何地方。

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")

我没有正在使用的插件,但您可能正在寻找以下代码片段:

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"
    )

这利用了python格式的字符串模板
以及os.listdir函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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