QGIS完成初步重构

This commit is contained in:
wzy-warehouse
2026-06-20 15:50:24 +08:00
parent d20b5744bb
commit 18d8bcb1a3
45 changed files with 1688 additions and 454 deletions
+166 -86
View File
@@ -17,6 +17,7 @@ from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from app.config.paths import get_logger
from app.config.qgis_mappings import build_static_layers_config, get_gpkg_dir
from app.repositories import qgis_repository
from app.schemas.api_schemas import QgisMapExportResponse, QgisMapExportRequest
from app.utils.api_deps import get_prediction_semaphore
@@ -100,44 +101,77 @@ def _build_map_title(event_type: str, condition: dict, template_name: str) -> st
return f"{prefix}{template_name}"
def _build_info_text(event_type: str, condition: dict, occurred_time) -> str:
"""
构建信息面板文本(左上角橙色矩形区域)。
暴雨:时间 + 累计降雨量(如有)+ 已持续(如有)
地震:时间 + 震级(如有)+ 位置(如有)
不显示灾害类型标签(暴雨/地震)。
"""
lines = []
# 时间
if isinstance(occurred_time, datetime):
time_str = f"{occurred_time.year}{occurred_time.month:02d}{occurred_time.day:02d}{occurred_time.hour:02d}{occurred_time.minute:02d}"
elif occurred_time:
time_str = str(occurred_time)
else:
time_str = datetime.now().strftime("%Y年%m月%d%H时%M分")
lines.append(f"时间:{time_str}")
if event_type == "rainfall":
rainfall = condition.get("rainfall")
if rainfall is not None and rainfall != "":
lines.append(f"累计降雨量:{float(rainfall)}mm")
duration = condition.get("duration")
if duration is not None and duration != "":
lines.append(f"已持续:{duration}")
elif event_type == "earthquake":
magnitude = condition.get("magnitude")
if magnitude is not None and magnitude != "":
lines.append(f"震级:{float(magnitude)}")
lon = condition.get("epicenter_lon")
lat = condition.get("epicenter_lat")
if lon is not None and lat is not None:
lines.append(f"位置:经度{float(lon)}°, 纬度{float(lat)}°")
return "\n".join(lines)
def _derive_model_params(
inference: dict, batch_folder: str, out_filename: str
inference: dict, batch_folder: str, template_path: str
) -> dict:
"""从推理结果 + 配置文件推导专题图生成所需的全部参数。"""
"""从推理结果 + 模板路径推导专题图生成所需的全部参数。"""
event_type = inference["event_type"]
condition = inference["condition"]
occurred_time = inference["occurred_time"]
map_unit = getattr(settings, "QGIS_DEFAULTS_MAP_UNIT", "西安市应急管理局")
map_layout = getattr(settings, "QGIS_DEFAULTS_MAP_LAYOUT", "A3")
zoom_rule = getattr(settings, "QGIS_DEFAULTS_ZOOM_RULE", "11")
zoom_value = getattr(settings, "QGIS_DEFAULTS_ZOOM_VALUE", "50")
map_unit = getattr(settings, "QGIS_DEFAULTS_MAP_UNIT", "西安市应急管理局")
template_base = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
"app", "data", "template"
)
template_path = os.path.join(template_base, event_type, f"{map_layout}.qgz")
template_name_map = {"earthquake": "地震专题图", "rainfall": "降雨专题图"}
template_name = template_name_map.get(event_type, f"{event_type}专题图")
# 从模板文件名推导标题和输出文件名(去掉 .qgz 后缀)
template_name = os.path.splitext(os.path.basename(template_path))[0]
map_title = _build_map_title(event_type, condition, template_name)
safe_name = re.sub(r'[\\/:*?"<>|]', '_', template_name)
out_file = os.path.join(batch_folder, f"{safe_name}.jpg").replace("\\", "/")
if occurred_time and isinstance(occurred_time, datetime):
map_time = occurred_time.strftime("%Y-%m-%d %H:%M")
elif occurred_time:
map_time = str(occurred_time)
else:
map_time = datetime.now().strftime("%Y-%m-%d %H:%M")
# 制图时间统一用当前日期
map_time = datetime.now().strftime("%Y-%m-%d")
center_x, center_y = _extract_center_from_condition(
event_type, condition
)
info_text = _build_info_text(event_type, condition, occurred_time)
return {
"name": f"inference_{inference['id']}",
"path": template_path,
@@ -146,7 +180,7 @@ def _derive_model_params(
"mapTitle": map_title,
"mapTime": map_time,
"mapUint": map_unit,
"info": "",
"info": info_text,
"centerX": center_x,
"centerY": center_y,
"event": str(inference["id"]),
@@ -172,11 +206,7 @@ def _extract_center_from_condition(event_type: str, condition: dict) -> tuple:
def _build_qgis_config(batch_folder: str) -> dict:
"""构建 QGIS 服务配置(含批次输出目录)"""
gpkg_subdir = getattr(settings, "QGIS_GPKG_DIR", "app/data/gpkg")
project_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
gpkg_dir = os.path.join(project_root, gpkg_subdir).replace("\\", "/")
gpkg_dir = get_gpkg_dir()
return {
"db": {
@@ -202,41 +232,15 @@ def _build_qgis_config(batch_folder: str) -> dict:
"port": getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_PORT", ""),
"dbname": getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_DB_NAME", "xian_new"),
"schema": getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_SCHEMA", "qgis"),
"username": getattr(settings, "DB_USER", "postgres"),
"password": getattr(settings, "DB_PASSWORD", ""),
},
},
"static_layers": _build_static_layers_config(gpkg_dir),
"static_layers": build_static_layers_config(gpkg_dir),
"batch_folder": batch_folder,
}
def _build_static_layers_config(gpkg_dir: str) -> dict:
"""构建静态底图配置"""
layer_defs = {
"水库": ("qgis.rivers", "rivers.gpkg"),
"市州驻地": ("qgis.sx_capital", "sx_capital.gpkg"),
"河流": ("qgis.river", "river.gpkg"),
"active_fault": ("qgis.active_fault", "active_fault.gpkg"),
"陕西省": ("qgis.sx", "sx.gpkg"),
"乡镇驻地": ("qgis.sx_street", "sx_street.gpkg"),
"区县驻地": ("qgis.sx_xa_county", "sx_xa_county.gpkg"),
"县界": ("qgis.sx_xa_county_boundary", "sx_xa_county_boundary.gpkg"),
"周边区县": ("qgis.sx_zb_county_boundary", "sx_zb_county_boundary.gpkg"),
"周边市州": ("qgis.sx_zb_city", "sx_zb_city.gpkg"),
"周边县区": ("qgis.sx_zb_county", "sx_zb_county.gpkg"),
"traffic_expressway": ("qgis.traffic_expressway", "traffic_expressway.gpkg"),
"traffic_provincial": ("qgis.traffic_provincial", "traffic_provincial.gpkg"),
"traffic_railway": ("qgis.traffic_railway", "traffic_railway.gpkg"),
"traffic_township": ("qgis.traffic_township", "traffic_township.gpkg"),
"traffic_trunk_line": ("qgis.traffic_trunk_line", "traffic_trunk_line.gpkg"),
}
layers = {}
for name, (table, gpkg_file) in layer_defs.items():
layers[name] = {"file": gpkg_file, "table": table}
return {"enabled": True, "gpkg_dir": gpkg_dir, "layers": layers}
# ============================================================
# 接口实现
# ============================================================
@@ -246,29 +250,30 @@ async def export_map(req: QgisMapExportRequest):
"""
根据模拟ID批量导出专题图。同一 occurred_time 视为同一场灾害,共享文件夹
"""
from app.services.qgis.qgis_env import is_qgis_ready
if not is_qgis_ready():
raise HTTPException(status_code=503, detail="QGIS 环境未初始化,专题图功能不可用")
from app.services.qgis.qgis_env import is_qgis_available
if not is_qgis_available():
raise HTTPException(status_code=503, detail="QGIS 环境不可用(未找到 QGIS Python 3.12 解释器)")
semaphore = get_prediction_semaphore()
async with semaphore:
simulation_id = req.simulationId
inference_id = req.inferenceId
loop = asyncio.get_event_loop()
try:
# 查询推理记录,获取 occurred_time
inference = await loop.run_in_executor(
None, qgis_repository.query_inference_result, simulation_id
None, qgis_repository.query_inference_result, inference_id
)
# 将 occurred_time 格式化为时间戳作为文件夹名
disaster_time = format_disaster_time(inference["occurred_time"])
event_type = inference["event_type"]
# 构建批次文件夹路径
file_store = getattr(settings, "FILE_STORE_DIR", "G:/files")
output_tmpl = getattr(settings, "QGIS_OUTPUT_DIR", "xian/qgis/map/:disasterTime")
output_dir = output_tmpl.replace(":disasterTime", disaster_time)
output_tmpl = getattr(settings, "QGIS_OUTPUT_DIR", "xian/qgis/map/:eventType/:disasterTime")
output_dir = output_tmpl.replace(":eventType", event_type).replace(":disasterTime", disaster_time)
batch_folder = os.path.join(file_store, output_dir).replace("\\", "/")
# 去重检查:同一 disasterTime 只产图一次
@@ -318,29 +323,41 @@ async def export_map(req: QgisMapExportRequest):
# 推导参数 + 构建配置
os.makedirs(batch_folder, exist_ok=True)
event_type = inference["event_type"]
template_name_map = {"earthquake": "地震专题图", "rainfall": "降雨专题图"}
template_name = template_name_map.get(event_type, f"{event_type}专题图")
out_filename = f"{template_name}.jpg"
model = _derive_model_params(inference, batch_folder, out_filename)
config = _build_qgis_config(batch_folder)
logger.info(
f"模板路径: {model['path']}, "
f"输出: {model['outFile']}, "
f"标题: {model['mapTitle']}, "
f"中心: ({model['centerX']}, {model['centerY']})"
# 扫描模板文件夹下所有 .qgz 文件
template_base = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
"app", "data", "template"
)
template_dir = os.path.join(template_base, event_type)
template_files = sorted([
f for f in os.listdir(template_dir)
if f.endswith(".qgz") and not f.startswith("tmp")
])
# 提交到线程池
_thread_pool.submit(
_generate_single_map, model, config, disaster_time
)
if not template_files:
raise FileNotFoundError(f"模板文件夹为空: {template_dir}")
# 构建所有模型参数(批量模式)
models = []
for tpl_file in template_files:
tpl_path = os.path.join(template_dir, tpl_file).replace("\\", "/")
model = _derive_model_params(inference, batch_folder, tpl_path)
logger.info(
f"模板: {tpl_path}, "
f"输出: {model['outFile']}, "
f"标题: {model['mapTitle']}, "
f"中心: ({model['centerX']}, {model['centerY']})"
)
models.append(model)
# 一次性提交所有模型到 QGIS 子进程(单次 DLL 加载)
_generate_batch_maps(models, config, disaster_time)
return QgisMapExportResponse(
code=200,
message="任务已提交",
message=f"任务已完成,共{len(models)}张专题图",
data=disaster_time,
)
@@ -357,22 +374,85 @@ async def export_map(req: QgisMapExportRequest):
pass
def _generate_single_map(model: dict, config: dict, disaster_time: str) -> None:
"""线程池工作函数:在工作线程中生成单张专题图"""
from app.services.qgis.map_service import MapService
def _generate_batch_maps(models: list, config: dict, disaster_time: str) -> None:
"""通过 QGIS Python 3.12 子进程批量生成专题图(单次 DLL 加载)"""
import json
import subprocess
import tempfile
from app.services.qgis.qgis_env import build_qgis_command
try:
logger.info(f"[线程池] 开始产图: {model['mapTitle']}{model['outFile']}")
service = MapService(config)
service.generate(model)
logger.info(f"[批量产图] 开始: {len(models)} 张专题图, batch={disaster_time}")
if os.path.exists(model["outFile"]):
logger.info(f"[线程池] 产图成功: {model['outFile']}")
# 构建子进程请求 JSON —— 批量格式
request_data = json.dumps(
{"config": config, "models": models},
ensure_ascii=False,
)
# 将请求 JSON 写入临时文件(避免 stdin 管道问题)
tmp_json = tempfile.NamedTemporaryFile(
suffix=".json", delete=False, mode="w", encoding="utf-8"
)
tmp_json.write(request_data)
tmp_json.close()
try:
from config import settings
qgis_root = getattr(settings, "QGIS_ROOT", "D:/QGIS")
cmd = build_qgis_command(qgis_root)
cmd.append(tmp_json.name)
logger.info(f"[批量产图] 启动 QGIS 子进程: {' '.join(cmd[:3])}...")
result = subprocess.run(
cmd,
capture_output=True,
timeout=300, # 5 分钟超时(批量处理多个模板)
)
finally:
try:
os.remove(tmp_json.name)
except OSError:
pass
if result.returncode != 0:
stderr_text = result.stderr.decode("utf-8", errors="replace").strip()
logger.error(f"[批量产图] QGIS 子进程失败 (exit={result.returncode}):")
for line in stderr_text.split("\n"):
logger.error(f" {line}")
raise RuntimeError(
f"QGIS 子进程失败: {stderr_text[:300]}"
)
# 解析子进程输出
stdout_text = result.stdout.decode("utf-8", errors="replace").strip()
if stdout_text:
for line in reversed(stdout_text.split("\n")):
line = line.strip()
if line.startswith("{"):
output = json.loads(line)
batch_results = output.get("results", [])
success_count = sum(1 for r in batch_results if r.get("success"))
fail_count = len(batch_results) - success_count
logger.info(
f"[批量产图] 完成: 成功={success_count}, 失败={fail_count}"
)
for r in batch_results:
if r.get("success"):
logger.info(f"{r.get('output', 'N/A')}")
else:
logger.error(f"{r.get('error', 'unknown')}")
break
else:
logger.warning("[批量产图] 子进程输出中未找到 JSON 结果")
else:
logger.error(f"[线程池] 产图后文件不存在: {model['outFile']}")
logger.warning("[批量产图] 子进程无输出,但 exit code = 0")
except subprocess.TimeoutExpired:
logger.error(f"[批量产图] QGIS 子进程超时 (300s)")
except Exception as e:
logger.error(f"[线程池] 产图失败: {model['name']} {e}", exc_info=True)
logger.error(f"[批量产图] 产图失败: {e}", exc_info=True)
# ============================================================