处理经纬度数据时,经常需要计算欧式空间下的若干特征,比如长度、距离、角度等。本文介绍经纬度与墨卡托坐标系下的两两转换。
import math
#经纬度转墨卡托坐标
def ls(lng,lat):
# list=ll_wl.split(',')
lat=float(lat)
lng=float(lng)
x = lng* 20037508.34 / 180
y = math.log(math.tan((90 + lat) * math.pi / 360)) / (math.pi / 180)
y = y * 20037508.34 / 180
print (str(x),str(y))
#墨卡托坐标转经纬度坐标
def ll(x,y):
x=float(x)
y=float(y)
x=x/20037508.34*180
y=y/20037508.34*180
y=180/math.pi*(2*math.atan(math.exp(y*math.pi/180))-math.pi/2)
print (x,y)
if __name__ == '__main__':
ls('104.073694','30.697218')
ll('11585430.619442267','3593489.251439686')