代码分析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中对list去重的多种方法
Sep 18 Python
Python实现的一个简单LRU cache
Sep 26 Python
python黑魔法之参数传递
Feb 12 Python
python多进程和多线程究竟谁更快(详解)
May 29 Python
python写一个md5解密器示例
Feb 23 Python
python将txt文档每行内容循环插入数据库的方法
Dec 28 Python
django模板结构优化的方法
Feb 28 Python
pyQT5 实现窗体之间传值的示例
Jun 20 Python
Python Django 命名空间模式的实现
Aug 09 Python
Python基于pygame实现单机版五子棋对战
Dec 26 Python
python入门之基础语法学习笔记
Feb 08 Python
如何把python项目部署到linux服务器
Aug 26 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 CURL 内存泄露问题解决方法
2015/02/12 PHP
PHP实现操作redis的封装类完整实例
2015/11/14 PHP
删除PHP数组中的重复元素的实现代码
2017/04/10 PHP
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
jquery插件qrcode在线生成二维码
2015/04/26 Javascript
jQuery数组处理函数整理
2016/08/03 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
jquery动态添加以及遍历option并获取特定样式名称的option方法
2018/01/29 jQuery
Vue导出页面为PDF格式的实现思路
2018/07/31 Javascript
layui 设置table 行的高度方法
2018/08/17 Javascript
trackingjs+websocket+百度人脸识别API实现人脸签到
2018/11/26 Javascript
vue操作动画的记录animate.css实例代码
2019/04/26 Javascript
jquery分页优化操作实例分析
2019/08/23 jQuery
javascript利用canvas实现鼠标拖拽功能
2020/07/23 Javascript
[48:38]DOTA2亚洲邀请赛 3.31 小组赛 B组 Mineski vs Secret
2018/03/31 DOTA
[01:11:28]DOTA2-DPC中国联赛定级赛 RNG vs Phoenix BO3第一场 1月8日
2021/03/11 DOTA
Python的函数嵌套的使用方法
2014/01/24 Python
python实现将英文单词表示的数字转换成阿拉伯数字的方法
2015/07/02 Python
Python selenium 三种等待方式详解(必会)
2016/09/15 Python
python3+PyQt5使用数据库窗口视图
2018/04/24 Python
django搭建项目配置环境和创建表过程详解
2019/07/22 Python
简单了解python中的与或非运算
2019/09/18 Python
python批量修改xml属性的实现方式
2020/03/05 Python
CSS3 函数技巧 用css 实现js实现的事情(clac Counters Tooltip)
2017/08/15 HTML / CSS
资生堂英国官网:Shiseido英国
2020/12/30 全球购物
暖通工程师岗位职责
2014/06/12 职场文书
校庆口号
2014/06/20 职场文书
打架赔偿协议书范本
2014/10/26 职场文书
2015年学校教务处工作总结
2015/05/11 职场文书
家庭聚会祝酒词
2015/08/11 职场文书
党员廉政准则心得体会
2016/01/20 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
python基础学习之递归函数知识总结
2021/05/26 Python
详解JAVA中的OPTIONAL
2021/06/14 Java/Android
关于springboot配置druid数据源不生效问题(踩坑记)
2021/09/25 Java/Android
一篇文章了解正则表达式的替换技巧
2022/02/24 Javascript