feat: 添加 Linux/Windows 双平台测试套件(云南省界叠加分析)
- test-linux: platform=linux, docker + python3 - test-windows: platform=windows, arcpy + python3 - 附示例 GeoJSON: 云南/昆明/大理/百色 Ref: SuiteHub/agent-profiles#22
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Step 1: GeoJSON → SHP
|
||||
将省界和城市 GeoJSON 文件转换为 Shapefile
|
||||
"""
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
import geopandas as gpd
|
||||
|
||||
|
||||
def main():
|
||||
province_path = os.environ.get("PARAM_PROVINCE", "")
|
||||
cities_param = os.environ.get("PARAM_CITIES", "")
|
||||
output_dir = os.environ.get("PARAM_OUTPUT_DIR", "/tmp/output/shp")
|
||||
|
||||
if not province_path:
|
||||
print(json.dumps({"error": "缺少 province 参数", "success": False}))
|
||||
sys.exit(1)
|
||||
if not cities_param:
|
||||
print(json.dumps({"error": "缺少 cities 参数", "success": False}))
|
||||
sys.exit(1)
|
||||
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 读取并转换省界
|
||||
print(json.dumps({"msg": f"读取省界: {province_path}"}))
|
||||
province_gdf = gpd.read_file(province_path)
|
||||
province_shp = os.path.join(output_dir, "province.shp")
|
||||
province_gdf.to_file(province_shp, driver="ESRI Shapefile")
|
||||
print(json.dumps({"msg": f"省界已转为 SHP: {province_shp}", "count": len(province_gdf)}))
|
||||
|
||||
# 读取并转换每个城市 GeoJSON
|
||||
city_files = [c.strip() for c in cities_param.split(",") if c.strip()]
|
||||
converted = []
|
||||
for cf in city_files:
|
||||
if not os.path.isfile(cf):
|
||||
print(json.dumps({"warn": f"文件不存在,跳过: {cf}"}))
|
||||
continue
|
||||
print(json.dumps({"msg": f"读取城市: {cf}"}))
|
||||
gdf = gpd.read_file(cf)
|
||||
base = os.path.splitext(os.path.basename(cf))[0]
|
||||
shp_path = os.path.join(output_dir, f"{base}.shp")
|
||||
gdf.to_file(shp_path, driver="ESRI Shapefile")
|
||||
converted.append(base)
|
||||
print(json.dumps({"msg": f"已转换: {cf} → {shp_path}", "count": len(gdf)}))
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
"province_shp": province_shp,
|
||||
"city_count": len(converted),
|
||||
"cities": converted
|
||||
}
|
||||
print(json.dumps(result))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Step 2: 叠加分析
|
||||
判断各城市是否在省界内(spatial join),输出分析结果
|
||||
"""
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
import glob
|
||||
import geopandas as gpd
|
||||
|
||||
|
||||
def main():
|
||||
province_shp = os.environ.get("PARAM_PROVINCE_SHP", "")
|
||||
cities_dir = os.environ.get("PARAM_CITIES_DIR", "/tmp/output/shp")
|
||||
output_dir = os.environ.get("PARAM_OUTPUT_DIR", "/tmp/output/result")
|
||||
|
||||
if not province_shp or not os.path.isfile(province_shp):
|
||||
print(json.dumps({"error": f"省界 SHP 文件不存在: {province_shp}", "success": False}))
|
||||
sys.exit(1)
|
||||
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 读取省界
|
||||
print(json.dumps({"msg": f"读取省界 SHP: {province_shp}"}))
|
||||
province = gpd.read_file(province_shp)
|
||||
|
||||
if province.empty:
|
||||
print(json.dumps({"error": "省界数据为空", "success": False}))
|
||||
sys.exit(1)
|
||||
|
||||
# 确保坐标系一致
|
||||
target_crs = province.crs or "EPSG:4326"
|
||||
province = province.to_crs(target_crs)
|
||||
|
||||
# 查找所有城市 SHP
|
||||
city_shp_files = glob.glob(os.path.join(cities_dir, "*.shp"))
|
||||
city_shp_files = [f for f in city_shp_files
|
||||
if os.path.basename(f) != "province.shp"]
|
||||
|
||||
if not city_shp_files:
|
||||
print(json.dumps({"error": "未找到城市 SHP 文件", "success": False}))
|
||||
sys.exit(1)
|
||||
|
||||
results = []
|
||||
|
||||
for shp in city_shp_files:
|
||||
city_name = os.path.splitext(os.path.basename(shp))[0]
|
||||
print(json.dumps({"msg": f"分析城市: {city_name}"}))
|
||||
city = gpd.read_file(shp)
|
||||
|
||||
if city.empty:
|
||||
results.append({"city": city_name, "in_province": False,
|
||||
"reason": "数据为空", "status": "跳过"})
|
||||
continue
|
||||
|
||||
city = city.to_crs(target_crs)
|
||||
|
||||
# 空间判断:城市中心点是否在省界内
|
||||
city_centroid = city.geometry.centroid.iloc[0]
|
||||
in_province = province.geometry.contains(city_centroid).any()
|
||||
|
||||
# 也检查 intersect 作为辅助判断
|
||||
intersects = province.geometry.intersects(city.geometry).any()
|
||||
|
||||
status = "通过 ✅" if in_province else "不通过 ❌"
|
||||
|
||||
city_name_prop = city.iloc[0].get("name", city_name)
|
||||
city_code = city.iloc[0].get("code", "")
|
||||
city_pop = city.iloc[0].get("population", 0)
|
||||
|
||||
results.append({
|
||||
"city": city_name,
|
||||
"city_code": str(city_code),
|
||||
"city_name": str(city_name_prop),
|
||||
"population": int(city_pop) if city_pop else 0,
|
||||
"in_province": bool(in_province),
|
||||
"intersects": bool(intersects),
|
||||
"status": status
|
||||
})
|
||||
print(json.dumps({"msg": f"{city_name}: {status}"}))
|
||||
|
||||
# 输出分析结果 JSON
|
||||
result_path = os.path.join(output_dir, "overlay_results.json")
|
||||
with open(result_path, "w", encoding="utf-8") as f:
|
||||
json.dump(results, f, ensure_ascii=False, indent=2)
|
||||
|
||||
summary = {
|
||||
"success": True,
|
||||
"total_cities": len(results),
|
||||
"in_province": sum(1 for r in results if r["in_province"]),
|
||||
"not_in_province": sum(1 for r in results if not r["in_province"]),
|
||||
"results": results,
|
||||
"result_json": result_path
|
||||
}
|
||||
print(json.dumps(summary))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user