refactor(packaging): PyInstaller资源路径统一适配get_resource_path

This commit is contained in:
DXC
2026-05-10 18:02:59 +08:00
parent 5a55be286f
commit 2a4a7ec7be
4 changed files with 74 additions and 47 deletions

View File

@ -5,9 +5,20 @@ Step5_5 面板 - 水质指数计算
"""
import os
import sys
from pathlib import Path
from typing import Dict, List, Union
def get_resource_path(relative_path: str) -> str:
"""获取资源的绝对路径,适配 PyInstaller 打包环境。"""
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.abspath(
os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), relative_path)
)
import pandas as pd
from PyQt5.QtWidgets import (
QWidget, QVBoxLayout, QGroupBox, QFormLayout, QGridLayout,
@ -153,10 +164,8 @@ class Step5_5Panel(QWidget):
self.setLayout(main_layout)
# 自动加载内置公式文件
formula_csv_path = (
Path(__file__).resolve().parent.parent / "model" / "waterindex.csv"
)
if formula_csv_path.is_file():
formula_csv_path = get_resource_path("data/sub/waterindex.csv")
if os.path.isfile(formula_csv_path):
self.formula_csv_widget.set_path(str(formula_csv_path))
self.refresh_formulas()

View File

@ -410,13 +410,27 @@ class Step9Panel(QWidget):
if not existing_out or not existing_out.strip():
self.output_dir.set_path(output_dir)
# 5. 自动继承步骤1的水域掩膜作为边界文件
# 5. 自动探测原始矢量边界文件(.shp作为专题图底图
# 优先回溯 input-test/roi.shpgeopandas.read_file 仅支持矢量格式
if self.work_dir:
default_mask = Path(self.work_dir) / "1_water_mask" / "water_mask_from_shp.dat"
if default_mask.exists():
existing_boundary = (self.boundary_file.get_path() or "").strip()
if not existing_boundary:
self.boundary_file.set_path(str(default_mask))
possible_shp = None
candidates = [
Path(self.work_dir).parent / "input-test" / "roi.shp",
Path(self.work_dir) / "roi.shp",
Path(self.work_dir).parent / "roi.shp",
]
for candidate in candidates:
if candidate.exists() and candidate.suffix.lower() == ".shp":
possible_shp = candidate
break
existing_boundary = (self.boundary_file.get_path() or "").strip()
if not existing_boundary and possible_shp:
self.boundary_file.set_path(str(possible_shp))
elif not existing_boundary:
# 未找到 .shp 时清空并提示用户手动选择矢量文件
self.boundary_file.set_path("")
print("⚠️ 提示:专题图生成模块需传入标准矢量边界文件 (.shp),请手动选择。")
except Exception as e:
import traceback
print(f"{self.__class__.__name__}】自动填充失败,跳过: {e}")