![](/img/trans.png)
[英]Search for multiple regexes in multiple files and then output each match and its respective file
[英]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.