fix: 测试数据打包进套件 data/,去掉外部输入参数
- 去掉 province/cities 参数,只保留 output_path - GeoJSON 数据放在 data/ 目录,脚本自动读取 - 版本升到 1.0.1 - MEMORY.md / TOOLS.md 补充 platform 发布参数说明 Ref: SuiteHub/agent-profiles#22
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Step 1: GeoJSON → SHP
|
||||
将省界和城市 GeoJSON 文件转换为 Shapefile
|
||||
从内置 data/ 目录读取 GeoJSON,转为 Shapefile
|
||||
"""
|
||||
import sys
|
||||
import json
|
||||
@@ -9,47 +9,47 @@ import os
|
||||
import geopandas as gpd
|
||||
|
||||
|
||||
def get_data_dir():
|
||||
"""返回内置 data/ 目录路径(相对脚本位置)"""
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
return os.path.normpath(os.path.join(script_dir, "..", "data"))
|
||||
|
||||
|
||||
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)}))
|
||||
data_dir = get_data_dir()
|
||||
|
||||
# 数据文件映射
|
||||
geo_files = {
|
||||
"province": "yunnan.geojson",
|
||||
"kunming": "kunming.geojson",
|
||||
"dali": "dali.geojson",
|
||||
"baise": "baise.geojson",
|
||||
}
|
||||
|
||||
# 读取并转换每个城市 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}"}))
|
||||
for name, filename in geo_files.items():
|
||||
filepath = os.path.join(data_dir, filename)
|
||||
if not os.path.isfile(filepath):
|
||||
print(json.dumps({"warn": f"内置数据文件不存在,跳过: {filepath}"}))
|
||||
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")
|
||||
print(json.dumps({"msg": f"读取内置数据: {filename}"}))
|
||||
gdf = gpd.read_file(filepath)
|
||||
shp_path = os.path.join(output_dir, f"{name}.shp")
|
||||
gdf.to_file(shp_path, driver="ESRI Shapefile")
|
||||
converted.append(base)
|
||||
print(json.dumps({"msg": f"已转换: {cf} → {shp_path}", "count": len(gdf)}))
|
||||
converted.append(name)
|
||||
print(json.dumps({"msg": f"已转换: {filename} → {shp_path}", "count": len(gdf)}))
|
||||
|
||||
if not converted:
|
||||
print(json.dumps({"error": "未找到任何内置 GeoJSON 数据", "success": False}))
|
||||
sys.exit(1)
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
"province_shp": province_shp,
|
||||
"city_count": len(converted),
|
||||
"cities": converted
|
||||
"output_dir": output_dir,
|
||||
"converted": converted
|
||||
}
|
||||
print(json.dumps(result))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user