UAV/post_pro/merge_laz.py

74 lines
2.4 KiB
Python
Raw Normal View History

2024-12-31 15:01:47 +08:00
import os
import logging
import numpy as np
from typing import Dict, Tuple
import pandas as pd
import subprocess
2025-01-13 14:32:36 +08:00
import shutil
2024-12-31 15:01:47 +08:00
class MergePly:
def __init__(self, output_dir: str):
self.output_dir = output_dir
self.logger = logging.getLogger('UAV_Preprocess.MergePly')
2025-01-02 17:21:23 +08:00
def merge_grid_laz(self, grid_points: Dict[tuple, pd.DataFrame]):
"""合并所有网格的点云数据"""
if len(grid_points) == 1:
2025-01-13 14:32:36 +08:00
grid_id = list(grid_points.keys())[0]
shutil.copy2(os.path.join(self.output_dir,
f"grid_{grid_id[0]}_{grid_id[1]}",
"project",
"odm_georeferencing",
"odm_georeferenced_model.laz"),
os.path.join(self.output_dir,
"pointcloud.laz")
)
2024-12-31 15:01:47 +08:00
try:
2025-01-02 17:21:23 +08:00
# 获取所有点云文件路径
laz_files = []
2024-12-31 21:37:44 +08:00
for grid_id, points in grid_points.items():
2025-01-02 17:21:23 +08:00
laz_path = os.path.join(
2024-12-31 15:01:47 +08:00
self.output_dir,
2024-12-31 21:37:44 +08:00
f"grid_{grid_id[0]}_{grid_id[1]}",
2024-12-31 15:01:47 +08:00
"project",
"odm_georeferencing",
"odm_georeferenced_model.laz"
)
2025-01-02 17:21:23 +08:00
if os.path.exists(laz_path):
laz_files.append(laz_path)
else:
2025-01-13 14:32:36 +08:00
self.logger.warning(
f"网格 ({grid_id[0]},{grid_id[1]}) 的点云文件不存在")
2024-12-31 15:01:47 +08:00
kwargs = {
2025-01-02 17:21:23 +08:00
'all_inputs': " ".join(laz_files),
2025-01-09 11:45:55 +08:00
'output': os.path.join(self.output_dir, 'pointcloud.laz')
2024-12-31 21:37:44 +08:00
}
2024-12-31 15:01:47 +08:00
2025-01-13 14:32:36 +08:00
subprocess.run(
'D:\\software\\LAStools\\bin\\lasmerge64.exe -i {all_inputs} -o "{output}"'.format(**kwargs))
2024-12-31 15:01:47 +08:00
except Exception as e:
self.logger.error(f"PLY点云合并过程中发生错误: {str(e)}", exc_info=True)
raise
if __name__ == "__main__":
from utils.logger import setup_logger
# 设置输出目录和日志
output_dir = r"G:\ODM_output\1009"
setup_logger(output_dir)
# 构造测试用的grid_points字典
grid_points = {
2024-12-31 21:37:44 +08:00
(0, 0): [], # 不再需要GPS点信息
(0, 1): []
2024-12-31 15:01:47 +08:00
}
# 创建MergePly实例并执行合并
merge_ply = MergePly(output_dir)
2024-12-31 21:37:44 +08:00
merge_ply.merge_grid_laz(grid_points)