python 地图经纬度转换、纠偏的实例代码
作者:小菜菜1223 时间:2021-11-06 11:12:18
python 地图经纬度转换、纠偏的代码如下所示:
# -*- coding: utf-8 -*-
import json
import urllib
import math
x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626 # π
a = 6378245.0 # 长半轴
ee = 0.00669342162296594323 # 偏心率平方
class Geocoding:
def __init__(self, api_key):
self.api_key = api_key
def geocode(self, address):
"""
利用高德geocoding服务解析地址获取位置坐标
:param address:需要解析的地址
:return:
"""
geocoding = {'s': 'rsv3',
'key': self.api_key,
'city': '全国',
'address': address}
geocoding = urllib.urlencode(geocoding)
ret = urllib.urlopen("%s?%s" % ("http://restapi.amap.com/v3/geocode/geo", geocoding))
if ret.getcode() == 200:
res = ret.read()
json_obj = json.loads(res)
if json_obj['status'] == '1' and int(json_obj['count']) >= 1:
geocodes = json_obj['geocodes'][0]
lng = float(geocodes.get('location').split(',')[0])
lat = float(geocodes.get('location').split(',')[1])
return [lng, lat]
else:
return None
else:
return None
def gcj02_to_bd09(lng, lat):
"""
火星坐标系(GCJ-02)转百度坐标系(BD-09)
谷歌、高德——>百度
:param lng:火星坐标经度
:param lat:火星坐标纬度
:return:
"""
z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi)
theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi)
bd_lng = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
return [bd_lng, bd_lat]
def bd09_to_gcj02(bd_lon, bd_lat):
"""
百度坐标系(BD-09)转火星坐标系(GCJ-02)
百度——>谷歌、高德
:param bd_lat:百度坐标纬度
:param bd_lon:百度坐标经度
:return:转换后的坐标列表形式
"""
x = bd_lon - 0.0065
y = bd_lat - 0.006
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)
gg_lng = z * math.cos(theta)
gg_lat = z * math.sin(theta)
return [gg_lng, gg_lat]
def wgs84_to_gcj02(lng, lat):
"""
WGS84转GCJ02(火星坐标系)
:param lng:WGS84坐标系的经度
:param lat:WGS84坐标系的纬度
:return:
"""
if out_of_china(lng, lat): # 判断是否在国内
return lng, lat
dlat = _transformlat(lng - 105.0, lat - 35.0)
dlng = _transformlng(lng - 105.0, lat - 35.0)
radlat = lat / 180.0 * pi
magic = math.sin(radlat)
magic = 1 - ee * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglng = lng + dlng
return [mglng, mglat]
def gcj02_to_wgs84(lng, lat):
"""
GCJ02(火星坐标系)转GPS84
:param lng:火星坐标系的经度
:param lat:火星坐标系纬度
:return:
"""
if out_of_china(lng, lat):
return lng, lat
dlat = _transformlat(lng - 105.0, lat - 35.0)
dlng = _transformlng(lng - 105.0, lat - 35.0)
radlat = lat / 180.0 * pi
magic = math.sin(radlat)
magic = 1 - ee * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglng = lng + dlng
return [lng * 2 - mglng, lat * 2 - mglat]
def bd09_to_wgs84(bd_lon, bd_lat):
lon, lat = bd09_to_gcj02(bd_lon, bd_lat)
return gcj02_to_wgs84(lon, lat)
def wgs84_to_bd09(lon, lat):
lon, lat = wgs84_to_gcj02(lon, lat)
return gcj02_to_bd09(lon, lat)
def _transformlat(lng, lat):
ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + \
0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng))
ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
math.sin(2.0 * lng * pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lat * pi) + 40.0 *
math.sin(lat / 3.0 * pi)) * 2.0 / 3.0
ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 *
math.sin(lat * pi / 30.0)) * 2.0 / 3.0
return ret
def _transformlng(lng, lat):
ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \
0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng))
ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
math.sin(2.0 * lng * pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lng * pi) + 40.0 *
math.sin(lng / 3.0 * pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 *
math.sin(lng / 30.0 * pi)) * 2.0 / 3.0
return ret
def out_of_china(lng, lat):
"""
判断是否在国内,不在国内不做偏移
:param lng:
:param lat:
:return:
"""
return not (lng > 73.66 and lng < 135.05 and lat > 3.86 and lat < 53.55)
if __name__ == '__main__':
lng = 118.7294833
lat = 31.9341833
result1 = gcj02_to_bd09(lng, lat)
result2 = bd09_to_gcj02(lng, lat)
result3 = wgs84_to_gcj02(lng, lat)
result4 = gcj02_to_wgs84(lng, lat)
result5 = bd09_to_wgs84(lng, lat)
result6 = wgs84_to_bd09(lng, lat)
s,v=wgs84_to_bd09(118.7294833,31.9341833)
#lng1,lat1= wgs84_to_gcj02(lng,lat)
#s,v =gcj02_to_bd09(lng1,lat1)
print s,v
#print result1, result2, result3, result4, result5, result6
总结
以上所述是小编给大家介绍的python 地图经纬度转换、纠偏的实例代码网站的支持!
来源:https://blog.csdn.net/qq_40771567/article/details/81450713
标签:python,地图,经纬度
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python+Pygame实现变异狗大战游戏
2021-04-19 09:46:45
![](https://img.aspxhome.com/file/2023/4/104844_0s.png)
Python中三种时间格式转换的方法
2023-10-18 13:11:43
![](https://img.aspxhome.com/file/2023/2/100932_0s.png)
在Django中使用ElasticSearch
2022-11-18 11:01:22
ASP.NET MVC Bundles 用法和说明(打包javascript和css)
2023-07-17 01:20:59
php实现通过cookie换肤的方法
2023-11-23 17:57:07
Python排序搜索基本算法之堆排序实例详解
2021-07-10 12:35:30
![](https://img.aspxhome.com/file/2023/9/103609_0s.png)
python jinjia2的项目使用
2021-03-16 04:57:21
css清除浮动“clear:both;”的应用
2008-03-17 12:54:00
php查找指定目录下指定大小文件的方法
2023-09-03 17:53:36
基于Python记录一场2023的烟花
2022-01-08 19:57:07
![](https://img.aspxhome.com/file/2023/1/109651_0s.png)
ASP教程:自己写的数据库操作类
2008-11-21 17:29:00
Python无损音乐搜索引擎实现代码
2021-01-28 19:19:02
![](https://img.aspxhome.com/file/2023/5/67325_0s.jpg)
Python Dataframe 指定多列去重、求差集的方法
2022-01-12 00:40:19
python 类详解及简单实例
2023-05-17 17:17:32
有感用户体验规划与系统实现
2009-11-27 18:33:00
![](https://img.aspxhome.com/file/UploadPic/200911/27/01-48s.jpg)
Asp定时执行操作 Asp定时读取数据库(网页定时操作详解)
2011-03-17 11:04:00
浅谈Python 敏感词过滤的实现
2022-12-11 07:47:17
python中日志logging模块的性能及多进程详解
2023-08-17 23:19:07
![](https://img.aspxhome.com/file/2023/5/78495_0s.png)
Python Unittest自动化单元测试框架详解
2023-09-13 23:13:56
![](https://img.aspxhome.com/file/2023/2/67822_0s.png)
Django 生成登陆验证码代码分享
2021-07-31 06:48:21
![](https://img.aspxhome.com/file/2023/8/79398_0s.jpg)