967fa0f59c
- test-linux: platform=linux, docker + python3 - test-windows: platform=windows, arcpy + python3 - 附示例 GeoJSON: 云南/昆明/大理/百色 Ref: SuiteHub/agent-profiles#22
59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
#!/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()
|