代码分析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操作mysql中文显示乱码的解决方法
Oct 11 Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
Nov 13 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
Jan 20 Python
Python脚本获取操作系统版本信息
Dec 17 Python
Python基于QRCode实现生成二维码的方法【下载,安装,调用等】
Jul 11 Python
使用Python读取安卓手机的屏幕分辨率方法
Mar 31 Python
python字符串与url编码的转换实例
May 10 Python
python爬取网页内容转换为PDF文件
Jul 28 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
Dec 27 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
Python基于pyecharts实现关联图绘制
Mar 27 Python
浅谈django框架集成swagger以及自定义参数问题
Jul 07 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
世界咖啡生产者论坛呼吁:需要立即就咖啡价格采取认真行动
2021/03/06 咖啡文化
php动态生成JavaScript代码
2009/03/09 PHP
Pain 全世界最小最简单的PHP模板引擎 (普通版)
2011/10/23 PHP
PHP date()函数警告: It is not safe to rely on the system解决方法
2014/08/20 PHP
php静态成员方法和静态的成员属性的使用方法
2017/10/26 PHP
PhpStorm2020.1 安装 debug - Postman 调用的详细教程
2020/08/17 PHP
25个非常棒的jQuery滑块插件和教程小结
2011/09/02 Javascript
jQuery的内容过滤选择器学习教程
2016/04/18 Javascript
浅析BootStrap模态框的使用(经典)
2016/04/29 Javascript
JS简单去除数组中重复项的方法
2016/09/13 Javascript
使用bootstrapValidator插件进行动态添加表单元素并校验
2016/09/28 Javascript
js实现带缓动动画的导航栏效果
2017/01/16 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
js实现AI五子棋人机大战
2020/05/28 Javascript
Vue学习笔记之计算属性与侦听器用法
2019/12/07 Javascript
vue实现列表滚动的过渡动画
2020/06/29 Javascript
[46:59]完美世界DOTA2联赛PWL S2 GXR vs Ink 第二场 11.19
2020/11/20 DOTA
Python常见数据结构详解
2014/07/24 Python
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
CentOS 6.5中安装Python 3.6.2的方法步骤
2017/12/03 Python
python模块之paramiko实例代码
2018/01/31 Python
python爬虫_实现校园网自动重连脚本的教程
2018/04/22 Python
基于python实现简单日历
2018/07/28 Python
mvc框架打造笔记之wsgi协议的优缺点以及接口实现
2018/08/01 Python
浅谈pycharm的xmx和xms设置方法
2018/12/03 Python
Python-openCV读RGB通道图实例
2020/01/17 Python
K最近邻算法(KNN)---sklearn+python实现方式
2020/02/24 Python
python suds访问webservice服务实现
2020/06/26 Python
pandas.DataFrame.drop_duplicates 用法介绍
2020/07/06 Python
加拿大便宜的隐形眼镜商店:Clearly
2016/09/15 全球购物
捷克汽车配件和工具销售网站:TorriaCars
2018/02/26 全球购物
C语言编程题
2015/03/09 面试题
电子商务专业个人的自我评价分享
2013/10/29 职场文书
学习十八大精神心得体会
2013/12/31 职场文书
个人工作表现评语
2014/04/30 职场文书
学校实习推荐信
2015/03/27 职场文书