代码分析Python地图坐标转换


Posted in Python onFebruary 08, 2018

最近做项目正好需要坐标的转换

  • 各地图API坐标系统比较与转换;
  • WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
  • 谷歌地图采用的是WGS84地理坐标系(中国范围除外);
  • GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
  • 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;
  • 搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的.

然后在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("转换成功")
Python 相关文章推荐
python下函数参数的传递(参数带星号的说明)
Sep 19 Python
Python正则表达式的使用范例详解
Aug 08 Python
用于统计项目中代码总行数的Python脚本分享
Apr 21 Python
在python中以相同顺序shuffle两个list的方法
Dec 13 Python
详解Python Matplot中文显示完美解决方案
Mar 07 Python
python 画3维轨迹图并进行比较的实例
Dec 06 Python
pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换
Jan 13 Python
Python要求O(n)复杂度求无序列表中第K的大元素实例
Apr 02 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
Apr 14 Python
python实现在内存中读写str和二进制数据代码
Apr 24 Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
May 13 Python
Python游戏开发实例之graphics实现AI五子棋
Nov 01 Python
python爬虫中get和post方法介绍以及cookie作用
Feb 08 #Python
Python OpenCV 直方图的计算与显示的方法示例
Feb 08 #Python
python OpenCV学习笔记之绘制直方图的方法
Feb 08 #Python
Python列表推导式与生成器表达式用法示例
Feb 08 #Python
详解python OpenCV学习笔记之直方图均衡化
Feb 08 #Python
python OpenCV学习笔记实现二维直方图
Feb 08 #Python
Python数据分析之双色球基于线性回归算法预测下期中奖结果示例
Feb 08 #Python
You might like
组合算法的PHP解答方法
2012/02/04 PHP
php.ini修改php上传文件大小限制的方法详解
2013/06/17 PHP
部署PHP项目应该注意的几点事项分享
2013/12/20 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
2014/07/05 PHP
php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比
2015/04/14 PHP
10条php编程小技巧
2015/07/07 PHP
PHP排序算法之堆排序(Heap Sort)实例详解
2018/04/21 PHP
js原生appendChild的bug解决心得分享
2013/07/01 Javascript
html文本框提示效果的示例代码
2014/06/28 Javascript
基于jquery固定于顶部的导航响应浏览器滚动条事件
2014/11/02 Javascript
JS表的模拟方法
2015/02/05 Javascript
JavaScript观察者模式(经典)
2015/12/09 Javascript
jQuery获取cookie值及删除cookie用法实例
2016/04/15 Javascript
JS中的hasOwnProperty()和isPrototypeOf()属性实例详解
2016/08/11 Javascript
详解javascript事件绑定使用方法
2016/10/20 Javascript
AngularJS双向绑定和依赖反转实例详解
2017/04/15 Javascript
让你5分钟掌握9个JavaScript小技巧
2018/06/09 Javascript
webpack 3.X学习之多页面打包的方法
2018/09/04 Javascript
Layui tree 下拉菜单树的实例代码
2019/09/21 Javascript
Vue使用NProgress进度条的方法
2019/09/21 Javascript
JS三级联动代码格式实例详解
2019/12/30 Javascript
Python程序语言快速上手教程
2012/07/18 Python
Python制作Windows系统服务
2017/03/25 Python
Python验证文件是否可读写代码分享
2017/12/11 Python
Python内置模块hashlib、hmac与uuid用法分析
2018/02/12 Python
对python PLT中的image和skimage处理图片方法详解
2019/01/10 Python
Python 获取指定文件夹下的目录和文件的实现
2019/08/30 Python
PyQt5中多线程模块QThread使用方法的实现
2020/01/31 Python
tensorflow 获取checkpoint中的变量列表实例
2020/02/11 Python
Python自动发送和收取邮件的方法
2020/08/12 Python
python 装饰器重要在哪
2021/02/14 Python
Python中如何定义一个函数
2016/09/06 面试题
给幼儿园老师的表扬信
2014/01/19 职场文书
应届大学生求职信
2014/07/20 职场文书
2015商场元旦促销活动策划方案
2014/12/09 职场文书
python3中apply函数和lambda函数的使用详解
2022/02/28 Python