Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn)
最近項(xiàng)目上有這樣的需求,需要依據(jù)設(shè)備的經(jīng)緯度坐標(biāo)計(jì)算距離及角度。經(jīng)驗(yàn)證后效果較好,并分享。
1 經(jīng)緯度轉(zhuǎn)換距離代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import math
# 計(jì)算距離
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 經(jīng)緯度轉(zhuǎn)化角度代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import math
# 計(jì)算角度
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 驗(yàn)證
選取深圳野生動(dòng)物園(22.599578, 113.973129)為起點(diǎn),深圳坪山站(22.6986848, 114.3311032)為終點(diǎn),結(jié)合百度地圖、谷歌地圖等進(jìn)行效果驗(yàn)證。
程序運(yùn)行結(jié)果如下:

百度測(cè)距為38.3km
Google地圖手動(dòng)測(cè)距為39.31km

距離與角度均無(wú)問(wèn)題。
到此這篇關(guān)于Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)WGS84火星百度及web墨卡托四種坐標(biāo)系相互轉(zhuǎn)換
- Python實(shí)現(xiàn)常見(jiàn)坐標(biāo)系的相互轉(zhuǎn)換
- Python實(shí)現(xiàn)常見(jiàn)的4種坐標(biāo)互相轉(zhuǎn)換
- 使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)
- 解決python gdal投影坐標(biāo)系轉(zhuǎn)換的問(wèn)題
- 代碼分析Python地圖坐標(biāo)轉(zhuǎn)換
- python實(shí)現(xiàn)無(wú)人機(jī)航拍圖片像素坐標(biāo)轉(zhuǎn)世界坐標(biāo)的示例代碼
相關(guān)文章
python實(shí)現(xiàn)接口并發(fā)測(cè)試腳本
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)接口并發(fā)測(cè)試腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Python之日期與時(shí)間處理模塊(date和datetime)
這篇文章主要介紹了Python之日期與時(shí)間處理模塊(date和datetime),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Python圖像處理之Hough變換檢測(cè)直線(xiàn)
霍夫變換是一種特征檢測(cè)(feature?extraction),被廣泛應(yīng)用在圖像分析,本文將利用Hough變換實(shí)現(xiàn)直線(xiàn)檢測(cè),感興趣的小伙伴可以了解一下2023-07-07
Python中數(shù)據(jù)類(lèi)轉(zhuǎn)換為JSON的方法詳解
這篇文章主要介紹了Python中數(shù)據(jù)類(lèi)轉(zhuǎn)換為JSON的方法詳解的相關(guān)資料,需要的朋友可以參考下2023-09-09
python實(shí)現(xiàn)廣度優(yōu)先搜索過(guò)程解析
這篇文章主要介紹了python實(shí)現(xiàn)廣度優(yōu)先搜索過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
python中sys.argv參數(shù)用法實(shí)例分析
Python 支持向量機(jī)分類(lèi)器的實(shí)現(xiàn)

