如何检查某些区域内是否有Python坐标

假设我有两种坐标,第一种称为center_point,第二种称为test_point.我想通过应用半径阈值来知道test_point坐标是否在靠近或不在center_point坐标内.如果我写它,它就像:

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在他/她的评论中的推荐,我得到了答案: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')

谢谢

上一篇:从行中提取坐标数组(C OpenCV)


下一篇:Android将阿拉伯数字转换为英文数字