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