修复Step7采样点布设路径读取问题:GDAL环境变量保护+路径归一化+FileNotFoundError检查+水域掩膜备选路径扫描

This commit is contained in:
DXC
2026-05-08 18:05:11 +08:00
parent 3c0bd29275
commit d7b5c45dd4
3 changed files with 45 additions and 3 deletions

View File

@ -229,6 +229,11 @@ class WorkerThread(QThread):
def run(self): def run(self):
"""运行 pipeline子线程内切换 Matplotlib 为 Agg避免 Qt5Agg 在后台线程绘图导致界面卡死。""" """运行 pipeline子线程内切换 Matplotlib 为 Agg避免 Qt5Agg 在后台线程绘图导致界面卡死。"""
import os
# GDAL 环境变量保护(放在最前面,防止路径/编码问题)
os.environ['GDAL_FILENAME_IS_UTF8'] = 'YES'
os.environ['SHAPE_ENCODING'] = 'UTF-8'
mpl_prev = None mpl_prev = None
try: try:
import matplotlib import matplotlib

View File

@ -150,7 +150,7 @@ class Step7Panel(QWidget):
deglint_path = os.path.join(self.work_dir or '', deglint_path).replace('\\', '/') deglint_path = os.path.join(self.work_dir or '', deglint_path).replace('\\', '/')
self.deglint_img_file.set_path(deglint_path) self.deglint_img_file.set_path(deglint_path)
# 2. 填充水域掩膜路径(优先pipeline.step_outputs 获取绝对路径 # 2. 填充水域掩膜路径(优先级:pipeline.step_outputs > step1_panel > 1_water_mask > input-test
water_mask_path = None water_mask_path = None
if pipeline and hasattr(pipeline, 'step_outputs'): if pipeline and hasattr(pipeline, 'step_outputs'):
step1_outputs = getattr(pipeline, 'step_outputs', {}).get('step1', {}) step1_outputs = getattr(pipeline, 'step_outputs', {}).get('step1', {})
@ -162,6 +162,26 @@ class Step7Panel(QWidget):
# 回退:从 step1 面板 widget 直接读取 # 回退:从 step1 面板 widget 直接读取
if not water_mask_path and hasattr(main_window, 'step1_panel'): if not water_mask_path and hasattr(main_window, 'step1_panel'):
water_mask_path = main_window.step1_panel.output_file.get_path() water_mask_path = main_window.step1_panel.output_file.get_path()
# 备选:扫描 1_water_mask 目录下的 .dat 文件
if not water_mask_path and self.work_dir:
mask_dir = os.path.join(self.work_dir, "1_water_mask")
if os.path.isdir(mask_dir):
dat_files = [f for f in os.listdir(mask_dir) if f.lower().endswith('.dat')]
if dat_files:
water_mask_path = os.path.join(mask_dir, dat_files[0]).replace('\\', '/')
# 备选:扫描 input-test 目录(优先匹配 water_mask_from_shp.dat
if not water_mask_path and self.work_dir:
input_test_dir = os.path.join(self.work_dir, "input-test")
if os.path.isdir(input_test_dir):
dat_files = [f for f in os.listdir(input_test_dir) if f.lower().endswith('.dat')]
# 优先匹配 water_mask_from_shp.dat
for f in dat_files:
if 'water_mask_from_shp' in f.lower():
water_mask_path = os.path.join(input_test_dir, f).replace('\\', '/')
break
# 否则取第一个 .dat 文件
if not water_mask_path and dat_files:
water_mask_path = os.path.join(input_test_dir, dat_files[0]).replace('\\', '/')
if water_mask_path: if water_mask_path:
# 若为相对路径,使用 work_dir 合成为绝对路径 # 若为相对路径,使用 work_dir 合成为绝对路径

View File

@ -1,10 +1,21 @@
from src.utils.util import * # -*- coding: utf-8 -*-
import math """
采样点生成模块 - 提供分块采样和光谱数据提取功能
"""
import os import os
import math
# GDAL 环境变量保护(放在最前面,防止路径/编码问题)
os.environ['GDAL_FILENAME_IS_UTF8'] = 'YES'
os.environ['SHAPE_ENCODING'] = 'UTF-8'
import numpy as np import numpy as np
from osgeo import gdal, ogr from osgeo import gdal, ogr
import spectral import spectral
from scipy import ndimage from scipy import ndimage
from src.utils.util import write_bands
try: try:
from skimage import morphology from skimage import morphology
from skimage.morphology import skeletonize, medial_axis from skimage.morphology import skeletonize, medial_axis
@ -87,6 +98,12 @@ def get_spectral_sampling_points_chunked(bil_file, water_mask_shp, severe_glint=
ogr.UseExceptions() ogr.UseExceptions()
try: try:
# ---------- 路径归一化 + 存在性检查 ----------
bil_file = os.path.abspath(bil_file).replace('\\', '/')
print(f"[路径检查] 去耀斑影像: {bil_file}")
if not os.path.exists(bil_file):
raise FileNotFoundError(f"【后端错误】无法在磁盘上找到指定的去耀斑影像: {bil_file}")
# 打开bil文件 # 打开bil文件
dataset_bil = gdal.Open(bil_file) dataset_bil = gdal.Open(bil_file)
if dataset_bil is None: if dataset_bil is None: