[英]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. 因此,我有这段代码,可以从Lidar Pointcloud中裁剪出树的shapefile。 When doing this for a single shapefile it works well.
对单个shapefile执行此操作时,效果很好。
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. 我想做的是:我有180个单独的树shapefile,并希望将每个文件从同一个pointcloud中裁剪出来,并将其另存为单独的.las文件。 So in the end I should have 180 .las files.
所以最后我应该有180个.las文件。 Eg Input_shp: Tree11.shp -> Output_las: Tree11.las
例如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.
我只是不知道如何选择所有shapefile并将输出保存到180个单独的.las文件。
Im really new to Python and any help would be appreciated. 我对Python真的很陌生,任何帮助将不胜感激。
I already tried to get this with placeholders (.format()) but couldnt really get anywhere. 我已经尝试使用占位符(.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")
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 . 这利用了python格式的字符串模板 。
Along with the os.listdir function. 以及os.listdir函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.