1.python环境安装
1.1如果已有python环境,此步骤跳过。
1.2如果没有python环境
至python官方网站Download Python | Python.org下载:建议下载最新的python3版本
【注意】此处需勾选Add Python3.6 to PATH选项,这一步是配置环境变量,勾选后会节省很多步骤!!!
1.3.验证Python环境
命令框中输入:python 如果出现下图所示显示python版本号,即为安装好了python环境。
2.坐标转换
2.1数据准备
下图所示为某处的json管线数据,坐标系为投影坐标,转换之前我们需要知道该数据投影坐标系EPSG编码。
根据投影带找EPSG编码方式如下:EPSG代号-坐标系对应表_epsg查询-CSDN博客。
本数据为EPSG:4548。
2.2python代码撰写
其中的pipe.json文件为待转换文件,pipe_covert_WGS84为输出的新文件名。
import json
from pyproj import Proj, transform
# 打开外部 JSON 文件
with open('pipe.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 坐标转换函数
def convert_coordinates(x, y):
in_proj = Proj(init='epsg:4548') # 假设输入坐标系为墨卡托投影
out_proj = Proj(init='epsg:4326') # 输出坐标系为经纬度坐标系
lon, lat = transform(in_proj, out_proj, x, y)
return lon, lat
# 提取每个 Feature 中的坐标进行转换
for feature in data['features']:
coords = feature['geometry']['coordinates']
for i in range(len(coords)):
lon, lat = convert_coordinates(coords[i][0], coords[i][1])
coords[i] = [lon, lat]
# 写入更新后的 JSON 文件,确保中文不转为 Unicode 编码
with open('pipe_covert_WGS84.json', 'w', encoding='utf-8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
2.3转换操作
将待转换坐标的json文件与python文件放置于同目录下:
在该目录中打开命令行窗口输入命令:python Coord_Change.py,回车运行文件。
运行结果:
坐标转换完成,通过python进行坐标转换,经与使用ArcGIS进行转换对比,两者生成的经纬度小数点后8位一致。