import math
import json
geojson ={"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[596461.703,4294428.963],[687371.885,4296710.895],[682376.833,4296942.36],[596461.703,4294428.963]]},"properties":{}}]}defweb_mercator_to_wgs84(x, y):
lon = x *180/20037508.34
lat =180/ math.pi *(2* math.atan(math.exp(y * math.pi /20037508.34))- math.pi /2)return lon, lat
# Convert coordinates in the GeoJSONfor feature in geojson["features"]:for i inrange(len(feature["geometry"]["coordinates"])):
x, y = feature["geometry"]["coordinates"][i]
feature["geometry"]["coordinates"][i]=list(web_mercator_to_wgs84(x, y))# Save the modified GeoJSON to a filewithopen('converted_geojson.json','w')as f:
json.dump(geojson, f, indent=2)print("转换后的GeoJSON已保存为converted_geojson.json")
UTM (3°带)平面转84
import json
from pyproj import Proj, transform
geojson ={"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[596461.703,4294428.963],[687371.885,4296710.895],[682376.833,4296942.36],[596461.703,4294428.963]]},"properties":{}}]}# 定义 UTM 3N 区投影和 WGS84 投影
utm_zone_3n = Proj(proj="utm", zone=30, ellps="WGS84")
wgs84 = Proj(proj="latlong", datum="WGS84")# 在 GeoJSON 中转换坐标for feature in geojson["features"]:for i inrange(len(feature["geometry"]["coordinates"])):
x, y = feature["geometry"]["coordinates"][i]
lon, lat = transform(utm_zone_3n, wgs84, x, y)
feature["geometry"]["coordinates"][i]=[lon, lat]# Save the modified GeoJSON to a filewithopen('converted_geojson.json','w')as f:
json.dump(geojson, f, indent=2)print("转换后的GeoJSON已保存为converted_geojson.json")