fix(step5/step5.5): 掩膜.shp智能替身为.dat、band_math.eval注入np.nan/np.inf命名空间

This commit is contained in:
DXC
2026-05-10 16:20:51 +08:00
parent abac272b31
commit 6d4d802ffe
2 changed files with 21 additions and 2 deletions

View File

@ -96,6 +96,19 @@ class DataPreparationStep:
if final_boundary_path is None and water_mask_path is not None: if final_boundary_path is None and water_mask_path is not None:
final_boundary_path = water_mask_path 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 flare_path = glint_mask_path
if flare_path: if flare_path:
print(f"光谱提取使用耀斑掩膜: {flare_path}") print(f"光谱提取使用耀斑掩膜: {flare_path}")

View File

@ -96,8 +96,14 @@ class BandMathCalculator:
print(f"计算表达式: {calc_expression}") print(f"计算表达式: {calc_expression}")
# 安全地计算表达式 # 【新增安全防护】引入 numpy 命名空间,让 eval 引擎安全识别 nan 与 inf
result = eval(calc_expression) 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: if var_name: