专题图修改
This commit is contained in:
@@ -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