代码分析Python地图坐标转换

 更新时间:2018年02月08日 10:24:39   投稿:laozhang  
这篇文章主要介绍了Python地图坐标转换的相关知识点以及分享了相关的代码实例,对此有兴趣的朋友学习下。

最近做项目正好需要坐标的转换

  • 各地图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的SQLAlchemy框架使用入门

    Python的SQLAlchemy框架使用入门

    这篇文章主要介绍了Python的SQLAlchemy框架使用入门,SQLAlchemy框架是Python中用来操作数据库的ORM框架之一,需要的朋友可以参考下
    2015-04-04
  • Python报错too many values to unpack问题及解决

    Python报错too many values to unpack问题及解决

    这篇文章主要介绍了Python报错too many values to unpack问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • python访问抓取网页常用命令总结

    python访问抓取网页常用命令总结

    这篇文章主要介绍了python访问抓取网页常用命令的相关资料,需要的朋友可以参考下
    2017-04-04
  • python实现nao机器人手臂动作控制

    python实现nao机器人手臂动作控制

    这篇文章主要为大家详细介绍了python实现nao机器人手臂动作控制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • python中OrderedDict的使用方法详解

    python中OrderedDict的使用方法详解

    本篇文章主要介绍了python中OrderedDict的使用方法详解,非常具有实用价值,需要的朋友可以参考下
    2017-05-05
  • 详解Python time库的使用

    详解Python time库的使用

    这篇文章主要介绍了Python time库的使用,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • python实现AES加密与解密

    python实现AES加密与解密

    这篇文章主要为大家详细介绍了python实现AES加密与解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • python高阶爬虫实战分析

    python高阶爬虫实战分析

    这篇文章给大家分享了python高阶爬虫实战的相关实例内容以及技巧分析,有兴趣的朋友参考下。
    2018-07-07
  • python中numpy.dot()计算矩阵相乘

    python中numpy.dot()计算矩阵相乘

    本文主要介绍了python中numpy.dot()计算矩阵相乘,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • python判断自身是否正在运行的方法

    python判断自身是否正在运行的方法

    今天小编就为大家分享一篇python判断自身是否正在运行的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论