代码分析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中条件选择和循环语句使用方法介绍
Mar 13 Python
在Django的视图中使用数据库查询的方法
Jul 16 Python
python中使用正则表达式的连接符示例代码
Oct 10 Python
python 不同方式读取文件速度不同的实例
Nov 09 Python
python生成九宫格图片
Nov 19 Python
Python facenet进行人脸识别测试过程解析
Aug 16 Python
selenium+Chrome滑动验证码破解二(某某网站)
Dec 17 Python
Python终端输出彩色字符方法详解
Feb 11 Python
python logging设置level失败的解决方法
Feb 19 Python
Python实现汇率转换操作
May 03 Python
Python使用jupyter notebook查看ipynb文件过程解析
Jun 02 Python
你喜欢篮球吗?Python实现篮球游戏
Jun 11 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
Http 1.1 Etag 与 Last-Modified提高php效率
2008/01/10 PHP
PHP实现判断数组是一维、二维或几维的方法
2017/02/06 PHP
帮助避免错误的Javascript陷阱清单
2009/05/31 Javascript
JavaScript内核之基本概念
2011/10/21 Javascript
JavaScript splice()方法详解
2020/09/22 Javascript
Javascript Ajax异步读取RSS文档具体实现
2013/12/12 Javascript
jquery text()方法取标签中的文本
2014/07/25 Javascript
当前流行的JavaScript代码风格指南
2014/09/10 Javascript
jQuery插件ImageDrawer.js实现动态绘制图片动画(附源码下载)
2016/02/25 Javascript
JavaScript知识点总结(四)之逻辑OR运算符详解
2016/05/31 Javascript
微信小程序 textarea 组件详解及简单实例
2017/01/10 Javascript
理解javascript中的闭包
2017/01/11 Javascript
JS实现的tab切换选项卡效果示例
2017/02/28 Javascript
浅析JS中的 map, filter, some, every, forEach, for in, for of 用法总结
2017/03/29 Javascript
解决Vue2.0自带浏览器里无法打开的原因(兼容处理)
2017/07/28 Javascript
JS实现的数组去除重复数据算法小结
2017/11/17 Javascript
JS正则表达式常见用法实例详解
2018/06/19 Javascript
vue+vue-router转场动画的实例代码
2018/09/01 Javascript
Vue搭建后台系统需要注意的问题
2019/11/08 Javascript
three.js 将图片马赛克化的示例代码
2020/07/31 Javascript
微信小程序实现页面左右滑动
2020/11/16 Javascript
[01:03:47]VP vs NewBee Supermajor 胜者组 BO3 第一场 6.5
2018/06/06 DOTA
python安装与使用redis的方法
2016/04/19 Python
梯度下降法介绍及利用Python实现的方法示例
2017/07/12 Python
python Flask 装饰器顺序问题解决
2018/08/08 Python
用python实现k近邻算法的示例代码
2018/09/06 Python
Django Channels 实现点对点实时聊天和消息推送功能
2019/07/17 Python
详解Python并发编程之创建多线程的几种方法
2019/08/23 Python
Python Numpy,mask图像的生成详解
2020/02/19 Python
奥斯汀独木舟和皮划艇:Austin Canoe & Kayak
2018/05/22 全球购物
馥蕾诗美国官网:Fresh美国
2019/10/09 全球购物
预备党员党课思想汇报
2014/01/13 职场文书
旅游网创业计划书
2014/01/31 职场文书
我的中国梦演讲稿800字
2014/08/19 职场文书
格林童话读书笔记
2015/06/30 职场文书
redis实现排行榜功能
2021/05/24 Redis