简体   繁体   English

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

[英]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.

相关问题 在多个文件中搜索多个正则表达式,然后输出每个匹配项及其各自的文件 - Search for multiple regexes in multiple files and then output each match and its respective file 在Python中使用多个输入和输出文件 - Working with multiple input and output files in Python askopenfilenames选择多个文件 - askopenfilenames selecting multiple files 使用成对的输入文件和多个输出文件编写bash或python for循环 - Writing a bash or python for loop with paired input files and multiple output files 将for循环的输出写入具有与输入文件相同文件名的多个文件 - Write output of for loop to multiple files with same filename as input file 来自biopython AlignIO的多个输入文件作为单个文件输出 - Multiple input files as single file output from biopython AlignIO 输入文本文件并使用Python编写多个输出文件 - Input a text file and write multiple output files in Python pyspark - 将多个输入文件合并为一个RDD和一个输出文件 - pyspark - multiple input files into one RDD and one output file 如何使用不同的输入和输出文件多次运行相同的脚本? - How to run the same script multiple times with different input and output files? 选择文件夹中的多个文件 - Selecting multiple files within a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM