Python 如何检查特定区域内的坐标
假设我有两种坐标,第一种叫做Python 如何检查特定区域内的坐标,python,location,coordinates,latitude-longitude,area,Python,Location,Coordinates,Latitude Longitude,Area,假设我有两种坐标,第一种叫做center\u point,第二种叫做test\u point。我想通过应用半径阈值,知道测试点坐标是否在中心点坐标附近。如果我写它,它就像: center_point = [{'lat': -7.7940023, 'lng': 110.3656535}] test_point = [{'lat': -7.79457, 'lng': 110.36563}] radius = 5 # in kilometer 在Python中,如何检查测试点是在中心点的半径内还是
center\u point
,第二种叫做test\u point
。我想通过应用半径
阈值,知道测试点
坐标是否在中心点
坐标附近。如果我写它,它就像:
center_point = [{'lat': -7.7940023, 'lng': 110.3656535}]
test_point = [{'lat': -7.79457, 'lng': 110.36563}]
radius = 5 # in kilometer
在Python中,如何检查测试点是在中心点的半径内还是半径外?我如何在Python中执行这种任务
预期结果将显示测试点
从中心点
坐标在半径之内或之外。从数学导入sqrt
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")
a=中心点[0]['lat']-测试点[0]['lat']
b=中心点[0]['lng']-测试点[0]['lng']
c=sqrt(a*a+b*b)
如果(c<半径):
打印(“内部”)
其他:
打印(“外部”)
根据@user1753919在其评论中的推荐,我在这里得到了答案:
最终代码:
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')
从数学导入弧度、cos、sin、asin、sqrt
def haversine(lon1、lat1、lon2、lat2):
"""
计算两点之间的大圆距离
地球上(以十进制度数表示)
"""
#将十进制度数转换为弧度
lon1,lat1,lon2,lat2=贴图(弧度,[lon1,lat1,lon2,lat2])
#哈弗森公式
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#地球半径,单位为公里。使用3956英里
返回c*r
中心点=[{'lat':-7.7940023,'lng':110.3656535}]
测试点=[{'lat':-7.79457,'lng':110.36563}]
lat1=中心点[0]['lat']
lon1=中心点[0]['lng']
lat2=测试点[0]['lat']
lon2=测试点[0]['lng']
半径=1.00#以公里为单位
a=哈弗新(lon1、lat1、lon2、lat2)
打印('距离(公里):',a)
如果a可以优雅地处理它:
from geopy import distance
center_point = [{'lat': -7.7940023, 'lng': 110.3656535}]
test_point = [{'lat': -7.79457, 'lng': 110.36563}]
radius = 5 # in kilometer
center_point_tuple = tuple(center_point[0].values()) # (-7.7940023, 110.3656535)
test_point_tuple = tuple(test_point[0].values()) # (-7.79457, 110.36563)
dis = distance.distance(center_point_tuple, test_point_tuple).km
print("Distance: {}".format(dis)) # Distance: 0.0628380925748918
if dis <= radius:
print("{} point is inside the {} km radius from {} coordinate".format(test_point_tuple, radius, center_point_tuple))
else:
print("{} point is outside the {} km radius from {} coordinate".format(test_point_tuple, radius, center_point_tuple))
使用haversine公式计算距离,看看它是否小于rhi@user1753919谢谢,它可以工作。嗨,aa
和bb
内部c=math.sqrt(aa+bb)
参考?抱歉,格式错误。我对其进行了编辑以使其更清晰。如何在整个数据帧上以更少的计算成本对其进行测试?这应该通过KDTree或kNearestNeighbors算法来更有效地完成,但我还没有找到任何实际使用lat-lon点的示例。感谢此解决方案!非常适合我们的项目:)
dis = distance.great_circle(center_point_tuple, test_point_tuple).km
print("Distance: {}".format(dis)) # Distance: 0.0631785164583489