重置QGIS实现

This commit is contained in:
wzy-warehouse
2026-06-21 12:47:43 +08:00
parent 480e793ff8
commit fe3ccd005d
4 changed files with 160 additions and 192 deletions
+29 -33
View File
@@ -23,12 +23,23 @@ import sys
import time
# ============================================================
# 1. 环境初始化(必须在任何 QGIS/Qt import 之前)
# 环境初始化(必须在任何 QGIS/Qt import 之前)
# ============================================================
QGIS_ROOT = os.environ.get("QGIS_ROOT", "D:/QGIS")
def _detect_qgis_app_dir():
"""自动检测 QGIS 应用目录(qgis-ltr 或 qgis"""
for name in ("qgis-ltr", "qgis"):
d = os.path.join(QGIS_ROOT, "apps", name)
if os.path.isdir(d):
return d
raise RuntimeError(
f"未找到 QGIS 应用目录: {QGIS_ROOT}\\apps\\qgis-ltr 或 qgis"
)
def _setup_python_path():
"""将项目根目录和 QGIS Python 路径加入 sys.path"""
project_root = os.path.dirname(
@@ -37,59 +48,44 @@ def _setup_python_path():
if project_root not in sys.path:
sys.path.insert(0, project_root)
qgis_python = os.path.join(QGIS_ROOT, "apps", "qgis-ltr", "python")
qgis_app_dir = _detect_qgis_app_dir()
qgis_python = os.path.join(qgis_app_dir, "python")
if os.path.isdir(qgis_python) and qgis_python not in sys.path:
sys.path.insert(0, qgis_python)
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 搜索路径。
# ★ 必须在任何 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
os.environ["QGIS_PREFIX_PATH"] = qgis_app_dir
QGIS_PREFIX_PATH / QT_PLUGIN_PATH / GDAL_DATA / PROJ_DATA
已由主进程通过 build_qgis_subprocess_env() 设置,子进程不重复设。
这里注册 DLL 搜索目录并预加载核心 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 = [
qgis_app_dir = _detect_qgis_app_dir()
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"),
]
for dll_dir in dll_dirs:
if os.path.isdir(dll_dir):
os.add_dll_directory(dll_dir)
# 预加载核心 DLL —— 强制从 QGIS 目录加载,防止系统 PATH 中同名 DLL 干扰
_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_dir in dll_dirs:
for dll_name in _preload_dlls:
dll_path = os.path.join(dll_dir, dll_name)
if os.path.isfile(dll_path):
@@ -100,14 +96,14 @@ def _setup_environment():
# ============================================================
# 2. 主逻辑
# 主逻辑
# ============================================================
def _init_qgis():
"""初始化 QgsApplication(只做一次)"""
from qgis.core import QgsApplication
qgis_app_dir = os.path.join(QGIS_ROOT, "apps", "qgis-ltr")
qgis_app_dir = _detect_qgis_app_dir()
QgsApplication.setPrefixPath(qgis_app_dir, True)
qgs_app = QgsApplication([], False)
qgs_app.initQgis()