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.331032)을 종점 으로 바 이 두 지도,구 글 지도 등 을 결합 해 효과 검증 을 실시 했다.
프로그램 실행 결 과 는 다음 과 같 습 니 다.

바 이 두 측정 거 리 는 38.3km 이다.
Google 지도 수 동 거리 39.31km

 
거리 와 각도 모두 문제 가 없다. 
파 이 썬 경위도 좌표 가 거리 와 각도 로 전환 되 는 실현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 파 이 썬 경위도 좌표 가 거리 와 각도 로 전환 되 는 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기