967fa0f59c
- test-linux: platform=linux, docker + python3 - test-windows: platform=windows, arcpy + python3 - 附示例 GeoJSON: 云南/昆明/大理/百色 Ref: SuiteHub/agent-profiles#22
91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Step 3: 输出 Excel
|
|
将叠加分析结果写入 Excel 文件
|
|
"""
|
|
import sys
|
|
import json
|
|
import os
|
|
from openpyxl import Workbook
|
|
from openpyxl.styles import Font, Alignment, PatternFill
|
|
|
|
|
|
def main():
|
|
result_dir = os.environ.get("PARAM_RESULT_DIR", "/tmp/output/result")
|
|
output_path = os.environ.get("PARAM_OUTPUT_PATH", "")
|
|
|
|
if not output_path:
|
|
print(json.dumps({"error": "缺少 output_path 参数", "success": False}))
|
|
sys.exit(1)
|
|
|
|
# 读取分析结果
|
|
result_json = os.path.join(result_dir, "overlay_results.json")
|
|
if not os.path.isfile(result_json):
|
|
print(json.dumps({"error": f"结果文件不存在: {result_json}", "success": False}))
|
|
sys.exit(1)
|
|
|
|
with open(result_json, "r", encoding="utf-8") as f:
|
|
results = json.load(f)
|
|
|
|
# 创建工作簿
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws.title = "叠加分析结果"
|
|
|
|
# 标题样式
|
|
header_font = Font(bold=True, size=12, color="FFFFFF")
|
|
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
|
|
header_align = Alignment(horizontal="center", vertical="center")
|
|
|
|
# 表头
|
|
headers = ["城市", "城市代码", "城市名称", "人口", "在省内", "相交", "状态"]
|
|
for col, h in enumerate(headers, 1):
|
|
cell = ws.cell(row=1, column=col, value=h)
|
|
cell.font = header_font
|
|
cell.fill = header_fill
|
|
cell.alignment = header_align
|
|
|
|
# 数据行
|
|
pass_fill = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
|
|
fail_fill = PatternFill(start_color="FCE4EC", end_color="FCE4EC", fill_type="solid")
|
|
|
|
for row_idx, r in enumerate(results, 2):
|
|
ws.cell(row=row_idx, column=1, value=r.get("city", ""))
|
|
ws.cell(row=row_idx, column=2, value=r.get("city_code", ""))
|
|
ws.cell(row=row_idx, column=3, value=r.get("city_name", ""))
|
|
ws.cell(row=row_idx, column=4, value=r.get("population", 0))
|
|
ws.cell(row=row_idx, column=5, value="是" if r.get("in_province") else "否")
|
|
ws.cell(row=row_idx, column=6, value="是" if r.get("intersects") else "否")
|
|
ws.cell(row=row_idx, column=7, value=r.get("status", ""))
|
|
|
|
# 条件高亮
|
|
fill = pass_fill if r.get("in_province") else fail_fill
|
|
for col in range(1, 8):
|
|
ws.cell(row=row_idx, column=col).fill = fill
|
|
|
|
# 调整列宽
|
|
ws.column_dimensions["A"].width = 15
|
|
ws.column_dimensions["B"].width = 12
|
|
ws.column_dimensions["C"].width = 15
|
|
ws.column_dimensions["D"].width = 12
|
|
ws.column_dimensions["E"].width = 10
|
|
ws.column_dimensions["F"].width = 10
|
|
ws.column_dimensions["G"].width = 15
|
|
|
|
# 确保输出目录存在
|
|
out_dir = os.path.dirname(output_path)
|
|
if out_dir:
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
|
|
wb.save(output_path)
|
|
print(json.dumps({
|
|
"success": True,
|
|
"output_path": output_path,
|
|
"total": len(results),
|
|
"msg": f"Excel 已输出到: {output_path}"
|
|
}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|