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

发布时间:2020-11-02 15:09:38 作者:Leah
来源:亿速云 阅读:692

今天就跟大家聊聊有关Python实现经纬度坐标转换为距离及角度,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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实现经纬度坐标转换为距离及角度

看完上述内容,你们对Python实现经纬度坐标转换为距离及角度有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. MySQL中geometry类型如何处理经纬度距离
  2. js如何根据百度地图提供经纬度计算两点距离

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 经纬

上一篇:Compare And Swap的底层原理

下一篇:java反射机制指的是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》