gpt4 book ai didi

python - 如何检查特定区域内Python的坐标

转载 作者:太空狗 更新时间:2023-10-30 00:22:44 26 4
gpt4 key购买 nike

假设我有两种坐标,第一种称为 center_point,第二种称为 test_point。我想通过应用 radius 阈值知道 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 中检查 test_point 是在 center_point 半径内还是半径外?我如何在 Python 中执行此类任务?

预期结果将说明 test_pointcenter_point 坐标的 radius 之内或之外。

最佳答案

根据@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')

谢谢

关于python - 如何检查特定区域内Python的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com