代码分析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批量修改文件后缀示例代码分享
Dec 24 Python
Python获取远程文件大小的函数代码分享
May 13 Python
Python采用raw_input读取输入值的方法
Aug 18 Python
python妹子图简单爬虫实例
Jul 07 Python
Python中线程的MQ消息队列实现以及消息队列的优点解析
Jun 29 Python
python使用pil库实现图片合成实例代码
Jan 20 Python
Django中使用Whoosh进行全文检索的方法
Mar 31 Python
Django组件cookie与session的具体使用
Jun 05 Python
在pycharm下设置自己的个性模版方法
Jul 15 Python
简单了解python中的与或非运算
Sep 18 Python
利用PyTorch实现VGG16教程
Jun 24 Python
Python深度学习之实现卷积神经网络
Jun 05 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
简单实用的.net DataTable导出Execl
2013/10/28 PHP
PHP图片库imagemagick安装方法
2014/09/23 PHP
PHP保存session到memcache服务器的方法
2016/01/19 PHP
js函数般调用正则
2008/04/08 Javascript
利用javascript/jquery对上传文件格式过滤的方法
2009/07/25 Javascript
JavaScript遍历table表格中的某行某列并打印其值
2014/07/08 Javascript
深入理解JavaScript系列(27):设计模式之建造者模式详解
2015/03/03 Javascript
网页收藏夹显示ICO图标(代码少)
2015/08/04 Javascript
基于JavaScript实现类似于百度学术高级检索功能
2016/03/02 Javascript
微信小程序 简单教程实例详解
2017/01/13 Javascript
JavaScript EventEmitter 背后的秘密 完整版
2018/03/29 Javascript
微信小程序上传图片实例
2018/05/28 Javascript
vue生命周期和react生命周期对比【推荐】
2018/09/19 Javascript
微信小程序tabBar 返回tabBar不刷新页面
2019/07/25 Javascript
微信小程序实现同时上传多张图片
2020/02/03 Javascript
解决vue无法侦听数组及对象属性的变化问题
2020/07/17 Javascript
利用Django模版生成树状结构实例代码
2019/05/19 Python
python3 下载网络图片代码实例
2019/08/27 Python
termux中matplotlib无法显示中文问题的解决方法
2021/01/11 Python
关于webview适配H5上传照片或者视频文件的方法
2020/11/04 HTML / CSS
法国时尚童装网站:Melijoe
2016/08/10 全球购物
全球最受追捧的运动服品牌领先数字目的地:Stylerunner
2020/11/25 全球购物
加拿大大码女装购物网站:Penningtons
2020/12/26 全球购物
公司拓展活动方案
2014/02/13 职场文书
暑期社会实践感言
2014/02/25 职场文书
拔河比赛口号
2014/06/10 职场文书
医学专业大学生求职信
2014/07/12 职场文书
办理房产过户的委托书
2014/09/14 职场文书
2014年党支部书记工作总结
2014/12/04 职场文书
毕业实习感受与体会
2015/05/26 职场文书
工人先锋号事迹材料(2016精选版)
2016/03/01 职场文书
2016年保险公众宣传日活动总结
2016/04/05 职场文书
Python爬虫实战之爬取携程评论
2021/06/02 Python
HTML+CSS 实现顶部导航栏菜单制作
2021/06/03 HTML / CSS
java如何实现获取客户端ip地址的示例代码
2022/04/07 Java/Android
Mysql中mvcc各场景理解应用
2022/08/05 MySQL