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实现将pvr格式转换成pvr.ccz的方法
Apr 28 Python
让Python代码更快运行的5种方法
Jun 21 Python
Python实现简单多线程任务队列
Feb 27 Python
Python求算数平方根和约数的方法汇总
Mar 09 Python
在Linux命令行终端中使用python的简单方法(推荐)
Jan 23 Python
Python 列表理解及使用方法
Oct 27 Python
python输出带颜色字体实例方法
Sep 01 Python
python 命名规范知识点汇总
Feb 14 Python
python实现批量修改文件名
Mar 23 Python
如何将PySpark导入Python的放实现(2种)
Apr 26 Python
Python接口测试数据库封装实现原理
May 09 Python
python数字类型和占位符详情
Mar 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
PHP 多维数组排序(usort,uasort)
2010/06/30 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
php获取网页请求状态程序示例
2014/06/17 PHP
PHP超全局数组(Superglobals)介绍
2015/07/01 PHP
IE 缓存策略的BUG的解决方法
2007/07/21 Javascript
for 循环性能比较 提高for循环的效率
2009/03/19 Javascript
IE和FireFox(FF)中js和css的不同
2009/04/13 Javascript
跟着JQuery API学Jquery 之二 属性
2010/04/09 Javascript
HTML页面弹出居中可拖拽的自定义窗口层
2014/05/07 Javascript
超级简单的jquery操作表格方法
2014/12/15 Javascript
Jquery实现鼠标移动放大图片功能实例
2015/03/25 Javascript
详细解读AngularJS中的表单验证编程
2015/06/19 Javascript
jquery+正则实现统一的表单验证
2015/09/20 Javascript
基于jQuery实现文本框只能输入数字(小数、整数)
2016/01/14 Javascript
JavaScript原生对象常用方法总结(推荐)
2016/05/13 Javascript
Javascript中的神器——Promise
2017/02/08 Javascript
微信小程序实现给循环列表添加点击样式实例
2017/04/26 Javascript
vue-i18n结合Element-ui的配置方法
2019/05/20 Javascript
Element InfiniteScroll无限滚动的具体使用方法
2020/07/27 Javascript
在Python中使用itertools模块中的组合函数的教程
2015/04/13 Python
简析Python的闭包和装饰器
2016/02/26 Python
Python3使用turtle绘制超立方体图形示例
2018/06/19 Python
TensorFlow Session使用的两种方法小结
2018/07/30 Python
numpy.meshgrid()理解(小结)
2019/08/01 Python
numpy 声明空数组详解
2019/12/05 Python
Python+OpenCV图像处理——实现轮廓发现
2020/10/23 Python
HTML5 在canvas中绘制矩形附效果图
2014/06/23 HTML / CSS
牦牛毛户外探险服装:Kora
2019/02/08 全球购物
Kipling意大利官网:世界著名的时尚休闲包袋品牌
2019/06/05 全球购物
Monki官网:斯堪的纳维亚的独立时尚品牌
2020/11/09 全球购物
线程的基本概念、线程的基本状态以及状态之间的关系
2012/10/26 面试题
shell程序中如何注释
2012/01/28 面试题
会计电算化专业求职信
2014/06/10 职场文书
长城导游词
2015/01/30 职场文书
详解Java实践之建造者模式
2021/06/18 Java/Android
Pygame游戏开发之太空射击实战敌人精灵篇
2022/08/05 Python