说明
在天地图下载了某个市级行政区划的GeoJSON数据,
但需要将数据转化后才能放入ArcGIS转换后的SHP坐标为GCS_China_Geodetic_Coordinate_System_2000
成品下载
请根据现实地理位置选择下载源
国内用户:GitCode
海外用户:GitHub
拓展
天地图行政区划下载地址:https://cloudcenter.tianditu.gov.cn/administrativeDivision/
脚本代码
import geopandas as gpd
import tkinter as tk
from tkinter import filedialog
def select_and_convert():
# 初始化 tkinter 窗口,但不显示
root = tk.Tk()
root.withdraw()
print("请选择要转换的 GeoJSON 文件...")
# 打开文件选择对话框,选择输入的 GeoJSON 文件
input_geojson = filedialog.askopenfilename(
title="选择输入的 GeoJSON 文件",
filetypes=[("GeoJSON files", "*.geojson"), ("All files", "*.*")]
)
if not input_geojson: # 如果用户取消了选择
print("未选择输入文件,程序退出。")
return
print(f"已选择输入文件: {input_geojson}")
print("\n请选择输出 Shapefile 文件的保存位置和名称...")
# 打开文件保存对话框,选择输出的 Shapefile 文件路径
output_shp = filedialog.asksaveasfilename(
title="保存输出的 Shapefile 文件",
defaultextension=".shp",
filetypes=[("Shapefile", "*.shp"), ("All files", "*.*")]
)
if not output_shp: # 如果用户取消了选择
print("未选择输出文件,程序退出。")
return
print(f"已选择输出文件: {output_shp}")
try:
print("\n正在读取 GeoJSON 文件...")
# 使用 geopandas 读取 GeoJSON 文件
gdf = gpd.read_file(input_geojson)
print("正在转换并保存为 Shapefile...")
# 将 GeoDataFrame 保存为 Shapefile
# driver='ESRI Shapefile' 参数明确指定驱动程序
gdf.to_file(output_shp, driver='ESRI Shapefile', encoding='utf-8')
print(f"\n转换成功!Shapefile 已保存至: {output_shp}")
# 尝试打印一些基本信息
print(f"- 包含要素数量: {len(gdf)}")
print(f"- 坐标参考系统 (CRS): {gdf.crs}")
if not gdf.empty:
print("- 属性字段:", list(gdf.columns))
except Exception as e:
print(f"转换过程中发生错误: {e}")
finally:
# 关闭 tkinter 窗口资源
root.destroy()
if __name__ == "__main__":
select_and_convert()