同事开发需要将web墨卡托投影坐标转为WGS 84坐标,记忆不是很清晰,拿python试验了下,也当是复习。
转换公式:
x = lon*20037508.34/180
y = log(tan((90+lat)*PI/360))/(PI/360)*20037508.34/180
注:lon为经度,lat为纬度,PI为π。
def wgs84toWebMercator(lon,lat):
x = lon*20037508.342789/180
y =math.log(math.tan((90+lat)*math.pi/360))/(math.pi/180)
y = y *20037508.34789/180
return x,y
实际上是WGS 84坐标转web墨卡托坐标的反向求解
转换公式:
lon= x/20037508.34*180
lat= 180/(PI*(2*atan(exp(y/20037508.34*180*PI/180))-PI/2)
注:lon为经度,lat为纬度,PI为π。
#WebMercator-wgs84
def webMercator2wgs84(x,y):
lon = x/20037508.34*180
lat = y/20037508.34*180
lat= 180/math.pi*(2*math.atan(math.exp(lat*math.pi/180))-math.pi/2)
return lon,lat
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author:zhoulong_GISER
# -*- coding: utf-8 -*-
# blog:https://blog.csdn.net/qq_33356563
#wgs84-WebMercator
import math
def wgs84toWebMercator(lon,lat):
x = lon*20037508.342789/180
y =math.log(math.tan((90+lat)*math.pi/360))/(math.pi/180)
y = y *20037508.34789/180
return x,y
#WebMercator-wgs84
def webMercator2wgs84(x,y):
lon = x/20037508.34*180
lat = y/20037508.34*180
lat= 180/math.pi*(2*math.atan(math.exp(lat*math.pi/180))-math.pi/2)
return lon,lat
if __name__=="__main__":
xylist = [12980914.6304,4912134.49832]
transform1 = webMercator2wgs84( xylist[0], xylist[1])
print('WebMercator坐标:',xylist[0], xylist[1])
print('转换后WGS84坐标:',transform1[0],transform1[1])
lonlatlist = [116.650155437,40.377151862]
transform2 = wgs84toWebMercator(lonlatlist[0], lonlatlist[1])
print('WGS84坐标:',lonlatlist[0], lonlatlist[1] )
print('转换后WebMercator坐标:',transform2[0],transform2[1] )