2017-03-09 5 views
0

좌표계가 두 가지 있다고 가정 해 봅시다. 처음에는 center_point이고 두 번째 좌표는 test_point입니다. 나는 test_point 좌표가 좌표에 가깝거나 없는지 알고 싶습니다. radius 임계 값을 적용하여 조정합니다. 나는 그것을 작성하는 경우, 그와 같은 :특정 영역 안의 좌표를 확인하는 방법 Python

center_point = [{'lat': -7.7940023, 'lng': 110.3656535}] 
test_point = [{'lat': -7.79457, 'lng': 110.36563}] 

radius = 5 # in kilometer 

확인하는 방법 내부 또는 파이썬에서 center_point에서 반경 외부 test_point 경우? 어떻게 이런 종류의 작업을 파이썬에서 수행 할 수 있습니까?

예상 결과는 에서 center_point 좌표 내외의 test_point 내부 또는 외부라고합니다. 그/그녀의 코멘트에서 @의 user1753919의 추천에서

+0

계산 거리를 사용하여 하버 사인 공식에 http : //stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points 및 r 미만인지 확인 – plasmon360

+0

안녕하세요 @ user1753919 그쪽으로 nks, 작동합니다. – ytomo

답변

6

, 나는 여기에 대답을 얻었다 : Haversine Formula in Python (Bearing and Distance between two GPS points)

최종 코드 :

from math import radians, cos, sin, asin, sqrt 

def haversine(lon1, lat1, lon2, lat2): 
    """ 
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees) 
    """ 
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) 

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles 
    return c * r 

center_point = [{'lat': -7.7940023, 'lng': 110.3656535}] 
test_point = [{'lat': -7.79457, 'lng': 110.36563}] 

lat1 = center_point[0]['lat'] 
lon1 = center_point[0]['lng'] 
lat2 = test_point[0]['lat'] 
lon2 = test_point[0]['lng'] 

radius = 1.00 # in kilometer 

a = haversine(lon1, lat1, lon2, lat2) 

print('Distance (km) : ', a) 
if a <= radius: 
    print('Inside the area') 
else: 
    print('Outside the area') 

감사

1
from math import sqrt 
a = center_point[0]['lat'] - test_point[0]['lat'] 
b = center_point[0]['lng'] - test_point[0]['lng'] 
c = sqrt(a * a + b * b) 
if (c < radius): 
     print("inside") 
else: 
     print("outside") 
+0

안녕하세요,'c = math.sqrt (aa + bb)'내부의'aa'와'bb'는 무엇입니까? – ytomo

+0

죄송합니다. 서식이 잘못되었습니다. 나는 그것을 더 명확하게 편집했다. – Egor

관련 문제