修复merge_tif的bug

This commit is contained in:
龙澳 2025-01-14 09:09:19 +08:00
parent 1c24ca7e4f
commit 46346e528f

View File

@ -108,6 +108,7 @@ class MergeTif:
input_tif1, input_tif2 = None, None input_tif1, input_tif2 = None, None
merge_count = 0 merge_count = 0
temp_files = []
try: try:
for grid_id, points in grid_points.items(): for grid_id, points in grid_points.items():
@ -127,8 +128,8 @@ class MergeTif:
# 如果文件大于600MB则不使用original文件 # 如果文件大于600MB则不使用original文件
file_size_mb = os.path.getsize( file_size_mb = os.path.getsize(
grid_tif) / (1024 * 1024) # 转换为MB grid_tif) / (1024 * 1024) # 转换为MB
file_name = file_name.replace(".original", "")
if file_size_mb > 600: if file_size_mb > 600:
file_name = file_name.replace(".original", "")
grid_tif = os.path.join( grid_tif = os.path.join(
self.output_dir, self.output_dir,
f"grid_{grid_id[0]}_{grid_id[1]}", f"grid_{grid_id[0]}_{grid_id[1]}",
@ -158,38 +159,20 @@ class MergeTif:
self.merge_two_tifs(input_tif1, input_tif2, temp_output) self.merge_two_tifs(input_tif1, input_tif2, temp_output)
merge_count += 1 merge_count += 1
# 如果之前的输入文件是临时文件,则删除它
if 'temp_merged_' in input_tif1:
try:
os.remove(input_tif1)
except Exception as e:
self.logger.warning(f"删除临时文件失败: {str(e)}")
input_tif1 = temp_output input_tif1 = temp_output
input_tif2 = None input_tif2 = None
temp_files.append(temp_output)
# 重命名最终的临时文件为目标文件名
final_output = os.path.join( final_output = os.path.join(
self.output_dir, product_info['output']) self.output_dir, product_info['output'])
if os.path.exists(final_output):
try:
os.remove(final_output)
except Exception as e:
self.logger.warning(f"删除已存在的最终输出文件失败: {str(e)}")
final_output = os.path.join(
self.output_dir,
f"merged_{int(time.time())}_{product_info['output']}"
)
try:
os.rename(input_tif1, final_output)
except Exception as e:
self.logger.warning(f"重命名最终文件失败: {str(e)}")
shutil.copy2(input_tif1, final_output) shutil.copy2(input_tif1, final_output)
# 清理所有临时文件
for temp_file in temp_files:
try: try:
os.remove(input_tif1) os.remove(temp_file)
except: except Exception as e:
pass self.logger.warning(f"删除临时文件失败: {str(e)}")
self.logger.info( self.logger.info(
f"{product_name}合并完成,共执行 {merge_count} 次合并," f"{product_name}合并完成,共执行 {merge_count} 次合并,"