Python实现地图坐标系转换:从WGS84到百度BD09
本文详解如何利用Python将WGS84国际坐标系转换为国内常用的GCJ02及BD09百度坐标系。文章对比了主流地图API的坐标标准,提供了完整的转换算法代码,并演示了通过CSV批量处理经纬度数据并在百度地图API验证的全过程,适用于需要处理地理位置数据的开发者。
在最近的项目开发中,遇到了地图坐标转换的实际需求。
- 不同地图API采用的坐标系统存在差异,需要进行相应的转换处理。
- WGS84坐标系是国际通用的地球坐标系,大多数包含GPS芯片或北斗芯片的设备所获取的经纬度数据均基于此标准。
- 谷歌地图在全球范围内(中国地区除外)采用WGS84地理坐标系。
- GCJ02坐标系,俗称火星坐标系,是由中国国家测绘局制定的地理信息系统坐标标准,该坐标系是在WGS84基础上进行加密处理后的结果。
- 谷歌中国地图以及搜搜中国地图均使用GCJ02地理坐标系;BD09坐标系即百度坐标系,是在GCJ02基础上进一步加密形成的坐标系统。
- 搜狗坐标系、图吧坐标系等其他地图服务,推测也是基于GCJ02坐标系进行加密衍生而来。
随后,通过Python脚本在CSV文件中执行坐标转换操作。
最终,利用百度地图API对转换后的数据进行校验,确认转换结果正确无误。
#http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
#代码原地址
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('2017.040'+ 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'])
l=[]
with open(n,newline='',encoding='UTF-8') as f:
reader = csv.DictReader(f)
for row in reader:
if row['md5'] == 'md5':
continue
else:
y=float(row['lng'])
x=float(row['lat'])
loc=wgs2bd(x,y)
l.append([row['md5'],row['content'],row['phone'],row['conntime'],row['recitime'],loc[0],loc[1]])
nodes.writerows(l)
csvfile.close()
print("转换成功")
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













