141 lines
4.7 KiB
Python
141 lines
4.7 KiB
Python
"""
|
|
QGIS 环境检测与子进程配置模块。
|
|
|
|
主进程运行在 Python 3.10,无法直接加载 QGIS 的 Python 3.12 C 扩展。
|
|
所有 QGIS 操作通过 subprocess 调用 QGIS Python 3.12 执行。
|
|
|
|
本模块提供:
|
|
- get_qgis_python_path(): 检测 QGIS Python 3.12 解释器路径
|
|
- build_qgis_command(): 构建通过 .bat 启动 QGIS 子进程的命令
|
|
- is_qgis_available(): 检查 QGIS 是否可用
|
|
- get_runner_script(): 获取 qgis_runner.py 的路径
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from app.config.paths import get_logger
|
|
|
|
logger = get_logger("qgis.env")
|
|
|
|
|
|
def get_qgis_python_path(qgis_root: str = None) -> str | None:
|
|
"""
|
|
检测 QGIS 自带的 Python 3.12 解释器路径。
|
|
|
|
Windows: {QGIS_ROOT}/apps/Python312/python3.exe
|
|
|
|
Returns:
|
|
解释器绝对路径,不存在则返回 None
|
|
"""
|
|
if qgis_root is None:
|
|
qgis_root = _detect_qgis_root()
|
|
if qgis_root is None:
|
|
return None
|
|
|
|
import platform
|
|
if platform.system() == "Windows":
|
|
for name in ("python3.exe", "python.exe"):
|
|
candidate = os.path.join(qgis_root, "apps", "Python312", name)
|
|
if os.path.isfile(candidate):
|
|
logger.info(f"检测到 QGIS Python: {candidate}")
|
|
return candidate
|
|
logger.warning(f"未找到 QGIS Python 3.12")
|
|
return None
|
|
else:
|
|
import shutil
|
|
|
|
# 优先检查环境变量
|
|
env_python = os.environ.get("QGIS_PYTHON_PATH")
|
|
if env_python and os.path.isfile(env_python):
|
|
logger.info(f"QGIS_PYTHON_PATH 指定: {env_python}")
|
|
return env_python
|
|
|
|
# 搜索标准 Linux QGIS 安装路径
|
|
linux_paths = [
|
|
"/usr/bin/qgis", # Ubuntu/Debian apt 安装
|
|
"/usr/bin/qgis.bin",
|
|
"/opt/QGIS/apps/Python312/bin/python3", # 独立安装器
|
|
"/opt/QGIS/apps/Python3/bin/python3",
|
|
"/usr/libexec/qgis/python3", # Fedora/RHEL
|
|
]
|
|
for candidate in linux_paths:
|
|
if os.path.isfile(candidate):
|
|
logger.info(f"检测到 QGIS 可执行文件: {candidate}")
|
|
return candidate
|
|
|
|
# 回退到系统 Python(如果 qgis.core 可导入)
|
|
sys_python = shutil.which("python3") or shutil.which("python")
|
|
if sys_python:
|
|
logger.info(f"Linux 环境,使用系统 Python: {sys_python}")
|
|
return sys_python
|
|
return None
|
|
|
|
|
|
def build_qgis_command(qgis_root: str = None) -> list[str]:
|
|
"""
|
|
构建通过静态启动脚本运行 QGIS 子进程的命令列表。
|
|
|
|
Windows: 调用 app/script/run_qgis.bat(通过 QGIS_ROOT 环境变量定位)
|
|
Linux: 直接调用 QGIS Python 解释器
|
|
"""
|
|
import platform
|
|
if qgis_root is None:
|
|
qgis_root = _detect_qgis_root() or "D:/QGIS"
|
|
|
|
runner_script = get_runner_script()
|
|
if not os.path.isfile(runner_script):
|
|
raise RuntimeError(f"QGIS Runner 脚本不存在: {runner_script}")
|
|
|
|
if platform.system() == "Windows":
|
|
script_dir = Path(__file__).parent.parent.parent / "script"
|
|
bat_path = str(script_dir / "run_qgis.bat")
|
|
if not os.path.isfile(bat_path):
|
|
raise RuntimeError(f"QGIS 启动脚本不存在: {bat_path}")
|
|
# 设置环境变量让 .bat 能找到 QGIS
|
|
os.environ["QGIS_ROOT"] = qgis_root
|
|
return ["cmd.exe", "/c", bat_path, runner_script]
|
|
else:
|
|
python_path = get_qgis_python_path(qgis_root)
|
|
if not python_path:
|
|
raise RuntimeError("未找到 QGIS Python 解释器")
|
|
return [python_path, runner_script]
|
|
|
|
|
|
def is_qgis_available(qgis_root: str = None) -> bool:
|
|
"""检查 QGIS 环境是否可用"""
|
|
return get_qgis_python_path(qgis_root) is not None
|
|
|
|
|
|
def get_runner_script() -> str:
|
|
"""获取 qgis_runner.py 的绝对路径"""
|
|
return str(Path(__file__).parent / "qgis_runner.py")
|
|
|
|
|
|
def _detect_qgis_root() -> str | None:
|
|
"""
|
|
自动检测 QGIS 安装根目录。
|
|
|
|
优先级:
|
|
1. 环境变量 QGIS_ROOT
|
|
2. Windows 默认路径 D:/QGIS
|
|
3. Linux 常见路径
|
|
"""
|
|
env_root = os.environ.get("QGIS_ROOT")
|
|
if env_root and os.path.isdir(env_root):
|
|
return env_root
|
|
|
|
import platform
|
|
if platform.system() == "Windows":
|
|
for candidate in ["D:/QGIS", "C:/OSGeo4W", "C:/QGIS"]:
|
|
if os.path.isdir(candidate):
|
|
logger.info(f"检测到 QGIS 根目录: {candidate}")
|
|
return candidate
|
|
else:
|
|
for candidate in ["/usr", "/opt/QGIS", "/home/QGIS"]:
|
|
if os.path.isdir(candidate):
|
|
logger.info(f"检测到 QGIS 根目录: {candidate}")
|
|
return candidate
|
|
|
|
logger.warning("未检测到 QGIS 安装目录")
|
|
return None
|