2017-03-09 117 views
0

可以說我有2種座標,第一個叫center_point,第二個叫test_point。我想知道如果test_point座標是靠近或不是到center_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在Python半徑外的test_point?我如何在Python中執行這種類型的任務?

預計結果會說test_point內部或外部的radiuscenter_point之間的座標。

+0

計算距離使用haversine公式HTTP: //stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points並查看它是否小於r – plasmon360

+0

hi @ user1753919 tha它工作。 – ytomo

回答

6

在他/她的評論@ user1753919的推薦下,我來到這裏的答案: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