专题图修改
This commit is contained in:
@@ -74,16 +74,9 @@ def get_qgis_python_path(qgis_root: str = None) -> str | None:
|
||||
|
||||
def build_qgis_command(qgis_root: str = None) -> list[str]:
|
||||
"""
|
||||
构建通过 .bat 包装器启动 QGIS 子进程的命令列表。
|
||||
设置环境变量并启动 QGIS Python 3.12。
|
||||
构建 QGIS 子进程启动命令(直接调用 Python,不经过 bat 包装器)。
|
||||
环境变量通过 build_qgis_env() 构建后传给 subprocess.run(env=...)。
|
||||
"""
|
||||
import platform
|
||||
if platform.system() != "Windows":
|
||||
python_path = get_qgis_python_path(qgis_root)
|
||||
if not python_path:
|
||||
raise RuntimeError("未找到 QGIS Python 解释器")
|
||||
return [python_path, get_runner_script()]
|
||||
|
||||
if qgis_root is None:
|
||||
qgis_root = _detect_qgis_root() or "D:/QGIS"
|
||||
|
||||
@@ -95,8 +88,76 @@ def build_qgis_command(qgis_root: str = None) -> list[str]:
|
||||
if not os.path.isfile(runner_script):
|
||||
raise RuntimeError(f"QGIS Runner 脚本不存在: {runner_script}")
|
||||
|
||||
bat_path = _generate_bat_wrapper(qgis_root, python_path, runner_script)
|
||||
return ["cmd.exe", "/c", bat_path]
|
||||
return [python_path, runner_script]
|
||||
|
||||
|
||||
def build_qgis_env(qgis_root: str = None) -> dict:
|
||||
"""
|
||||
构建 QGIS 子进程所需的完整环境变量字典。
|
||||
|
||||
基于当前进程环境继承,设置 QGIS 所需的 PYTHONPATH、PATH、
|
||||
GDAL_DATA、PROJ_DATA 等变量。调用方直接传给 subprocess.run(env=...)。
|
||||
"""
|
||||
import platform
|
||||
|
||||
if qgis_root is None:
|
||||
qgis_root = _detect_qgis_root() or "D:/QGIS"
|
||||
|
||||
env = dict(os.environ)
|
||||
|
||||
if platform.system() != "Windows":
|
||||
return env
|
||||
|
||||
# --- 检测 QGIS 应用目录 ---
|
||||
qgis_app_dir = os.path.join(qgis_root, "apps", "qgis-ltr")
|
||||
if not os.path.isdir(qgis_app_dir):
|
||||
qgis_app_dir = os.path.join(qgis_root, "apps", "qgis")
|
||||
if not os.path.isdir(qgis_app_dir):
|
||||
raise RuntimeError(f"未找到 QGIS 应用目录: {qgis_root}\\apps\\qgis-ltr 或 qgis")
|
||||
|
||||
# --- 检测 Python 目录 ---
|
||||
python_dir = os.path.join(qgis_root, "apps", "Python312")
|
||||
if not os.path.isdir(python_dir):
|
||||
for name in ("Python39", "Python310", "Python311"):
|
||||
candidate = os.path.join(qgis_root, "apps", name)
|
||||
if os.path.isdir(candidate):
|
||||
python_dir = candidate
|
||||
break
|
||||
|
||||
# --- 核心路径 ---
|
||||
qtplugins = os.path.join(qgis_app_dir, "qtplugins")
|
||||
qt5_plugins = os.path.join(qgis_root, "apps", "Qt5", "plugins")
|
||||
gdal_data = os.path.join(qgis_root, "apps", "gdal", "share", "gdal")
|
||||
qgis_python = os.path.join(qgis_app_dir, "python")
|
||||
qgis_bin = os.path.join(qgis_app_dir, "bin")
|
||||
qt5_bin = os.path.join(qgis_root, "apps", "Qt5", "bin")
|
||||
gdal_lib = os.path.join(qgis_root, "apps", "gdal", "lib")
|
||||
|
||||
env["PYTHONPATH"] = qgis_python
|
||||
env["QGIS_PREFIX_PATH"] = qgis_app_dir
|
||||
env["QT_PLUGIN_PATH"] = f"{qtplugins};{qt5_plugins}"
|
||||
env["GDAL_DATA"] = gdal_data
|
||||
env["PYTHONUTF8"] = "1"
|
||||
env["GDAL_FILENAME_IS_UTF8"] = "YES"
|
||||
env["VSI_CACHE"] = "TRUE"
|
||||
env["VSI_CACHE_SIZE"] = "1000000"
|
||||
|
||||
# --- PROJ 数据目录 ---
|
||||
for pd in [
|
||||
os.path.join(qgis_app_dir, "share", "proj"),
|
||||
os.path.join(qgis_root, "share", "proj"),
|
||||
os.path.join(qgis_root, "apps", "gdal", "share", "proj"),
|
||||
]:
|
||||
if os.path.isfile(os.path.join(pd, "proj.db")):
|
||||
env["PROJ_DATA"] = pd
|
||||
break
|
||||
|
||||
# --- PATH:前置 QGIS 二进制目录 ---
|
||||
prepend = f"{qgis_bin};{qt5_bin};{gdal_lib}"
|
||||
env["PATH"] = f"{prepend};{env.get('PATH', '')}"
|
||||
|
||||
logger.debug(f"QGIS env built: QGIS_ROOT={qgis_root}, QGIS_APP={qgis_app_dir}")
|
||||
return env
|
||||
|
||||
|
||||
def is_qgis_available(qgis_root: str = None) -> bool:
|
||||
|
||||
@@ -43,61 +43,21 @@ def _setup_python_path():
|
||||
|
||||
|
||||
def _setup_environment():
|
||||
"""设置 QGIS 运行所需的环境变量"""
|
||||
# 自动检测 QGIS 应用目录(与 run_qgis.bat 保持一致)
|
||||
qgis_app_dir = os.path.join(QGIS_ROOT, "apps", "qgis-ltr")
|
||||
if not os.path.isdir(qgis_app_dir):
|
||||
qgis_app_dir = os.path.join(QGIS_ROOT, "apps", "qgis")
|
||||
"""设置 QGIS 运行所需的环境变量。
|
||||
|
||||
# ★ 必须在任何 DLL 加载前设置 PROJ_DATA,防止 PostgreSQL 的旧 proj.db 干扰
|
||||
for proj_dir in [
|
||||
os.path.join(qgis_app_dir, "share", "proj"),
|
||||
os.path.join(QGIS_ROOT, "share", "proj"),
|
||||
os.path.join(QGIS_ROOT, "apps", "gdal", "share", "proj"),
|
||||
]:
|
||||
if os.path.isdir(proj_dir):
|
||||
os.environ["PROJ_DATA"] = proj_dir
|
||||
break
|
||||
注意:QGIS Python 3.12 自带完整的 DLL 配置,不应设置
|
||||
QGIS_PREFIX_PATH、QT_PLUGIN_PATH、PATH 等变量,
|
||||
否则会与 QGIS 内置的 DLL 加载机制冲突,
|
||||
导致 0xC0000005 (ACCESS_VIOLATION) 崩溃。
|
||||
|
||||
os.environ["QGIS_PREFIX_PATH"] = qgis_app_dir
|
||||
仅设置不影响 DLL 加载的辅助变量。
|
||||
"""
|
||||
# 仅设置 UTF-8 和 GDAL 相关的编码变量(不影响 DLL 加载)
|
||||
os.environ["PYTHONUTF8"] = "1"
|
||||
os.environ["GDAL_FILENAME_IS_UTF8"] = "YES"
|
||||
os.environ["VSI_CACHE"] = "TRUE"
|
||||
os.environ["VSI_CACHE_SIZE"] = "1000000"
|
||||
|
||||
if sys.platform == "win32":
|
||||
os.environ["QT_PLUGIN_PATH"] = (
|
||||
f"{os.path.join(qgis_app_dir, 'qtplugins')};"
|
||||
f"{os.path.join(QGIS_ROOT, 'apps', 'Qt5', 'plugins')}"
|
||||
)
|
||||
gdal_data = os.path.join(QGIS_ROOT, "apps", "gdal", "share", "gdal")
|
||||
if os.path.isdir(gdal_data):
|
||||
os.environ["GDAL_DATA"] = gdal_data
|
||||
|
||||
import ctypes
|
||||
_dll_dirs = [
|
||||
os.path.join(qgis_app_dir, "bin"),
|
||||
os.path.join(QGIS_ROOT, "apps", "Qt5", "bin"),
|
||||
os.path.join(QGIS_ROOT, "apps", "gdal", "lib"),
|
||||
]
|
||||
_preload_dlls = [
|
||||
"qgis_core.dll", "qgispython.dll",
|
||||
"Qt5Core.dll", "Qt5Gui.dll", "Qt5Widgets.dll",
|
||||
"Qt5Network.dll", "Qt5Svg.dll", "Qt5Xml.dll",
|
||||
"Qt5Concurrent.dll", "Qt5PrintSupport.dll",
|
||||
]
|
||||
for dll_dir in _dll_dirs:
|
||||
if not os.path.isdir(dll_dir):
|
||||
continue
|
||||
os.add_dll_directory(dll_dir)
|
||||
for dll_name in _preload_dlls:
|
||||
dll_path = os.path.join(dll_dir, dll_name)
|
||||
if os.path.isfile(dll_path):
|
||||
try:
|
||||
ctypes.WinDLL(dll_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 2. 主逻辑
|
||||
@@ -189,8 +149,22 @@ def main():
|
||||
print(f"[qgis_runner] 致命错误 ({elapsed:.1f}s): {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
finally:
|
||||
qgs_app.exitQgis()
|
||||
# 跳过 qgs_app.exitQgis() — 已知 Qt DLL 在 exitQgis() 时会触发
|
||||
# STATUS_ACCESS_VIOLATION (0xC0000005) 崩溃,进程通过 os._exit 终止,
|
||||
# 操作系统会回收所有资源。
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
pass
|
||||
# 强制终止进程,避免 Qt/QGIS 后台线程在 ExitProcess 等待期间 DLL 崩溃
|
||||
try:
|
||||
import ctypes
|
||||
ctypes.windll.kernel32.TerminateProcess(
|
||||
ctypes.windll.kernel32.GetCurrentProcess(), 0
|
||||
)
|
||||
except Exception:
|
||||
os._exit(0)
|
||||
|
||||
Reference in New Issue
Block a user