Python經緯度座標轉換為距離及角度的實現

2022-10-06 04:12:09 字數 2365 閱讀 1669

最近專案上有這樣的需求,需要依據裝置的經緯度座標計算距離及角度。經驗證後效果較好,並分享。

1 經緯度轉換距離**

#!/usr/bin/env python

# -*- coding: utf-8 -*-

__author__ = 'seven'

import math

# 計算www.cppcns.com距離

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.radjpkerohizuians(lonb)

pa = math.atan(rb / ra * math.tan(radlata))

pb = math.atan(rb / ra * math.tan(radlatb))

x = mjpkerohizuath.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 - jpkerohizuc2)

distance = ra * (x + dr)

distance = round(distance / 1000, 4)

return f'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 - www.cppcns.comradlona

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'北偏東'

elif 90 < brng < 180:

return f'東偏南'

elif 180 < brng < 270:

return f'西偏南'

elif 270 < brng < 360:

return f'北偏西'

else:

pass

3 驗證

選取深圳野生動物園(22.599578, 113.973129)為起點,深圳坪山站(22.6986848, 114.3311032)為終點,結合百度地圖、谷歌地圖等進行效果驗證。

程式執行結果如下:

百度測距為38.3km

google地圖手動測距為39.31km

距離與角度均無問題。 

經緯度座標轉換

經緯度與xy座標轉換工具類 public class latlngxyzconverthelper xy轉經緯度 需要轉換的x座標 需要轉換的y座標 地圖級別 轉換後的經度 轉換後的緯度 public static void tiletolatlng double tile x,double til...

經緯度座標系轉換為UTM座標系(matlab)

如題所說,直接上程式。驗證自己做一下,結果應該是對的。誠不我欺。注意 程式名與函式名保持一致!地理經緯度座標轉換為utm座標 size shuzu size lat shuru for i 1 size shuzu 2 輸入經緯度 lat 29.819206 lon 116.133243 lat l...

經緯度座標系轉東北天 經緯度座標系轉換

網際網路地圖的座標系現狀 地球座標 wgs84 國際標準,從 gps 裝置中取出的資料的座標系 國際地圖提供商使用的座標系 火星座標 gcj 02 也叫國測局座標系 中國標準,從國行移動裝置中定位獲取的座標資料使用這個座標系 國家規定 國內出版的各種地圖系統 包括電子形式 必須至少採用gcj 02對...