From abc8fe56949751e07c6ee06bab0f1c4d42d7d635 Mon Sep 17 00:00:00 2001 From: duxin Date: Tue, 15 Sep 2026 10:34:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20GTiff=20=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=BC=BA=E5=A4=B1=20.hdr=20=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E6=B3=A2=E9=95=BF=E5=85=83=E6=95=B0=E6=8D=AE=E6=96=AD?= =?UTF-8?q?=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit copy_hdr_info 原实现要求目标 .hdr 已存在,但 GTiff/BIGTIFF 输出时 GDAL 不会自动生成 .hdr,导致波长信息丢失、下游采样无法解析波段。 改为:源 .hdr 候选路径兼容 x.hdr / x.bip.hdr / x.HDR 等多种命名;目标无 .hdr 时直接以源 .hdr 为基底旁置创建,已存在时仍走 write_fields_to_hdrfile 合并逻辑。 --- src/core/utils/gdal_helper.py | 60 +++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/src/core/utils/gdal_helper.py b/src/core/utils/gdal_helper.py index abc672c..06e694e 100644 --- a/src/core/utils/gdal_helper.py +++ b/src/core/utils/gdal_helper.py @@ -276,34 +276,62 @@ def save_bands_as_image(corrected_bands: list, output_path: str, def copy_hdr_info(source_img_path: str, dest_img_path: str) -> bool: """ - 复制原始影像的hdr文件信息(如波长等)到目标影像的hdr文件 + 将源影像的 .hdr 元数据(尤其是波长)继承到目标影像。 + + ★ 断链修复:若目标影像无 .hdr(如 GTiff/BIGTIFF 输出,GDAL 不自动生成), + 主动以源 .hdr 为基底创建旁置 .hdr,保证下游(采样等)能读取波长; + 若目标 .hdr 已存在(ENVI 场景),走原有合并逻辑 write_fields_to_hdrfile。 Args: - source_img_path: 源影像文件路径(原始bsq文件) - dest_img_path: 目标影像文件路径(去耀斑后的bsq文件) + source_img_path: 源影像文件路径(原始 bsq/bip 等) + dest_img_path: 目标影像文件路径(去耀斑后的 bsq 等) Returns: bool: 是否成功 """ + import shutil + if not UTIL_AVAILABLE: print("警告: util模块未导入,无法复制hdr文件信息") return False + # 1) 定位源 .hdr(兼容 x.hdr / x.bip.hdr / x.HDR 等命名习惯) + src_hdr = None + src_candidates = [] + _existing = get_hdr_file_path(source_img_path) + if _existing: + src_candidates.append(_existing) + _p = Path(source_img_path) + for _suff in ('.hdr', '.HDR'): + src_candidates.append(str(_p.with_suffix(_suff))) + src_candidates.append(str(Path(str(_p) + _suff))) + for _cand in src_candidates: + if _cand and Path(_cand).exists(): + src_hdr = Path(_cand) + break + if src_hdr is None: + print(f"警告: 源影像无 .hdr 文件,无法提取波长元数据: {source_img_path}") + return False + + # 2) 目标 .hdr 路径:以目标数据文件同名基底 + .hdr + dest_hdr_path = Path(os.path.splitext(dest_img_path)[0] + '.hdr') + + # 3) ★ 核心修复:目标 .hdr 不存在(如 GTiff 场景)→ 直接旁置创建 + if not dest_hdr_path.exists(): + print(f"提示: 目标未自动生成 .hdr (如 GTiff 格式),正在主动创建旁置波长元数据: " + f"{dest_hdr_path}") + try: + shutil.copy2(str(src_hdr), str(dest_hdr_path)) + return True + except Exception as e: + print(f"错误: 旁置 .hdr 文件生成失败: {e}") + return False + + # 4) 目标 .hdr 已存在(ENVI 场景)→ 原有合并逻辑(把源波长等字段追加进去) try: - source_hdr_path = get_hdr_file_path(source_img_path) - dest_hdr_path = get_hdr_file_path(dest_img_path) - - if not Path(source_hdr_path).exists(): - print(f"警告: 源hdr文件不存在: {source_hdr_path}") - return False - - if not Path(dest_hdr_path).exists(): - print(f"警告: 目标hdr文件不存在: {dest_hdr_path}") - return False - - write_fields_to_hdrfile(source_hdr_path, dest_hdr_path) + write_fields_to_hdrfile(str(src_hdr), str(dest_hdr_path)) print(f"已复制原始hdr文件信息到: {dest_hdr_path}") return True except Exception as e: - print(f"警告: 复制hdr文件信息时出错: {e}") + print(f"错误: 追加 .hdr 元数据失败: {e}") return False \ No newline at end of file