2025-02-09 19:23:12 +08:00
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
import subprocess
|
2025-02-09 21:05:33 +08:00
|
|
|
|
from typing import Tuple
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ObjPostProcessor:
|
|
|
|
|
def __init__(self, output_dir: str):
|
|
|
|
|
self.output_dir = output_dir
|
|
|
|
|
self.logger = logging.getLogger('UAV_Preprocess.ObjPostProcessor')
|
|
|
|
|
|
2025-02-09 21:05:33 +08:00
|
|
|
|
def create_metadata_xml(self, osgb_dir: str, lon: float, lat: float):
|
|
|
|
|
"""创建metadata.xml文件,包含地理参考信息
|
|
|
|
|
Args:
|
|
|
|
|
osgb_dir: osgb输出目录
|
|
|
|
|
lon: 中心点经度
|
|
|
|
|
lat: 中心点纬度
|
|
|
|
|
"""
|
2025-02-09 19:23:12 +08:00
|
|
|
|
try:
|
2025-02-09 21:05:33 +08:00
|
|
|
|
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>{lon},{lat},0.000000</SRSOrigin>
|
|
|
|
|
<Texture>
|
|
|
|
|
<ColorSource>Visible</ColorSource>
|
|
|
|
|
</Texture>
|
|
|
|
|
</ModelMetadata>'''
|
|
|
|
|
|
|
|
|
|
# metadata.xml 放在根目录
|
|
|
|
|
metadata_path = os.path.join(osgb_dir, 'metadata.xml')
|
|
|
|
|
with open(metadata_path, 'w', encoding='utf-8') as f:
|
|
|
|
|
f.write(metadata_content)
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
2025-02-09 21:05:33 +08:00
|
|
|
|
self.logger.info(f"已创建metadata.xml: {metadata_path}")
|
|
|
|
|
|
2025-02-09 19:23:12 +08:00
|
|
|
|
except Exception as e:
|
2025-02-09 21:05:33 +08:00
|
|
|
|
self.logger.error(f"创建metadata.xml时发生错误: {str(e)}")
|
2025-02-09 19:23:12 +08:00
|
|
|
|
raise
|
|
|
|
|
|
2025-02-09 21:05:33 +08:00
|
|
|
|
def convert_to_osgb(self, center_coords: Tuple[float, float]):
|
|
|
|
|
"""将obj转换为osgb,并创建metadata.xml
|
|
|
|
|
Args:
|
|
|
|
|
center_coords: (longitude, latitude)中心点经纬度坐标
|
|
|
|
|
"""
|
2025-02-09 19:23:12 +08:00
|
|
|
|
try:
|
2025-02-09 21:05:33 +08:00
|
|
|
|
# 获取合并后的obj文件路径
|
|
|
|
|
obj_dir = os.path.join(self.output_dir, 'texturing')
|
|
|
|
|
obj_file = os.path.join(obj_dir, 'textured_model.obj')
|
|
|
|
|
if not os.path.exists(obj_file):
|
|
|
|
|
raise Exception(f"未找到obj文件: {obj_file}")
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
2025-02-09 21:05:33 +08:00
|
|
|
|
# 创建osgb目录结构
|
|
|
|
|
osgb_dir = os.path.join(self.output_dir, 'osgb')
|
|
|
|
|
osgb_data_dir = os.path.join(osgb_dir, 'Data', 'textured_model')
|
|
|
|
|
os.makedirs(osgb_data_dir, exist_ok=True)
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
2025-02-09 21:05:33 +08:00
|
|
|
|
# 输出文件路径
|
|
|
|
|
output_osgb = os.path.join(osgb_data_dir, 'textured_model.osgb')
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
|
|
|
|
# 构建osgconv命令
|
|
|
|
|
cmd = [
|
|
|
|
|
'osgconv',
|
|
|
|
|
'--compressed',
|
|
|
|
|
'--smooth',
|
|
|
|
|
'--fix-transparency',
|
|
|
|
|
'-o', '0,1,0-0,0,-1',
|
|
|
|
|
obj_file,
|
|
|
|
|
output_osgb
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 执行命令
|
2025-02-09 21:05:33 +08:00
|
|
|
|
self.logger.info(f"执行osgconv命令:{' '.join(cmd)}")
|
2025-02-09 19:23:12 +08:00
|
|
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0:
|
2025-02-09 21:05:33 +08:00
|
|
|
|
raise Exception(f"osgb格式转换失败: {result.stderr}")
|
|
|
|
|
|
|
|
|
|
# 创建metadata.xml
|
|
|
|
|
lon, lat = center_coords
|
|
|
|
|
self.create_metadata_xml(osgb_dir, lon, lat)
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
|
|
|
|
self.logger.info(f"转换完成: {output_osgb}")
|
2025-02-09 21:05:33 +08:00
|
|
|
|
return True
|
2025-02-09 19:23:12 +08:00
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.logger.error(f"转换osgb时发生错误: {str(e)}")
|
2025-02-09 21:05:33 +08:00
|
|
|
|
return False
|