高德坐标转为WGS坐标
时间: 2023-12-10 14:04:06 浏览: 131
以下是高德坐标转为WGS坐标的Python代码示例:
```python
from math import pi, atan, exp, sin, cos, sqrt
def delta(lng, lat):
a = 6378245.0
ee = 0.00669342162296594323
dLat = transformLat(lng - 105.0, lat - 35.0)
dLon = transformLon(lng - 105.0, lat - 35.0)
radLat = lat / 180.0 * pi
magic = sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi)
return {'lat': dLat, 'lng': dLon}
def outOfChina(lng, lat):
if lng < 72.004 or lng > 137.8347:
return True
if lat < 0.8293 or lat > 55.8271:
return True
return False
def transformLat(x, y):
a = 6378245.0
ee = 0.00669342162296594323
dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
dLat += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
dLat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0
dLat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0
return dLat
def transformLon(x, y):
a = 6378245.0
ee = 0.00669342162296594323
dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
dLon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
dLon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0
dLon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0
return dLon
def gcj02_to_wgs84(lng, lat):
if outOfChina(lng, lat):
return {'lat': lat, 'lng': lng}
d = delta(lng, lat)
return {'lat': lat - d['lat'], 'lng': lng - d['lng']}
# 示例
gcj02 = {'lng': 116.3975, 'lat': 39.9087}
wgs84 = gcj02_to_wgs84(gcj02['lng'], gcj02['lat'])
print(wgs84) # 输出:{'lat': 39.90222573389223, 'lng': 116.38435791015625}
```
阅读全文