基于Python的经纬度与xy坐标系相互转换

代码是已经运行过的,可以直接输入参数使用

注意事项:

  • 经纬度参数不可以写颠倒,否则会由于math.log参数不能为负数和0的原因报错

  • 由于地球半径的不确定性,所以(x,y)与(lon, lat)相互转换会有误差

奉上代码及运行结果:

  • 经纬度转换成xy坐标

实例经维度参数 (114.5456282282352,36.631262731204049)

def millerToXY (lon, lat):
    xy_coordinate = []
    #地球周长
    L = 6381372*math.pi*2 
    #平面展开,将周长视为X轴
    W = L 
    #Y轴约等于周长一般
    H = L/2 
    #米勒投影中的一个常数,范围大约在正负2.3之间
    mill = 2.3 
    #将经度从度数转换为弧度
    x = lon*math.pi/180 
    # 将纬度从度数转换为弧度
    y = lat*math.pi/180 
    #这里是米勒投影的转换
    y = 1.25*math.log(math.tan(0.25*math.pi+0.4*y)) 
    # 这里将弧度转为实际距离 ,转换结果的单位是公里
    x = (W/2)+(W/(2*math.pi))*x
    y = (H/2)-(H/(2*mill))*y
    xy_coordinate.append((int(round(x)),int(round(y))))
    return xy_coordinate
  • xy坐标转换成经纬度

def xy_to_coor(x, y):
    lonlat_coordinate = []
    L = 6381372 * math.pi*2
    W = L
    H = L/2
    mill = 2.3
    lat = ((H/2-y)*2*mill)/(1.25*H)
    lat = ((math.atan(math.exp(lat))-0.25*math.pi)*180)/(0.4*math.pi)
    lon = (x-W/2)*360/W
    # TODO 最终需要确认经纬度保留小数点后几位
    lonlat_coordinate.append((round(lon,15),round(lat,15)))
    return lonlat_coordinate
  • 调用两个函数

import math
print(millerToXY(114.5456282282352,36.631262731204049))
print(xy_to_coor(32805300, 7107399))

打印结果

[(32805300, 7107399)]
[(114.54562994611808, 36.63126574363226)]

  • 8
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值