2017-03-09 21 views
0

私は2種類の座標を持っており、最初にcenter_pointと呼ばれ、2番目にtest_pointと呼ばれています。私は、test_pointの座標がの座標に近いかどうかを調べるには、radiusのしきい値を適用して座標を知りたいと思っています。私はそれを書く場合は、そのような:特定の領域内の座標をチェックする方法

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

radius = 5 # in kilometer 

どのようにチェックするために内部またはPythonでcenter_pointから半径外test_point場合は?このような種類のタスクをPythonでどのように実行するのですか?

結果は、の内か外のtest_pointが、center_pointの座標になると予想されます。彼/彼女のコメントの@ user1753919の勧告から

+0

計算距離使って半正矢式のhttp: //stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-pointsとrより小さいかどうかを確認 – plasmon360

+0

hi @ user1753919 tha 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

関連する問題