SIMOrthoProgram-Orth_LT1AB-.../Ortho/OrthoMain.py

750 lines
38 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: UTF-8 -*-
"""
@Project microproduct
@File OneOrthoMain.py
@Function :正射校正
@Author KHZ
@Contact
@Date 2021/8/14
@Version 1.0.0
"""
import logging
from tool.algorithm.image.ImageHandle import ImageHandler
from tool.algorithm.xml.CreateMetaDict import CreateMetaDict, CreateProductXml
from tool.algorithm.algtools.PreProcess import PreProcess as pp
import tarfile
from tool.algorithm.xml.AlgXmlHandle import ManageAlgXML, CheckSource # 导入xml文件读取与检查文件
from OrthoAlg import IndirectOrthorectification, DEMProcess,rpc_correction,getRCImageRC,get_RPC_lon_lat,getRCImageRC2
from OrthoAlg import ScatteringAlg as alg
from tool.algorithm.algtools.logHandler import LogHandler
from tool.algorithm.xml.CreatMetafile import CreateMetafile
from OrthoXmlInfo import CreateDict, CreateStadardXmlFile
from osgeo import gdal, osr
import os
import glob
# import gc
import datetime
import shutil
import sys
import scipy #解决打包错误
import scipy.spatial.transform # 用于解决打包错误
import scipy.spatial.transform.rotation
import scipy.spatial.transform._rotation_groups # 用于解决打包错误
DEBUG = True
EXE_NAME = 'Ortho'
#env_str = os.getcwd()
env_str =os.path.dirname(os.path.abspath(sys.argv[0])) #os.path.split(os.path.realpath(__file__))[0]
os.environ['PROJ_LIB'] = env_str
LogHandler.init_log_handler(os.path.join("run_log", EXE_NAME)) # r"run_log\Ortho"
logger = logging.getLogger("mylog")
logger.info(env_str)
class LogHandler2:
"""日志记录工具,用于输出程序运行状况。
这里因为是单程序执行没有必要调用logging 类。
具体日志策略:
1. 最外层使用try,catch 捕捉异常
2. 最后程序执行终止判断。
日志记录格式:
第一行:
TaskID时间输入参数文件地址。
中间:
时间,状态,执行步骤,消息
最后一行:
finished 表示程序执行完成
failed 程序执行出现错误
"""
def __init__(self, sLogPath) -> None:
'''
初始化日志文件
args:
sLogPath:str 日志文件的路径
raise:
IOError:Exception 错误
'''
self.__sLogPath = sLogPath
def loggingStart(self,sTaskID,sParamPath):
'''输出日志开头
TaskID时间输入参数文件地址。
args:
sTaskID:str 任务ID
sParamPath:str 任务参数文件地址
return:
None
'''
sDateTime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") # 执行时间
sOutput="[{}],[{}],[{}]".format(sTaskID, sDateTime, sParamPath)
self.__outputText(sOutput)
pass
def logging(self,sStates,sExecuteStep,sException):
"""输出中间计算信息"""
sDateTime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") # 执行时间
sOutput="[{}],[{}],[{}],[{}]".format(sDateTime,sStates,sExecuteStep,sExecuteStep)
self.__outputText(sOutput)
pass
def __outputText(self,sMessage):
'''将消息输出到最终的日志文件中
'''
with open(self.__sLogPath, 'a', encoding='utf-8') as fp:
fp.write("{}".format(sMessage))
def logggingEnd(self, bSuccessful):
'''
最后一行输出,判断输出的结果是否是正确的
'''
if bSuccessful:
sEndText="\n{}\nfinished".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
else:
sEndText="\n{}\nError".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
self.__outputText(sEndText)
class OrthoMain:
"""
间接定位法正射校正 主函数
"""
def __init__(self, alg_xml_path):
self.alg_xml_path = alg_xml_path
self.imageHandler = ImageHandler()
self.__alg_xml_handler = ManageAlgXML(alg_xml_path)
self.__check_handler = CheckSource(self.__alg_xml_handler)
self.__workspace_path = None
self.__task_id = None
self.__input_paras = {}
# self.__output_paras = {}
self.__in_processing_paras = {}
# self.__out_processing_paras = {}
self.__preprocessed_paras = {}
self.__out_para = None
def check_source(self):
"""
检查算法相关的配置文件,图
像,辅助文件是否齐全
"""
if self.__check_handler.check_alg_xml() is False:
return False
if self.__check_handler.check_run_env() is False:
return False
input_para_names = ["SLC", "DEM", "CorrectMethod"] # //todo 增加检查校正方法
if self.__check_handler.check_input_paras(input_para_names) is False:
return False
self.__workspace_path = self.__alg_xml_handler.get_workspace_path()
self.__task_id = self.__alg_xml_handler.get_task_id()
self.__input_paras = self.__alg_xml_handler.get_input_paras()
# self.__output_paras = self.__alg_xml_handler.get_output_paras()
self.__create_work_space()
self.__in_processing_paras = self.__init_processing_paras(self.__input_paras) # 输入{paranameparavalue}
# self.__out_processing_paras = self.__init_processing_paras(self.__output_paras) # 输入{paranameparavalue}
self.__out_name = os.path.splitext(os.path.splitext(os.path.basename(self.__input_paras['SLC']['ParaValue']))[0])[0]
# AlgorithmName = self.__alg_xml_handler.get_algorithm_name()
# TaskId = self.__alg_xml_handler.get_task_id()
result_name = self.__out_name + ".tar.gz"
self.__out_para = os.path.join(self.__workspace_path, EXE_NAME, 'Output', result_name)
isError, CorrectMethod = self.__check_handler.check_input_paras(['CorrectMethod']) # //todo 获取xml中校正方法 根据不同方法进行结果处理
if CorrectMethod.get('CorrectMethod') == '1' or CorrectMethod.get('CorrectMethod') == 1:
logger.info("CorrectMethod is RPC!")
# self.__out_para=self.__out_para.replace(".tar.gz","_RPC.tar.gz")
self.__out_para=self.__out_para.replace(".tar.gz","-ortho.tar.gz")
elif CorrectMethod.get('CorrectMethod') == '2' or CorrectMethod.get('CorrectMethod') == 2:
logger.info("CorrectMethod is RD!")
# self.__out_para=self.__out_para.replace(".tar.gz","_RD.tar.gz")
self.__out_para=self.__out_para.replace(".tar.gz","-ortho.tar.gz")
else:
raise Exception('No CorrectMethod')
self.__alg_xml_handler.write_out_para("OrthoProduct", self.__out_para) #写入输出参数
logger.info('check_source finished!')
logger.info('progress bar :5')
return True
def __create_work_space(self):
"""
删除原有工作区文件夹,创建新工作区文件夹
"""
self.__workspace_Output_path = os.path.join(self.__workspace_path, EXE_NAME, "Output")
self.__workspace_Temporary_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary")
self.__workspace_unpack_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", "unpack")
self.__workspace_ResampledDEM_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'TestDEM')
self.__workspace_LutImg_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'TestLut')
self.__workspace_IncidenceImg_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'TestInc')
self.__workspace_SimImg_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'TestSim')
self.__workspace_SARIntensity_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'TestSAR')
self.__workspace_package_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", 'package')
self.__workspace_origin_path = os.path.join(self.__workspace_path, EXE_NAME, "Temporary", "origin")
path_list = [self.__workspace_Output_path, self.__workspace_Temporary_path,
self.__workspace_unpack_path, self.__workspace_ResampledDEM_path,
self.__workspace_LutImg_path, self.__workspace_IncidenceImg_path,
self.__workspace_SimImg_path, self.__workspace_SARIntensity_path,
self.__workspace_package_path, self.__workspace_origin_path]
for path in path_list:
if os.path.exists(path):
if DEBUG is True:
continue
self.del_floder(path)
os.makedirs(path)
else:
os.makedirs(path)
logger.info('create new workspace success!')
@staticmethod
def force_del_file(file_path):
"""
强制删除文件
"""
if os.path.isdir(file_path):
for main_dir, subdir, file_name_list in os.walk(file_path):
for filename in file_name_list:
apath = main_dir + filename
# noinspection PyBroadException
try:
os.remove(apath)
except Exception as error: # 使用windows命令行强制删除
os.system("del /f /q %s" % apath)
elif os.path.isfile(file_path) is True:
# noinspection PyBroadException
try:
os.remove(file_path)
except Exception as error: # 使用windows命令行强制删除
os.system("del /f /q %s" % file_path)
@staticmethod
def make_targz(output_filename, source_dir):
"""
一次性打包整个根目录。空子目录会被打包。
如果只打包不压缩,将"w:gz"参数改为"w:""w"即可。
:param output_filename:输出压缩包的完整路径eg:'E:\test.tar.gz'
:param source_dir:需要打包的跟目录eg: 'E:\testFfile\'打包文件夹里面的所有文件,'E:\testFfile'打包文件夹
"""
dir = os.path.split(output_filename)[0]
if os.path.exists(dir) is False:
os.makedirs(dir)
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
@staticmethod
def del_floder(path_data):
"""
删除整个文件夹
"""
if os.path.isdir(path_data):
shutil.rmtree(path_data)
def del_temp_workspace(self):
"""
临时工作区
"""
if DEBUG is True:
return
path = self.__workspace_path + EXE_NAME + r'\Temporary'
if os.path.exists(path):
self.del_floder(path)
def __init_processing_paras(self, names):
"""
:param names:字典列表,每个字典为一个输入产品的配置信息
"""
processing_paras = {}
for name in names:
para = names[name]
if para is None:
logger.error(name + "is None!")
return False
if para['ParaType'] == 'File':
if para['DataType'] == 'File':
para_path = os.path.join(self.__workspace_path, para['ParaValue'])
processing_paras.update({name: para_path})
if para['DataType'] == 'xml':
para_path = os.path.join(self.__workspace_path, para['ParaValue'])
processing_paras.update({name: para_path})
if para['DataType']=="ymal":
para_path = os.path.join(self.__workspace_path, para['ParaValue'])
processing_paras.update({name: para_path})
if para['DataType'] == 'tar.gz':
para_path = os.path.join(self.__workspace_path, para['ParaValue'])
tar_gz_dic = self.__dec_tar_gz(name, para_path, self.__workspace_unpack_path)
processing_paras.update(tar_gz_dic)
if para['DataType'] == 'tif' or para['DataType'] == 'tiff': # 新增修改dem数据为文件绝对路径
if para['ParaValue'] != 'empty' and para['ParaValue'] != 'Empty' and para['ParaValue'] != '':
para_path_list = para['ParaValue'].split(";")
if len(para_path_list) != 0:
dem_path = os.path.join(self.__workspace_origin_path, para['ParaName'])
if os.path.exists(dem_path) is False:
os.mkdir(dem_path)
for file_path in para_path_list:
tif_name = os.path.basename(file_path)
shutil.copy(file_path, os.path.join(dem_path, tif_name))
para_path = os.path.join(self.__workspace_origin_path,para['ParaName'])
processing_paras.update({name: para_path})
elif para['ParaType'] == 'Value':
if para['DataType'] == 'float':
value = float(para['ParaValue'])
processing_paras.update({name: value})
return processing_paras
def __dec_tar_gz(self, name1, tar_gz_path, out_dir):
"""
解压.tar_gz格式景影像文件
:param tar_gz_path:.tar_gz文件路径
:param out_dir:输出文件夹
:return para_dic:全极化影像路径
"""
# 创建文件夹
name = os.path.split(tar_gz_path)[1].rstrip('.tar.gz')
file_dir = os.path.join(out_dir, name + '\\')
if os.path.exists(file_dir) is False:
os.makedirs(file_dir)
# 解压
t = tarfile.open(tar_gz_path)
t.extractall(path=file_dir)
# 获取文件夹内的文件
para_dic = {}
# if os.path.exists(file_dir + name + '\\'):
# meta_xml_paths = list(glob.glob(os.path.join(file_dir + name, '*.meta.xml')))
# para_dic.update({'SLC': file_dir + name})
# else:
# meta_xml_paths = list(glob.glob(os.path.join(file_dir, '*.meta.xml')))
# para_dic.update({'SLC': file_dir})
if os.path.exists(file_dir + name + '\\'):
meta_xml_paths = list(glob.glob(os.path.join(file_dir + name, '*.xml')))
para_dic.update({'SLC': file_dir + name})
else:
meta_xml_paths = list(glob.glob(os.path.join(file_dir, '*.xml')))
para_dic.update({'SLC': file_dir})
if meta_xml_paths == []:
raise Exception('there is not .meta.xml in path: ', file_dir + '\\')
para_dic.update({'META': meta_xml_paths[0]})
self.image_meta_xml = meta_xml_paths
# para_dic.update({name1: file_dir}) # {SLC: file_path}
# 获取文件夹内的文件
hh_flag, hv_flag, vh_flag, vv_flag, dh_flag = 0, 0, 0, 0, 0 #
if os.path.exists(file_dir + name + '\\'):
in_tif_paths = list(glob.glob(os.path.join(file_dir + name + '\\', '*.tif')))
if in_tif_paths == []:
in_tif_paths = list(glob.glob(os.path.join(file_dir + name + '\\', '*.tiff')))
else:
in_tif_paths = list(glob.glob(os.path.join(file_dir, '*.tif')))
if in_tif_paths == []:
in_tif_paths = list(glob.glob(os.path.join(file_dir, '*.tiff')))
for in_tif_path in in_tif_paths:
# 获取极化类型
if 'hh' in os.path.basename(in_tif_path) or 'HH' in os.path.basename(in_tif_path):
hh_flag = 1
elif 'hv' in os.path.basename(in_tif_path) or 'HV' in os.path.basename(in_tif_path):
hv_flag = 1
elif 'vh' in os.path.basename(in_tif_path) or 'VH' in os.path.basename(in_tif_path):
vh_flag = 1
elif 'vv' in os.path.basename(in_tif_path) or 'VV' in os.path.basename(in_tif_path):
vv_flag = 1
elif "DH" in os.path.basename(in_tif_path):
dh_flag = 1
if hh_flag == 0 and hv_flag == 0 and vh_flag == 0 and vv_flag == 0 and dh_flag == 0:
raise Exception('can not found files: HH、HV、VH、VV、DH in path:', tar_gz_path)
self.processinfo = [hh_flag, hv_flag, vh_flag, vv_flag,dh_flag]
return para_dic
def process_handle(self):
isError, CorrectMethod = self.__check_handler.check_input_paras(['CorrectMethod']) # //todo 获取xml中校正方法 根据不同方法进行结果处理
if CorrectMethod.get('CorrectMethod') == '1' or CorrectMethod.get('CorrectMethod') == 1:
logger.info("CorrectMethod is RPC!")
return self.RPC_process_handle()
elif CorrectMethod.get('CorrectMethod') == '2' or CorrectMethod.get('CorrectMethod') == 2:
logger.info("CorrectMethod is RD!")
return self.RD_process_handle()
else:
raise Exception('No CorrectMethod')
def RPC_process_handle(self):
logger.info(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# 1、DEM拼接、裁剪、重采样
Orth_Slc=[]
in_dem_path = self.__in_processing_paras['DEM']
meta_file_path = self.__in_processing_paras['META'] # .meta文件路径
out_dem_path = self.__workspace_ResampledDEM_path
dem_merged_path=DEMProcess.dem_merged(in_dem_path, meta_file_path, out_dem_path) # 生成TestDEM\mergedDEM_VRT.tif
# 2、间接定位法求解行列坐标
slc_paths = self.__in_processing_paras["SLC"]
rpc_slc_path=None
for slc_path in os.listdir(slc_paths):
if slc_path.find(".tiff")>0:
slc_path_temp=os.path.join(slc_paths,slc_path)
# out_power_path=os.path.join(self.__workspace_Temporary_path,slc_path.replace(".tiff","_db.tif").replace("L1A","L1B"))
out_power_path=os.path.join(self.__workspace_Temporary_path,slc_path.replace(".tiff","_db.tif").replace("L1A","L1B").replace("HH","h_h").replace("HV","h_v").replace("VH","v_h").replace("VV","v_v"))
alg.sar_backscattering_coef(slc_path_temp,self.__in_processing_paras['META'],out_power_path)
rpc_slc_path=slc_path_temp.replace(".tiff",".rpc")
if not os.path.exists(rpc_slc_path):
rpc_slc_path=slc_path_temp.replace(".tiff",".rpb")
if not os.path.exists(rpc_slc_path):
logger.error("rpc file Not Found!")
# out_slc_power_path=os.path.join(self.__workspace_package_path,os.path.basename(out_power_path.replace("_db.tif",".tif").replace("L1B","L4")))
out_slc_power_path=os.path.join(self.__workspace_package_path,os.path.basename(out_power_path.replace("_db.tif","-ortho.tif")))
rpc_correction(out_power_path,rpc_slc_path,out_slc_power_path)
break
logger.info('progress bar: 30%')
# 2.1 生成映射表
slc_path=os.path.join(slc_paths,os.listdir(slc_paths)[0])
out_rpc_rc_path = os.path.join(self.__workspace_package_path,"ori_sim-ortho.tif")
get_RPC_lon_lat(out_power_path,out_rpc_rc_path)
#getRCImageRC(slc_path_temp,out_rpc_rc_path,rpc_slc_path)
logger.info('progress bar: 70%')
# 2.2 生成局地入射角
Orthorectification = IndirectOrthorectification(os.path.join(os.path.dirname(__file__),"config.yaml"))
Orthorectification.IndirectOrthorectification(self.__in_processing_paras["SLC"],self.__workspace_package_path) # 改动1
out_incangle_path=os.path.join(self.__workspace_package_path,"inci_Angle-ortho.tif")
out_localincangle_path=os.path.join(self.__workspace_package_path,"LocalincidentAngle-ortho.tif")
out_incangle_geo_path=os.path.join(self.__workspace_package_path,"inc_angle.tif")
out_localincangle_geo_path=os.path.join(self.__workspace_package_path,"LocalincidenceAngle.tif") # 决定入射角名字
Orthorectification.getRPC_incidenceAngle_lon_lat(dem_merged_path,out_rpc_rc_path,self.__workspace_Temporary_path,self.__workspace_package_path,out_incangle_path,out_localincangle_path,out_incangle_geo_path,out_localincangle_geo_path)
# 2.3 输出结果
# self.del_floder(self.__workspace_processing_path)
logger.info('process_handle finished!')
logger.info('progress bar :90')
# 7、打包生成快视图
for tiff_name in os.listdir(self.__workspace_package_path):
if tiff_name.find(".tiff")>0 or tiff_name.find(".tif")>0:
self.imageHandler.write_quick_view(os.path.join(self.__workspace_package_path,tiff_name))
# 1/5、移动原始数据 ------------------------- 这里限制 原始数据是否进入 最终产品列表中
for maindir, subdir, file_name_list in os.walk(slc_paths):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
file_type = apath.split('.')[-1]
if file_type in ["xml"]:
output = os.path.join(self.__workspace_package_path, filename)
shutil.copy(apath, output)
else:
output=os.path.join(self.__workspace_package_path, filename)
shutil.copy(apath, output)
# 生成元文件案例
# xml_path = "./model_meta.xml"
tem_folder=self.__workspace_path + EXE_NAME + r"\Temporary""\\"
image_path=out_slc_power_path# os.path.join(self.__workspace_package_path, "OrthoMapTable.tif")
out_path1 = os.path.join(tem_folder, "trans_geo_projcs.tif")
out_path2 = os.path.join(tem_folder, "trans_projcs_geo.tif")
# par_dict = CreateDict().calu_nature(image_path, self.processinfo, out_path1, out_path2)
#
# dem_path=os.path.join(self.__workspace_ResampledDEM_path, 'mergedDEM.tif')
# out_dem_path1 = os.path.join(tem_folder, "trans_dem_geo_projcs.tif")
# out_dem_path2 = os.path.join(tem_folder, "trans_dem_projcs_geo.tif")
# # par_dict2 = CreateDict().calu_dem_nature(dem_path, dem_meta, out_dem_path1, out_dem_path2, sampling_f, para_A_arr)
# # par_dict2 = CreateDict().calu_dem_nature(dem_path, dem_meta, out_dem_path1, out_dem_path2, sampling_f, para_A_arr)
# par_dict2 = CreateDict().calu_dem_nature(dem_path, out_dem_path1, out_dem_path2, None,Orthorectification.SatelliteOrbitModel.A_arr)
# model_xml_path = os.path.join(self.__workspace_Temporary_path, "creat_standard.meta.xml") # 输出xml路径
# CreateStadardXmlFile(xml_path, self.alg_xml_path, par_dict, par_dict2, model_xml_path).create_standard_xml()
#
# sar_image_meta_xml = list(glob.glob(os.path.join(self.__workspace_package_path, '*.meta.xml')))
# meta_xml_path = os.path.join(self.__workspace_package_path, os.path.basename(self.__out_para).replace(".tar.gz",".meta.xml"))
# CreateMetafile(sar_image_meta_xml[0], self.alg_xml_path, model_xml_path, meta_xml_path).process(os.path.basename(self.__in_processing_paras["SLC"]))
model_path = "./product.xml"
meta_xml_path = os.path.join(self.__workspace_package_path,
os.path.basename(self.__out_para).replace(".tar.gz", ".meta.xml"))
para_dict = CreateMetaDict(image_path, self.__in_processing_paras['META'], self.__workspace_package_path,
out_path1, out_path2).calu_nature()
para_dict.update({"ProductProductionInfo_BandSelection": "1,2"})
para_dict.update({"ProductProductionInfo_AuxiliaryDataDescription": "DEM"})
CreateProductXml(para_dict, model_path, meta_xml_path).create_standard_xml()
# 生成压缩包
logger.info('progress bar :94')
logger.info('start make targz..')
self.del_floder(self.__workspace_unpack_path)
self.del_floder(self.__workspace_ResampledDEM_path)
self.del_floder(self.__workspace_LutImg_path)
self.del_floder(self.__workspace_IncidenceImg_path)
self.del_floder(self.__workspace_SimImg_path)
self.del_floder(self.__workspace_SARIntensity_path)
self.make_targz(self.__out_para, self.__workspace_package_path+"\\")
logger.info('make targz finish')
logger.info('progress bar :100')
return True
pass
def cut_dem(self, dem_merged_path, meta_file_path):
left_up_lon = 0
left_up_lat = 0
def process_sim_ori(self, ori_sim, sim_ori):
scopes = ()
scopes += (ImageHandler.get_scope_ori_sim(ori_sim),)
intersect_polygon = pp().intersect_polygon(scopes)
if intersect_polygon is None:
raise Exception('create intersect shp fail!')
shp_path = os.path.join(self.__workspace_Temporary_path, 'IntersectPolygon.shp')
if pp().write_polygon_shp(shp_path, intersect_polygon, 4326) is False:
raise Exception('create intersect shp fail!')
sim_ori_process = os.path.join(self.__workspace_Temporary_path, 'sim_ori_process.tif')
pp().cut_img(sim_ori_process, sim_ori, shp_path)
return sim_ori_process
def RD_process_handle(self):
# RPC
logger.info(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# 1、DEM拼接、裁剪、重采样
Orth_Slc=[]
in_dem_path = self.__in_processing_paras['DEM']
meta_file_path = self.__in_processing_paras['META'] # .meta文件路径
out_dem_path = self.__workspace_ResampledDEM_path
dem_merged_path=DEMProcess.dem_merged(in_dem_path, meta_file_path, out_dem_path) # 生成TestDEM\mergedDEM_VRT.tif
# self.cut_dem(dem_merged_path, meta_file_path)
# 2、间接定位法求解行列坐标
slc_paths = self.__in_processing_paras["SLC"]
# 2.1 生成映射表
slc_path=os.path.join(slc_paths,os.listdir(slc_paths)[0])
# 2.2 生成局地入射角
path2 = env_str
Orthorectification = IndirectOrthorectification(os.path.join(path2,"config.yaml"))
Orthorectification.IndirectOrthorectification(self.__in_processing_paras["SLC"], self.__workspace_package_path) # 改动1
# 2.3 输出结果
# 3 处理RD
in_slc_path=None
for slc_path in os.listdir(slc_paths):
if slc_path.find(".tiff")>0 and (slc_path.find("_HH_")>0 or slc_path.find("_VV_")>0 or slc_path.find("_DH_")>0):
in_slc_path=os.path.join(slc_paths,slc_path)
break
# 获取校正模型后
Orthorectification.preCaldem_sar_rc(dem_merged_path,in_slc_path,self.__workspace_Temporary_path,self.__workspace_package_path.replace("\\","\\\\")) # 初步筛选坐标范围
logger.info('progress bar: 40%')
# clip_dem_reample_path=os.path.join(self.__workspace_Temporary_path, "SAR_dem.tiff")
# infooption=gdal.InfoOptions("-json")
# clip_dem_tif_info=gdal.Info(clip_dem_reample_path,options=infooption)
# dem_merged_info=gdal.Info(dem_merged_path,options=infooption)
# sampling_f=clip_dem_tif_info['size'][0]/dem_merged_info['size'][0]
out_dir_path=self.__workspace_package_path.replace("\\","\\\\")
this_outSpace_path = out_dir_path
this_out_dem_slantRange_path = out_dir_path + "\\" + "dem_slantRange.tiff"#// 地形斜距
this_out_plant_slantRange_path = out_dir_path + "\\" + "flat_slantRange.tiff"#// 平地斜距
# 保留结果
if(os.path.exists(this_out_dem_slantRange_path)):
os.remove(this_out_dem_slantRange_path)
if(os.path.exists(this_out_plant_slantRange_path)):
os.remove(this_out_plant_slantRange_path)
this_out_dem_rc_path = out_dir_path + "\\" + "WGS_SAR_map.tiff"#// 经纬度与行列号映射
if(os.path.exists(this_out_dem_rc_path)):
os.remove(this_out_dem_rc_path)
this_out_sar_sim_path = out_dir_path + "\\" + "sar_sim.tiff"
if (os.path.exists(this_out_sar_sim_path)):
os.remove(this_out_sar_sim_path)
this_out_sar_sim_wgs_path = out_dir_path + "\\" + "sar_sim_wgs.tiff" # // 经纬度与行列号映射
if (os.path.exists(this_out_sar_sim_wgs_path)):
os.remove(this_out_sar_sim_wgs_path)
this_out_incidence_path = out_dir_path + "\\" + "incidentAngle.tiff"#// 入射角
this_out_localIncidenct_path = out_dir_path + "\\" + "localincidentAngle.tiff"#// 局地入射角
this_out_inc_angle_rpc_path = out_dir_path + "\\" + "RD_incidentAngle.tiff"#// 局地入射角
this_out_local_inc_angle_rpc_path = out_dir_path + "\\" + "RD_localincidentAngle.tiff"#// 局地入射角
if (os.path.exists(this_out_inc_angle_rpc_path)):
shutil.move(this_out_inc_angle_rpc_path, out_dir_path + "\\" + "inci_Angle-ortho.tif")
if (os.path.exists(this_out_local_inc_angle_rpc_path)):
shutil.move(this_out_local_inc_angle_rpc_path, out_dir_path + "\\" + "LocalIncidentAngle-ortho.tif")
if(os.path.exists(this_out_incidence_path)):
shutil.move(this_out_incidence_path,out_dir_path + "\\" + "inc_angle.tif")
if(os.path.exists(this_out_localIncidenct_path)):
shutil.move(this_out_localIncidenct_path,out_dir_path + "\\" + "LocalIncidenceAngle.tif")
this_out_ori_sim_tiff = out_dir_path + "\\" + "RD_ori_sim.tif"#// 局地入射角
if (os.path.exists(this_out_ori_sim_tiff)):
shutil.move(this_out_ori_sim_tiff, out_dir_path + "\\" + "ori_sim-ortho.tif")
this_out_sim_ori_tiff = out_dir_path + "\\" + "RD_sim_ori.tif" # // 局地入射角
if (os.path.exists(this_out_sim_ori_tiff)):
shutil.move(this_out_sim_ori_tiff, out_dir_path + "\\" + "sim_ori-ortho.tif")
# GTC 入射角
GTC_rc_path=os.path.join(self.__workspace_package_path,"ori_sim-ortho.tif")
GTC_out_path=self.__workspace_package_path
parameter_path = os.path.join(self.__workspace_package_path, "orth_para.txt")
this_in_rpc_lon_lat_path = os.path.join(self.__workspace_package_path, "ori_sim-ortho.tif")
dem_rc = os.path.join(self.__workspace_package_path, "sim_ori-ortho.tif")
dem_rc_pro = self.process_sim_ori(this_in_rpc_lon_lat_path, dem_rc)
shutil.move(dem_rc_pro, dem_rc)
in_tif_paths = list(glob.glob(os.path.join(slc_paths, '*.tiff')))
for in_tif_path in in_tif_paths:
out_sar_path = os.path.join(GTC_out_path, os.path.split(in_tif_path)[1])
slc_path_temp=os.path.join(slc_paths,in_tif_path)
out_power_path = os.path.join(self.__workspace_Temporary_path,
slc_path_temp.replace(".tiff", "-lin.tif").replace("L1A", "L1B")).replace(
"HH", "h_h").replace("HV", "h_v").replace("VH", "v_h").replace("VV", "v_v").replace("DH", "d_h")
# out_power_path=os.path.join(self.__workspace_Temporary_path,slc_path_temp.replace(".tiff","_db.tif"))
alg.sar_backscattering_coef(slc_path_temp, self.__in_processing_paras['META'], out_power_path)
lin_tif_path = os.path.join(self.__workspace_Temporary_path,
os.path.basename(out_power_path).split('-')[0] + "-lin_geo.tif")
# Orthorectification.calInterpolation_cubic_Wgs84_rc_sar_sigma(parameter_path, dem_rc,
# out_power_path,
# lin_tif_path)
Orthorectification.calInterpolation_bil_Wgs84_rc_sar_sigma(parameter_path, dem_rc,
out_power_path,
lin_tif_path)
tempout_tif_path = os.path.join(self.__workspace_package_path,
os.path.basename(lin_tif_path).split('-')[0] + "-ortho.tif")
alg.lin_to_db(lin_tif_path, tempout_tif_path) # 线性值转回DB值
# temp_slc_path=os.path.join(self.__workspace_package_path, os.path.basename(out_power_path))
# temp_slc_path=temp_slc_path.replace("_db.tif","-ortho.tif")
#inter_Range2Geo(self,lon_lat_path , data_tiff , grid_path , space)
# Orthorectification.inter_Range2Geo(GTC_rc_path,out_power_path,temp_slc_path,Orthorectification.heightspace)
# Orthorectification.calInterpolation_cubic_Wgs84_rc_sar_sigma(parameter_path, dem_rc, out_power_path, temp_slc_path) #
break
#Orth_Slc.append(temp_slc_path)
# power_list.append(out_power_path)
for tiff_name in os.listdir(self.__workspace_package_path):
if tiff_name.find(".tiff")>0 or tiff_name.find(".tif")>0:
self.imageHandler.write_quick_view(os.path.join(self.__workspace_package_path,tiff_name))
# 1/5、原始数据中的.incidence.xml、.meta.xml输出影像不输出
for maindir, subdir, file_name_list in os.walk(slc_paths):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
file_type = apath.split('.')[-1]
if file_type in ["xml"]:
output = os.path.join(self.__workspace_package_path, filename)
shutil.copy(apath, output)
else:
output=os.path.join(self.__workspace_package_path, filename)
shutil.copy(apath, output)
# 2/5、正射的成果图
# for maindir, subdir, file_name_list in os.walk(self.__workspace_package_path):
# for filename in file_name_list:
# apath = os.path.join(maindir, filename)
# file_type = filename.split('.')[-1]
# image_name = os.path.splitext(filename)[0]
#self.imageHandler.write_quick_view(output_OrthoResult) # 快视图
# 生成元文件案例
# xml_path = "./model_meta.xml"
tem_folder=self.__workspace_path + EXE_NAME + r"\Temporary""\\"
image_path=tempout_tif_path# os.path.join(self.__workspace_package_path, "OrthoMapTable.tif")
out_path1 = os.path.join(tem_folder, "trans_geo_projcs.tif")
out_path2 = os.path.join(tem_folder, "trans_projcs_geo.tif")
# par_dict = CreateDict().calu_nature(image_path, self.processinfo, out_path1, out_path2)
#
# dem_path=os.path.join(self.__workspace_ResampledDEM_path, 'mergedDEM.tif')
# out_dem_path1 = os.path.join(tem_folder, "trans_dem_geo_projcs.tif")
# out_dem_path2 = os.path.join(tem_folder, "trans_dem_projcs_geo.tif")
# # par_dict2 = CreateDict().calu_dem_nature(dem_path, dem_meta, out_dem_path1, out_dem_path2, sampling_f, para_A_arr)
# par_dict2 = CreateDict().calu_dem_nature(dem_path, out_dem_path1, out_dem_path2, sampling_f,Orthorectification.SatelliteOrbitModel.A_arr)
# model_xml_path = os.path.join(self.__workspace_Temporary_path, "creat_standard.meta.xml") # 输出xml路径
# CreateStadardXmlFile(xml_path, self.alg_xml_path, par_dict, par_dict2, model_xml_path).create_standard_xml()
#
# sar_image_meta_xml = list(glob.glob(os.path.join(self.__workspace_package_path, '*.meta.xml')))
# meta_xml_path = os.path.join(self.__workspace_package_path, os.path.basename(self.__out_para).replace(".tar.gz",".meta.xml"))
# CreateMetafile(sar_image_meta_xml[0], self.alg_xml_path, model_xml_path, meta_xml_path).process(os.path.basename(self.__in_processing_paras["SLC"]))
model_path = "./product.xml"
meta_xml_path = os.path.join(self.__workspace_package_path, os.path.basename(self.__out_para).replace(".tar.gz",".meta.xml"))
para_dict = CreateMetaDict(image_path, self.__in_processing_paras['META'], self.__workspace_package_path, out_path1, out_path2).calu_nature()
para_dict.update({"imageinfo_ProductName": '正射校正'})
para_dict.update({"imageinfo_ProductIdentifier": 'Ortho'})
para_dict.update({"imageinfo_ProductLevel": '3A'})
para_dict.update({"ProductProductionInfo_BandSelection": "1,2"})
para_dict.update({"ProductProductionInfo_AuxiliaryDataDescription": "DEM"})
CreateProductXml(para_dict, model_path, meta_xml_path).create_standard_xml()
temp_folder = os.path.join(self.__workspace_path, EXE_NAME, 'Output')
out_xml = os.path.join(temp_folder, os.path.basename(meta_xml_path))
if os.path.exists(temp_folder) is False:
os.mkdir(temp_folder)
# CreateProductXml(para_dict, model_path, out_xml).create_standard_xml()
shutil.copy(meta_xml_path, out_xml)
# 生成压缩包
logger.info('progress bar :94')
logger.info('start make targz..')
self.del_floder(self.__workspace_unpack_path)
self.del_floder(self.__workspace_ResampledDEM_path)
self.del_floder(self.__workspace_LutImg_path)
self.del_floder(self.__workspace_IncidenceImg_path)
self.del_floder(self.__workspace_SimImg_path)
self.del_floder(self.__workspace_SARIntensity_path)
self.make_targz(self.__out_para, self.__workspace_package_path+"\\")
logger.info('make targz finish')
logger.info('progress bar :100')
return True
pass
if __name__ == '__main__':
DEBUG=False
if '-DEBUG' in sys.argv:
DEBUG=True
start = datetime.datetime.now()
try:
if len(sys.argv) < 2:
xml_path = 'Ortho.xml'
else:
xml_path = sys.argv[1]
OrthoMain = OrthoMain(xml_path)
if OrthoMain.check_source() is False:
raise Exception('check_source() failed!')
if OrthoMain.process_handle() is False:
raise Exception('check_source() failed!')
logger.info('successful production of ortho products!')
except Exception:
logger.exception("run-time error!")
finally:
OrthoMain.del_temp_workspace()
pass
end = datetime.datetime.now()
logger.info('running use time: %s ' % (end - start))