UAV/post_pro/obj_post_pro.py

103 lines
3.5 KiB
Python
Raw Permalink Normal View History

2025-02-09 19:23:12 +08:00
import os
import logging
import subprocess
2025-02-14 17:31:39 +08:00
from typing import Tuple, Dict
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-14 17:31:39 +08:00
def create_metadata_xml(self, osgb_dir: str, lon: float, lat: float, bounding_box):
2025-02-09 21:05:33 +08:00
"""创建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"?>
2025-02-14 17:31:39 +08:00
<ModelMetadata version="1">
<!-- Spatial Reference System -->
<SRS>EPSG:4326</SRS>
<!-- Center point in Spatial Reference System (in Longitude, Latitude, Height) -->
<SRSOrigin>{lon},{lat},0.000000</SRSOrigin>
<!-- Bounding Box with Two Points -->
<BoundingBox>
<!-- Left-Bottom Corner (Longitude, Latitude) -->
<LB_lon>{bounding_box[0]}</LB_lon>
<LB_lat>{bounding_box[1]}</LB_lat>
<!-- Right-Top Corner (Longitude, Latitude) -->
<RU_lon>{bounding_box[2]}</RU_lon>
<RU_lat>{bounding_box[3]}</RU_lat>
</BoundingBox>
<Texture>
<ColorSource>Visible</ColorSource>
</Texture>
</ModelMetadata>
'''
2025-02-09 21:05:33 +08:00
# 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-14 17:31:39 +08:00
def convert_to_osgb(self, center_lon, center_lat, bounding_box):
2025-02-09 21:05:33 +08:00
"""将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}")
2025-02-14 17:31:39 +08:00
self.logger.info(f"转换完成: {output_osgb}")
2025-02-09 21:05:33 +08:00
# 创建metadata.xml
2025-02-14 17:31:39 +08:00
self.create_metadata_xml(osgb_dir, center_lon, center_lat, bounding_box)
2025-02-09 19:23:12 +08:00
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