GPS经纬度坐标也称作火星系坐标,是标准的地理坐标,一般是WGS坐标。
但是国家为了保密,一般使用的地图上的坐标都不是标准的地理坐标,而是经过加密处理后的偏移坐标,一般会偏移几百米,甚至上千米。
当下,高德地图等使用的是GCJ坐标,而百度地图使用的是在此基础上又加密的BD坐标。
因此,要想调用百度地图API在地图上进行正确的标注位置,就要首先将WGS坐标转换成BD坐标。
代码如下:
import csv
import string
import time
import math
#系数常量
a = 6378245.0
ee = 0.00669342162296594323
x_pi = 3.14159265358979324 * 3000.0 / 180.0;
#转换经度
def transformLat(lat,lon):
ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon +0.2 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lon * math.pi) + 40.0 * math.sin(lon / 3.0 * math.pi)) * 2.0 / 3.0
ret += (160.0 * math.sin(lon / 12.0 * math.pi) + 320 * math.sin(lon * math.pi / 30.0)) * 2.0 / 3.0
return ret
#转换纬度
def transformLon(lat,lon):
ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lat * math.pi) + 40.0 * math.sin(lat / 3.0 * math.pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lat / 12.0 * math.pi) + 300.0 * math.sin(lat / 30.0 * math.pi)) * 2.0 / 3.0
return ret
#Wgs transform to gcj
def wgs2gcj(lat,lon):
dLat = transformLat(lon - 105.0, lat - 35.0)
dLon = transformLon(lon - 105.0, lat - 35.0)
radLat = lat / 180.0 * math.pi
magic = math.sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = math.sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * math.pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * math.pi)
mgLat = lat + dLat
mgLon = lon + dLon
loc=[mgLat,mgLon]
return loc
#gcj transform to bd2
def gcj2bd(lat,lon):
x=lon
y=lat
z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * x_pi)
theta = math.atan2(y, x) + 0.000003 * math.cos(x * x_pi)
bd_lon = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
bdpoint = [bd_lon,bd_lat]
return bdpoint
#wgs transform to bd
def wgs2bd(lat,lon):
wgs_to_gcj = wgs2gcj(lat,lon)
gcj_to_bd = gcj2bd(wgs_to_gcj[0], wgs_to_gcj[1])
return gcj_to_bd;
for i in range (3,4):
n = str('2017040'+ str(i)+'.csv')
m = str('2017040' + str(i)+'.csv')
csvfile = open(m,'w',encoding='UTF-8',newline='')
nodes = csv.writer(csvfile)
# nodes.writerow(['md5','content','phone','conntime','recitime','lng','lat'])
nodes.writerow(['lng','lat'])
nodes.writerow(['117.187','39.0998'])
# nodes.writerow(['117.184','39.0996'])
l=[]
with open(n,newline='',encoding='UTF-8') as f:
reader = csv.DictReader(f)
for row in reader:
if row['lng'] == 'lng':
continue
else:
#
y=float(row['lng'])
x=float(row['lat'])
loc=wgs2bd(x,y)
l.append([loc[0],loc[1]])
nodes.writerows(l)
csvfile.close()
然后在生成的文件中即可得到转换过得到的BD坐标。
经过对比,得到的经纬度坐标较精确。