967fa0f59c
- test-linux: platform=linux, docker + python3 - test-windows: platform=windows, arcpy + python3 - 附示例 GeoJSON: 云南/昆明/大理/百色 Ref: SuiteHub/agent-profiles#22
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Step 1: GeoJSON → SHP (arcpy)
|
|
将省界和城市 GeoJSON 文件转换为 Shapefile
|
|
使用 arcpy 的 JSON To Feature Class 工具
|
|
"""
|
|
import sys
|
|
import json
|
|
import os
|
|
import arcpy
|
|
|
|
# 允许覆写输出
|
|
arcpy.env.overwriteOutput = True
|
|
|
|
|
|
def main():
|
|
province_path = os.environ.get("PARAM_PROVINCE", "")
|
|
cities_param = os.environ.get("PARAM_CITIES", "")
|
|
output_dir = os.environ.get("PARAM_OUTPUT_DIR", r"C:\temp\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)
|
|
|
|
if not os.path.isdir(output_dir):
|
|
os.makedirs(output_dir)
|
|
|
|
# 转换省界
|
|
print json.dumps({"msg": "转换省界: " + province_path})
|
|
province_name = os.path.splitext(os.path.basename(province_path))[0]
|
|
province_shp = os.path.join(output_dir, "province.shp")
|
|
# GeoJSON → Feature Class → Shapefile
|
|
arcpy.JSONToFeatures_conversion(province_path, province_shp)
|
|
print json.dumps({"msg": "省界已转为 SHP: " + province_shp})
|
|
|
|
# 转换每个城市
|
|
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": "文件不存在,跳过: " + cf})
|
|
continue
|
|
print json.dumps({"msg": "转换城市: " + cf})
|
|
base = os.path.splitext(os.path.basename(cf))[0]
|
|
shp_path = os.path.join(output_dir, base + ".shp")
|
|
arcpy.JSONToFeatures_conversion(cf, shp_path)
|
|
converted.append(base)
|
|
print json.dumps({"msg": "已转换: " + cf + " -> " + shp_path})
|
|
|
|
result = {
|
|
"success": True,
|
|
"province_shp": province_shp,
|
|
"city_count": len(converted),
|
|
"cities": converted
|
|
}
|
|
print json.dumps(result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|