diff --git a/src/core/steps/data_preparation_step.py b/src/core/steps/data_preparation_step.py index 4792414..e0b6ef7 100644 --- a/src/core/steps/data_preparation_step.py +++ b/src/core/steps/data_preparation_step.py @@ -96,6 +96,19 @@ class DataPreparationStep: if final_boundary_path is None and water_mask_path is not None: final_boundary_path = water_mask_path + # 【新增安全防护】智能拦截矢量 .shp,强制替换为步骤 1 生成的栅格 .dat + if final_boundary_path and str(final_boundary_path).lower().endswith('.shp'): + # 向上追溯查找 1_water_mask 目录下的 dat 替身 + possible_dat = Path(deglint_img_path).parent.parent / "1_water_mask" / "water_mask_from_shp.dat" + if not possible_dat.exists() and output_path: + possible_dat = Path(output_path).parent.parent / "1_water_mask" / "water_mask_from_shp.dat" + + if possible_dat.exists(): + print(f"💡 智能拦截:检测到输入掩膜为矢量 .shp,自动切换为已生成的栅格掩膜: {possible_dat}") + final_boundary_path = str(possible_dat) + else: + print(f"⚠️ 警告:检测到输入掩膜为 .shp 且未找到对应 .dat 替身,可能导致底层读取失败。") + flare_path = glint_mask_path if flare_path: print(f"光谱提取使用耀斑掩膜: {flare_path}") diff --git a/src/utils/band_math.py b/src/utils/band_math.py index b5678a8..8e20a9d 100644 --- a/src/utils/band_math.py +++ b/src/utils/band_math.py @@ -96,8 +96,14 @@ class BandMathCalculator: print(f"计算表达式: {calc_expression}") - # 安全地计算表达式 - result = eval(calc_expression) + # 【新增安全防护】引入 numpy 命名空间,让 eval 引擎安全识别 nan 与 inf + import numpy as np + try: + # 即使 calc_expression 含有纯字符 nan,也能被 np.nan 安全接管 + result = eval(calc_expression, {"__builtins__": None}, {"nan": np.nan, "inf": np.inf, "np": np}) + except Exception as e: + print(f"⚠️ 警告:公式计算异常 ({e}),该点赋值为 nan") + result = np.nan # 返回结果 if var_name: