【投影转换】Geojson矢量投影坐标转换到84坐标系(经纬度)

Web墨卡托平面坐标系转84坐标系(经纬度)

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": {}
        }
    ]
}
def web_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 GeoJSON
for feature in geojson["features"]:
    for i in range(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 file
with open('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 in range(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 file
with open('converted_geojson.json', 'w') as f:
    json.dump(geojson, f, indent=2)

print("转换后的GeoJSON已保存为converted_geojson.json")

高斯-克吕格 转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": {}
        }
    ]
}
# 定义高斯-克吕格投影(这里假设带号为3)
# 高斯-克吕格投影的带号和中央经线
zone_number = 30
central_meridian = zone_number * 40  # 对于3度带,每个带的中央经线是带号乘以3

gauss_kruger = Proj(proj='tmerc', lat_0=0, lon_0=central_meridian, k=1, x_0=500000, y_0=0, ellps='WGS84', datum='WGS84')
wgs84 = Proj(proj='latlong', datum='WGS84')

# 转换GeoJSON中的坐标
for feature in geojson["features"]:
    for i in range(len(feature["geometry"]["coordinates"])):
        x, y = feature["geometry"]["coordinates"][i]
        lon, lat = transform(gauss_kruger, wgs84, x, y)
        feature["geometry"]["coordinates"][i] = [lon, lat]

# 保存修改后的GeoJSON到文件
with open('converted_geojson.json', 'w') as f:
    json.dump(geojson, f, indent=2)

print("转换后的GeoJSON已保存为converted_geojson.json")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值