QGIS的docker管理
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
QGIS 专题图生成子进程入口。
|
||||
QGIS 专题图生成子进程入口 — Docker 容器内运行。
|
||||
|
||||
由主进程 (Python 3.10) 通过 subprocess 调用,
|
||||
运行在 QGIS 自带的 Python 3.12 环境中。
|
||||
由主进程通过 docker exec 调用,运行在 QGIS Docker 容器内。
|
||||
|
||||
支持两种模式:
|
||||
- 批量模式(推荐):单次启动 QgsApplication,顺序处理多个模板
|
||||
@@ -23,102 +22,70 @@ import sys
|
||||
import time
|
||||
|
||||
# ============================================================
|
||||
# 环境初始化(必须在任何 QGIS/Qt import 之前)
|
||||
# 环境初始化(Docker 容器内,QGIS 通过 apt 安装在 /usr)
|
||||
# ============================================================
|
||||
|
||||
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"""
|
||||
"""将项目根目录和 QGIS dist-packages 加入 sys.path"""
|
||||
project_root = os.path.dirname(
|
||||
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
)
|
||||
if project_root not in sys.path:
|
||||
sys.path.insert(0, project_root)
|
||||
|
||||
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)
|
||||
# QGIS Python 包路径(从配置读取,避免硬编码)
|
||||
try:
|
||||
from config import settings
|
||||
pythonpath = getattr(settings, "QGIS_DOCKER_PYTHONPATH", None) or []
|
||||
except Exception:
|
||||
pythonpath = []
|
||||
|
||||
for p in pythonpath:
|
||||
if os.path.isdir(p) and p not in sys.path:
|
||||
sys.path.insert(0, p)
|
||||
|
||||
|
||||
def _setup_environment():
|
||||
"""设置 QGIS 运行所需的环境变量和 DLL 搜索路径。
|
||||
|
||||
QGIS_PREFIX_PATH / QT_PLUGIN_PATH / GDAL_DATA / PROJ_DATA
|
||||
已由主进程通过 build_qgis_subprocess_env() 设置,子进程不重复设。
|
||||
这里注册 DLL 搜索目录并预加载核心 DLL 以确保正确加载顺序。
|
||||
"""
|
||||
"""设置 QGIS 运行所需的环境变量"""
|
||||
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":
|
||||
import ctypes
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 主逻辑
|
||||
# ============================================================
|
||||
|
||||
def _init_qgis():
|
||||
"""初始化 QgsApplication(只做一次)"""
|
||||
"""初始化 QgsApplication(从配置读取 prefixPath,避免硬编码)"""
|
||||
from qgis.core import QgsApplication
|
||||
|
||||
qgis_app_dir = _detect_qgis_app_dir()
|
||||
QgsApplication.setPrefixPath(qgis_app_dir, True)
|
||||
try:
|
||||
from config import settings
|
||||
prefix_path = getattr(settings, "QGIS_DOCKER_PREFIX_PATH", "/usr")
|
||||
except Exception:
|
||||
prefix_path = "/usr"
|
||||
|
||||
QgsApplication.setPrefixPath(prefix_path, True)
|
||||
qgs_app = QgsApplication([], False)
|
||||
qgs_app.initQgis()
|
||||
return qgs_app
|
||||
|
||||
|
||||
def _process_single(service, model):
|
||||
"""处理单个模板,返回结果 dict"""
|
||||
"""处理单个模板,返回结果 dict。成功/失败均输出 PROGRESS 行。"""
|
||||
name = service.generate(model)
|
||||
result = {"name": name, "output": model["outFile"]}
|
||||
# ★ 实时进度:每完成一张图就输出到 stdout
|
||||
print(f"PROGRESS:{json.dumps(result, ensure_ascii=False)}", flush=True)
|
||||
return result
|
||||
|
||||
|
||||
def _emit_progress(result: dict):
|
||||
"""输出 PROGRESS 行(成功或失败均调用)"""
|
||||
print(f"PROGRESS:{json.dumps(result, ensure_ascii=False)}", flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
t_start = time.time()
|
||||
|
||||
@@ -166,7 +133,9 @@ def main():
|
||||
f"耗时 {elapsed:.1f}s — {error_msg}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
results.append({"name": model.get("name", ""), "output": "", "error": error_msg})
|
||||
fail_result = {"name": model.get("name", ""), "output": "", "error": error_msg}
|
||||
_emit_progress(fail_result)
|
||||
results.append(fail_result)
|
||||
|
||||
# 输出结果
|
||||
if len(models) == 1 and not request.get("models"):
|
||||
|
||||
Reference in New Issue
Block a user