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中使用SQLite的简单教程
Apr 29 Python
使用Python生成url短链接的方法
May 04 Python
Python使用urllib2模块抓取HTML页面资源的实例分享
May 03 Python
python中如何使用朴素贝叶斯算法
Apr 06 Python
python实战教程之自动扫雷
Jul 13 Python
python使用selenium登录QQ邮箱(附带滑动解锁)
Jan 23 Python
python程序快速缩进多行代码方法总结
Jun 23 Python
连接pandas以及数组转pandas的方法
Jun 28 Python
python 实现多维数组转向量
Nov 30 Python
python之生成多层json结构的实现
Feb 27 Python
python用tkinter实现一个简易能进行随机点名的界面
Sep 27 Python
python单向链表实例详解
May 25 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
PHP网上调查系统
2006/10/09 PHP
php5 and xml示例
2006/11/22 PHP
用 PHP5 轻松解析 XML
2006/12/04 PHP
php获取指定日期之间的各个周和月的起止时间
2014/11/24 PHP
PHP中的Trait 特性及作用
2016/04/03 PHP
PHP对象链式操作实现原理分析
2016/10/09 PHP
ThinkPHP3.2框架自带分页功能实现方法示例
2019/05/13 PHP
JavaScript初学者应注意的七个细节详细介绍
2012/12/27 Javascript
js实现动态添加、删除行、onkeyup表格求和示例
2013/08/18 Javascript
在javascript中实现函数数组的方法
2013/12/25 Javascript
jquery实现个人中心导航菜单效果和美观都非常不错
2014/09/02 Javascript
jQuery实现复选框批量选择与反选的方法
2015/06/17 Javascript
D3.js中强制异步文件读取同步的几种方法
2017/02/06 Javascript
解决JQuery全选/反选第二次失效的问题
2017/10/11 jQuery
浅谈React前后端同构防止重复渲染
2018/01/05 Javascript
Postman的下载及安装教程详解
2018/10/16 Javascript
javascript判断一个变量是数组还是对象
2019/04/10 Javascript
Node.js安装详细步骤教程(Windows版)详解
2019/09/01 Javascript
JavaScript如何判断对象有某属性
2020/07/03 Javascript
Element中Slider滑块的具体使用
2020/07/29 Javascript
JS如何判断对象是否包含某个属性
2020/08/29 Javascript
[51:52]Liquid vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.24
2019/09/10 DOTA
[54:10]完美世界DOTA2联赛PWL S2 Magma vs FTD 第二场 11.29
2020/12/03 DOTA
python实现批量改文件名称的方法
2015/05/25 Python
使用简单工厂模式来进行Python的设计模式编程
2016/03/01 Python
python开发环境PyScripter中文乱码问题解决方案
2016/09/11 Python
centos6.4下python3.6.1安装教程
2017/07/21 Python
对python sklearn one-hot编码详解
2018/07/10 Python
使用python 的matplotlib 画轨道实例
2020/01/19 Python
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
凌阳科技股份有限公司C++程序员面试题笔试题
2014/11/20 面试题
网上签名寄语活动留言
2014/01/18 职场文书
六一儿童节主持词
2014/03/21 职场文书
幼儿评语大全
2014/04/30 职场文书
铅球加油稿100字
2014/09/26 职场文书
担保书范本
2015/01/20 职场文书