103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
import os
|
||
import logging
|
||
import subprocess
|
||
from typing import Tuple, Dict
|
||
|
||
|
||
class ObjPostProcessor:
|
||
def __init__(self, output_dir: str):
|
||
self.output_dir = output_dir
|
||
self.logger = logging.getLogger('UAV_Preprocess.ObjPostProcessor')
|
||
|
||
def create_metadata_xml(self, osgb_dir: str, lon: float, lat: float, bounding_box):
|
||
"""创建metadata.xml文件,包含地理参考信息
|
||
Args:
|
||
osgb_dir: osgb输出目录
|
||
lon: 中心点经度
|
||
lat: 中心点纬度
|
||
"""
|
||
try:
|
||
metadata_content = f'''<?xml version="1.0" encoding="utf-8"?>
|
||
<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>
|
||
'''
|
||
|
||
# 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)
|
||
|
||
self.logger.info(f"已创建metadata.xml: {metadata_path}")
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"创建metadata.xml时发生错误: {str(e)}")
|
||
raise
|
||
|
||
def convert_to_osgb(self, center_lon, center_lat, bounding_box):
|
||
"""将obj转换为osgb,并创建metadata.xml
|
||
Args:
|
||
center_coords: (longitude, latitude)中心点经纬度坐标
|
||
"""
|
||
try:
|
||
# 获取合并后的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}")
|
||
|
||
# 创建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)
|
||
|
||
# 输出文件路径
|
||
output_osgb = os.path.join(osgb_data_dir, 'textured_model.osgb')
|
||
|
||
# 构建osgconv命令
|
||
cmd = [
|
||
'osgconv',
|
||
'--compressed',
|
||
'--smooth',
|
||
'--fix-transparency',
|
||
'-o', '0,1,0-0,0,-1',
|
||
obj_file,
|
||
output_osgb
|
||
]
|
||
|
||
# 执行命令
|
||
self.logger.info(f"执行osgconv命令:{' '.join(cmd)}")
|
||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||
|
||
if result.returncode != 0:
|
||
raise Exception(f"osgb格式转换失败: {result.stderr}")
|
||
self.logger.info(f"转换完成: {output_osgb}")
|
||
|
||
# 创建metadata.xml
|
||
self.create_metadata_xml(osgb_dir, center_lon, center_lat, bounding_box)
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"转换osgb时发生错误: {str(e)}")
|
||
return False
|