Python经纬度坐标转换为距离及角度的实现


Posted in Python onNovember 01, 2020

最近项目上有这样的需求,需要依据设备的经纬度坐标计算距离及角度。经验证后效果较好,并分享。

1 经纬度转换距离代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'


import math


# 计算距离
def getDistance(latA, lonA, latB, lonB):
  ra = 6378140 # 赤道半径
  rb = 6356755 # 极半径
  flatten = (ra - rb) / ra # Partial rate of the earth
  # change angle to radians
  radLatA = math.radians(latA)
  radLonA = math.radians(lonA)
  radLatB = math.radians(latB)
  radLonB = math.radians(lonB)

  pA = math.atan(rb / ra * math.tan(radLatA))
  pB = math.atan(rb / ra * math.tan(radLatB))
  x = math.acos(math.sin(pA) * math.sin(pB) + math.cos(pA) * math.cos(pB) * math.cos(radLonA - radLonB))
  c1 = (math.sin(x) - x) * (math.sin(pA) + math.sin(pB)) ** 2 / math.cos(x / 2) ** 2
  c2 = (math.sin(x) + x) * (math.sin(pA) - math.sin(pB)) ** 2 / math.sin(x / 2) ** 2
  dr = flatten / 8 * (c1 - c2)
  distance = ra * (x + dr)
  distance = round(distance / 1000, 4)
  return f'{distance}km'

2 经纬度转化角度代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'

import math

# 计算角度
def getDegree(latA, lonA, latB, lonB):
  radLatA = math.radians(latA)
  radLonA = math.radians(lonA)
  radLatB = math.radians(latB)
  radLonB = math.radians(lonB)
  dLon = radLonB - radLonA
  y = math.sin(dLon) * math.cos(radLatB)
  x = math.cos(radLatA) * math.sin(radLatB) - math.sin(radLatA) * math.cos(radLatB) * math.cos(dLon)
  brng = math.degrees(math.atan2(y, x))
  brng = round((brng + 360) % 360, 4)
  brng = int(brng)
  if (brng == 0.0) or ((brng == 360.0)):
    return '正北方向'
  elif brng == 90.0:
    return '正东方向'
  elif brng == 180.0:
    return '正南方向'
  elif brng == 270.0:
    return '正西方向'
  elif 0 < brng < 90:
    return f'北偏东{brng}'
  elif 90 < brng < 180:
    return f'东偏南{brng - 90}'
  elif 180 < brng < 270:
    return f'西偏南{270 - brng}'
  elif 270 < brng < 360:
    return f'北偏西{brng - 270}'
  else:
    pass

3 验证

选取深圳野生动物园(22.599578, 113.973129)为起点,深圳坪山站(22.6986848, 114.3311032)为终点,结合百度地图、谷歌地图等进行效果验证。

程序运行结果如下:

Python经纬度坐标转换为距离及角度的实现

百度测距为38.3km

Google地图手动测距为39.31km

Python经纬度坐标转换为距离及角度的实现

Python经纬度坐标转换为距离及角度的实现 

距离与角度均无问题。 

到此这篇关于Python经纬度坐标转换为距离及角度的实现的文章就介绍到这了,更多相关Python经纬度坐标转换为距离及角度内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
跟老齐学Python之list和str比较
Sep 20 Python
详解字典树Trie结构及其Python代码实现
Jun 03 Python
django admin 后台实现三级联动的示例代码
Jun 22 Python
python模拟菜刀反弹shell绕过限制【推荐】
Jun 25 Python
django将网络中的图片,保存成model中的ImageField的实例
Aug 07 Python
python使用sklearn实现决策树的方法示例
Sep 12 Python
Python中xml和dict格式转换的示例代码
Nov 07 Python
jupyter lab文件导出/下载方式
Apr 22 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
Apr 22 Python
Python之变量类型和if判断方式
May 05 Python
Python利用Faiss库实现ANN近邻搜索的方法详解
Aug 03 Python
深入了解Python装饰器的高级用法
Aug 13 Python
详解Anaconda安装tensorflow报错问题解决方法
Nov 01 #Python
python Cartopy的基础使用详解
Nov 01 #Python
Python中使用aiohttp模拟服务器出现错误问题及解决方法
Oct 31 #Python
关于python中导入文件到list的问题
Oct 31 #Python
python批量检查两个对应的txt文件的行数是否一致的实例代码
Oct 31 #Python
Python在线和离线安装第三方库的方法
Oct 31 #Python
python安装第三方库如xlrd的方法
Oct 31 #Python
You might like
咖啡豆分级制度 咖啡豆等级分类 咖啡豆是按口感分类的吗?
2021/03/05 新手入门
php中让上传的文件大小在上传前就受限制的两种解决方法
2013/06/24 PHP
php实现无限级分类(递归方法)
2015/08/06 PHP
微信开发之网页授权获取用户信息(二)
2016/01/08 PHP
extjs 的权限问题 要求控制的对象是 菜单,按钮,URL
2010/03/09 Javascript
基于jquery的无刷新分页技术
2011/06/11 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
调用jQuery滑出效果时闪烁的解决方法
2014/03/27 Javascript
jQuery中的一些常见方法小结(推荐)
2016/06/13 Javascript
Node.JS中事件轮询(Event Loop)的解析
2017/02/25 Javascript
微信小程序实战之自定义模态弹窗(8)
2017/04/18 Javascript
jQuery实现菜单栏导航效果
2017/08/15 jQuery
vue-cli + sass 的正确打开方式图文详解
2017/10/27 Javascript
element ui 对话框el-dialog关闭事件详解
2018/02/26 Javascript
官方推荐react-navigation的具体使用详解
2018/05/08 Javascript
jQuery实现table表格checkbox全选的方法分析
2018/07/04 jQuery
Weex开发之地图篇的具体使用
2019/10/16 Javascript
Vue v-bind动态绑定class实例方法
2020/01/15 Javascript
在Echarts图中给坐标轴加一个标识线markLine
2020/07/20 Javascript
[01:32]2014DOTA2西雅图邀请赛 CIS我们有信心进入正赛
2014/07/08 DOTA
[02:58]魔廷新尊——痛苦女王至宝语音台词节选
2020/06/14 DOTA
Pthon批量处理将pdb文件生成dssp文件
2015/06/21 Python
Python方法的延迟加载的示例代码
2017/12/18 Python
使用python画社交网络图实例代码
2019/07/10 Python
python 扩展print打印文件路径和当前时间信息的实例代码
2019/10/11 Python
python Tornado框架的使用示例
2020/10/19 Python
国际书籍零售商:Wordery
2017/11/01 全球购物
新西兰便宜隐形眼镜购买网站:QUICKLENS New Zealand
2019/03/02 全球购物
存储过程和函数的区别
2013/05/28 面试题
财务出纳员岗位职责
2013/11/26 职场文书
普通院校学生的自荐信
2013/11/27 职场文书
优秀中学生事迹材料
2014/01/31 职场文书
市场推广策划方案
2014/06/02 职场文书
上班旷工检讨书
2015/08/15 职场文书
js实现模拟购物商城案例
2021/05/18 Javascript
MySQL下使用Inplace和Online方式创建索引的教程
2021/05/26 MySQL