79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
![]() |
import os
|
||
|
import subprocess
|
||
|
import json
|
||
|
import shutil
|
||
|
import logging
|
||
|
|
||
|
class ConvertOBJ:
|
||
|
def __init__(self, output_dir: str):
|
||
|
self.output_dir = output_dir
|
||
|
self.logger = logging.getLogger('UAV_Preprocess.ConvertOBJ')
|
||
|
|
||
|
def convert_grid_obj(self, grid_points):
|
||
|
"""转换每个网格的OBJ文件为OSGB格式"""
|
||
|
os.makedirs(os.path.join(self.output_dir, "osgb"), exist_ok=True)
|
||
|
for grid_id in grid_points.keys():
|
||
|
try:
|
||
|
self._convert_single_grid(grid_id)
|
||
|
except Exception as e:
|
||
|
self.logger.error(f"网格 {grid_id} 转换失败: {str(e)}")
|
||
|
|
||
|
def _convert_single_grid(self, grid_id):
|
||
|
"""转换单个网格的OBJ文件"""
|
||
|
# 1. 构建相关路径
|
||
|
grid_name = f"grid_{grid_id[0]}_{grid_id[1]}"
|
||
|
project_dir = os.path.join(self.output_dir, grid_name, "project")
|
||
|
texturing_dir = os.path.join(project_dir, "odm_texturing")
|
||
|
opensfm_dir = os.path.join(project_dir, "opensfm")
|
||
|
|
||
|
# 检查输入文件是否存在
|
||
|
obj_file = os.path.join(texturing_dir, "odm_textured_model_geo.obj")
|
||
|
if not os.path.exists(obj_file):
|
||
|
raise FileNotFoundError(f"找不到OBJ文件: {obj_file}")
|
||
|
|
||
|
# 2. 执行格式转换
|
||
|
self.logger.info(f"开始转换网格 {grid_id} 的OBJ文件")
|
||
|
output_osgb = os.path.join(texturing_dir, "Tile.osgb")
|
||
|
|
||
|
cmd = (
|
||
|
f"osgconv --compressed --smooth --fix-transparency -o 0,1,0-0,0,-1 "
|
||
|
f"{obj_file} {output_osgb}"
|
||
|
)
|
||
|
|
||
|
try:
|
||
|
subprocess.run(cmd, shell=True, check=True, cwd=texturing_dir)
|
||
|
except subprocess.CalledProcessError as e:
|
||
|
raise RuntimeError(f"OSGB转换失败: {str(e)}")
|
||
|
|
||
|
# 3. 读取地理信息
|
||
|
ref_lla_file = os.path.join(opensfm_dir, "reference_lla.json")
|
||
|
with open(ref_lla_file, 'r') as f:
|
||
|
ref_lla = json.load(f)
|
||
|
|
||
|
# 4. 创建OSGB目录结构
|
||
|
osgb_dir = os.path.join(self.output_dir, "osgb", grid_name)
|
||
|
data_dir = os.path.join(osgb_dir, "Data")
|
||
|
tile_dir = os.path.join(data_dir, "Tile")
|
||
|
os.makedirs(tile_dir, exist_ok=True)
|
||
|
|
||
|
# 5. 创建metadata.xml
|
||
|
metadata_content = f"""<?xml version="1.0" encoding="utf-8"?>
|
||
|
<ModelMetadata version="1">
|
||
|
<!--Spatial Reference System-->
|
||
|
<SRS>EPSG:4326</SRS>
|
||
|
<!--Origin in Spatial Reference System-->
|
||
|
<SRSOrigin>{ref_lla['longitude']},{ref_lla['latitude']},0.000000</SRSOrigin>
|
||
|
<Texture>
|
||
|
<ColorSource>Visible</ColorSource>
|
||
|
</Texture>
|
||
|
</ModelMetadata>"""
|
||
|
|
||
|
metadata_file = os.path.join(osgb_dir, "metadata.xml")
|
||
|
with open(metadata_file, 'w', encoding='utf-8') as f:
|
||
|
f.write(metadata_content)
|
||
|
|
||
|
# 6. 复制OSGB文件
|
||
|
shutil.copy2(output_osgb, os.path.join(tile_dir, "Tile.osgb"))
|
||
|
|
||
|
self.logger.info(f"网格 {grid_id} 转换完成")
|