代码分析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发送以整个文件夹的内容为附件的邮件的教程
May 06 Python
python安装与使用redis的方法
Apr 19 Python
python3 模拟登录v2ex实例讲解
Jul 13 Python
Python中如何优雅的合并两个字典(dict)方法示例
Aug 09 Python
Python实现的本地文件搜索功能示例【测试可用】
May 30 Python
详解Python中的正则表达式
Jul 08 Python
python一键去抖音视频水印工具
Sep 14 Python
使用GitHub和Python实现持续部署的方法
May 09 Python
python实现知乎高颜值图片爬取
Aug 12 Python
pytorch对梯度进行可视化进行梯度检查教程
Feb 04 Python
python使用多线程查询数据库的实现示例
Aug 17 Python
python 实现非极大值抑制算法(Non-maximum suppression, NMS)
Oct 15 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
phpMyAdmin下载、安装和使用入门教程
2007/05/31 PHP
一些常用的php简单命令代码集锦
2007/09/24 PHP
php fseek函数读取大文件两种方法
2016/10/12 PHP
PHP常见加密函数用法示例【crypt与md5】
2019/01/27 PHP
Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
2010/08/27 Javascript
兼容IE、FireFox、Chrome等浏览器的xml处理函数js代码
2011/11/30 Javascript
游览器中javascript的执行过程(图文)
2012/05/20 Javascript
让AJAX不依赖后端接口实现方案
2012/12/03 Javascript
jquery判断RadioButtonList和RadioButton中是否有选中项示例
2013/09/29 Javascript
JavaScript数组和循环详解
2015/04/27 Javascript
JavaScript更改字符串的大小写
2015/05/07 Javascript
简单讲解AngularJS的Routing路由的定义与使用
2016/03/05 Javascript
ng-alain表单使用方式详解
2018/07/10 Javascript
Vue实现开心消消乐游戏算法
2019/10/22 Javascript
vue实现商城秒杀倒计时功能
2019/12/12 Javascript
JS简单表单验证功能完整示例
2020/01/26 Javascript
js实现点击按钮随机生成背景颜色
2020/09/05 Javascript
Vue 使用iframe引用html页面实现vue和html页面方法的调用操作
2020/11/16 Javascript
python正则表达式抓取成语网站
2013/11/20 Python
windows下python模拟鼠标点击和键盘输示例
2014/02/28 Python
Python中的Classes和Metaclasses详解
2015/04/02 Python
Python爬虫框架Scrapy实战之批量抓取招聘信息
2015/08/07 Python
python代码实现逻辑回归logistic原理
2019/08/07 Python
Django Haystack 全文检索与关键词高亮的实现
2020/02/17 Python
python如何代码集体右移
2020/07/20 Python
python使用建议技巧分享(三)
2020/08/18 Python
python 根据列表批量下载网易云音乐的免费音乐
2020/12/03 Python
Swisse官方海外旗舰店:澳大利亚销量领先,自然健康品牌
2017/12/15 全球购物
洛杉矶时尚女装系列:J.ING US
2019/03/17 全球购物
Vilebrequin美国官方网上商店:法国豪华泳装品牌
2020/02/22 全球购物
Linux面试题LINUX系统类
2014/11/19 面试题
编辑求职信样本
2013/12/16 职场文书
出纳工作岗位责任制
2014/02/02 职场文书
安全月宣传标语
2014/10/07 职场文书
长城导游词300字
2015/01/30 职场文书
员工辞退通知书
2015/04/17 职场文书