From 6d4d802ffea68281e7206d6ad24393ed86380918 Mon Sep 17 00:00:00 2001 From: DXC Date: Sun, 10 May 2026 16:20:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(step5/step5.5):=20=E6=8E=A9=E8=86=9C.shp?= =?UTF-8?q?=E6=99=BA=E8=83=BD=E6=9B=BF=E8=BA=AB=E4=B8=BA.dat=E3=80=81band?= =?UTF-8?q?=5Fmath.eval=E6=B3=A8=E5=85=A5np.nan/np.inf=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/steps/data_preparation_step.py | 13 +++++++++++++ src/utils/band_math.py | 10 ++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) 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: