修复多步运行时参数传递及文件读取问题

This commit is contained in:
DXC
2026-05-08 09:27:07 +08:00
parent 742bc392a5
commit 0f36da742f
3 changed files with 111 additions and 9 deletions

View File

@ -81,8 +81,14 @@ except ImportError:
print("警告: scipy未安装0值像素插值功能可能无法正常工作")
# 导入GDAL用于影像读写
try:
from osgeo import gdal
from osgeo import gdal, ogr
GDAL_AVAILABLE = True
# 注册所有 GDAL/OGR 驱动,确保 ESRI Shapefile 驱动可用
gdal.AllRegister()
ogr.RegisterAll()
# 启用 GDAL/OGR 异常,使错误以 Python 异常形式抛出(而不是静默失败)
gdal.UseExceptions()
ogr.UseExceptions()
except ImportError:
GDAL_AVAILABLE = False
print("警告: GDAL未安装新算法可能无法正常工作")
@ -369,7 +375,8 @@ class WaterQualityInversionPipeline:
# 执行栅格化
from src.utils.extract_water_area import rasterize_shp
rasterize_shp(mask_path, shp_output_path, img_path)
safe_mask_path = os.path.abspath(mask_path).replace('\\', '/')
rasterize_shp(safe_mask_path, shp_output_path, img_path)
self.water_mask_path = shp_output_path
step_end_time = time.time()
self._record_step_time("步骤1: 生成水域mask", step_start_time, step_end_time)
@ -1134,10 +1141,54 @@ class WaterQualityInversionPipeline:
# 从shp文件创建掩膜这种情况应该很少因为步骤1已经统一转换为dat
try:
from src.utils.extract_water_area import rasterize_shp
# 路径标准化:转为绝对路径,统一正斜杠
safe_shp_path = os.path.abspath(water_mask).replace('\\', '/')
print(f"[DEBUG] 标准化后的 SHP 路径: {safe_shp_path}")
# 检查必要的伴随文件是否存在
shp_dir = os.path.dirname(safe_shp_path)
shp_base = os.path.splitext(safe_shp_path)[0]
for ext in ['.dbf', '.shx', '.prj']:
companion = shp_base + ext
if not os.path.exists(companion):
print(f"[DEBUG] 缺失伴随文件: {companion}")
# 检查 ESRI Shapefile 驱动是否可用
if GDAL_AVAILABLE:
driver = ogr.GetDriverByName("ESRI Shapefile")
if driver is None:
raise RuntimeError("系统中未找到 ESRI Shapefile 驱动!请检查 GDAL 安装。")
print(f"[DEBUG] ESRI Shapefile 驱动可用: {driver.GetName()}")
# 尝试用 ogr.Open 打开,捕获详细错误
try:
ogr_ds = ogr.Open(safe_shp_path)
if ogr_ds is None:
# 通过 gdal.OpenEx 再试一次,获取详细原因
ogr_ds2 = gdal.OpenEx(safe_shp_path, gdal.OF_VECTOR)
if ogr_ds2 is None:
raise RuntimeError(
f"ogr.Open 和 gdal.OpenEx 均无法打开 SHP 文件。\n"
f"可能原因:\n"
f" 1. 文件路径包含中文/特殊字符(当前路径: {safe_shp_path}\n"
f" 2. .dbf/.shx 等伴随文件缺失或损坏\n"
f" 3. GDAL 驱动未注册\n"
f"建议:将 SHP 文件复制到纯英文路径下重试"
)
else:
print(f"[DEBUG] ogr.Open 成功打开 SHP图层数: {ogr_ds.GetLayerCount()}")
ogr_ds = None # 仅用于诊断,不做后续处理
except Exception as ogr_err:
raise RuntimeError(
f"OGR 打开 SHP 时出错(详细原因): {str(ogr_err)}\n"
f"文件路径: {safe_shp_path}"
)
# 使用固定路径,避免重复转换
shp_name = Path(water_mask).stem
shp_name = Path(safe_shp_path).stem
temp_mask_path = str(self.water_mask_dir / f"water_mask_{shp_name}.dat")
# 如果文件已存在,直接使用
if Path(temp_mask_path).exists():
print(f"使用已存在的栅格化掩膜: {temp_mask_path}")
@ -1146,10 +1197,11 @@ class WaterQualityInversionPipeline:
# 需要栅格化需要img_path
if img_path is None:
raise ValueError("当water_mask为shp格式时需要提供img_path参数用于栅格化")
rasterize_shp(water_mask, temp_mask_path, img_path)
# 传入标准化后的路径
rasterize_shp(safe_shp_path, temp_mask_path, img_path)
water_mask = temp_mask_path
print(f"已将shp格式的水域掩膜栅格化为: {temp_mask_path}")
# 读取栅格化的掩膜
if not GDAL_AVAILABLE:
raise ImportError("GDAL未安装无法读取掩膜文件")