如何在python中找到距给定lat和long 500米范围内的用户位置

如何在python中找到距给定lat和long 500米范围内的用户位置,python,geocoding,Python,Geocoding,我想在Python中找到距离给定lat和long 500米以内的用户位置 给定纵横比=19.114315,72.911174 我想检查新的lat和long是否在给定lat和long的500米范围内 新纵横比=19.112398,72.912743 我在python中使用这个公式 math.acos(math.sin(19.114315) * math.sin(19.112398) + math.cos(19.114315) * math.cos(19.112398) * math.cos(72.

我想在Python中找到距离给定lat和long 500米以内的用户位置

给定纵横比=19.114315,72.911174

我想检查新的lat和long是否在给定lat和long的500米范围内

新纵横比=19.112398,72.912743

我在python中使用这个公式

math.acos(math.sin(19.114315) * math.sin(19.112398) + math.cos(19.114315) * math.cos(19.112398) * math.cos(72.912743 - (72.911174))) * 6371 <= 0.500 

math.acos(math.sin(19.114315)*math.sin(19.112398)+math.cos(19.114315)*math.cos(19.112398)*math.cos(72.912743-(72.911174))*6371提示:编写可重用代码,并清理您的数学。
似乎你有个错误,就在公式的末尾

math.cos(longRad-(longRad))
==math.cos(0)==1

我不擅长地理方面的东西,所以我不会纠正它

import math

def inRangeRad(latRad, longRad):
    sinLatSqrd = math.sin(latRad) * math.sin(latRad)
    cosLatSqrd = math.cos(latRad) * math.cos(latRad)
    inner = sinLatSqrd +  cosLatSqrd * math.cos(longRad - (longRad))
    return math.acos( inner ) * 6371 <= 0.500

def inRangeDeg(latDeg, longDeg):
    latRad = 0.0174532925 * latDeg
    longRad = 0.0174532925 * longDeg
    return inRangeRad(latRad, longRad)

print "test"
print "19.114315, 72.911174"
print inRangeDeg(19.114315, 72.911174)
导入数学
范围内的def(latRad、longRad):
sinLatSqrd=math.sin(latRad)*math.sin(latRad)
cosLatSqrd=math.cos(latRad)*math.cos(latRad)
内部=sinLatSqrd+cosLatSqrd*math.cos(longRad-(longRad))

return math.acos(内部)*6371对此要非常小心!对于使用GPS坐标的距离,不能仅使用
cos
sin
,因为距离将不正确

对于平面几何体(适用于地球表面的小区域) 这两个问题的解都归结为简单的三角函数。 在球体上,解决方案要复杂得多,例如 反问题是两端的方位角不同 连接大圆、圆弧的点,即测地线


仔细研究这些计算,你真的不想自己去实现。

你可以使用哈弗公式来获得两点之间的大圆距离(沿球体)。将地球视为一个球体进行远距离治疗会有一些问题,但在500米范围内,你可能会没事(假设你没有试图将医疗包扔到船上或其他东西上)

如果两点之间的距离小于阈值,则在以下范围内:

points_1 = (19.114315,72.911174)
points_2 = (19.112398,72.912743)
threshold_km = 0.5


distance_km = haversine(points_1[0], points_1[1], points_2[0], points_2[1])
if distance_km < threshold_km:
    print('within range')
else:
    print('outside range')
points_1=(19.114315,72.911174)
点_2=(19.112398,72.912743)
阈值_km=0.5
距离\u km=haversine(点\u 1[0]、点\u 1[1]、点\u 2[0]、点\u 2[1])
如果距离小于阈值公里:
打印('范围内')
其他:
打印('超出范围')

Eh,
math.sin()
以及
math.cos()
想要弧度,而不是度。我可以在python中将其转换为弧度吗?是的,可以在python中进行乘法。如果你用度乘以一个数字,用~
0.0174532925
得到的是弧度。。(2*PI/360)@user2927983请参阅已接受的答案。基本上,使用
math.radians(degrees)
。这不是一个真正的答案,但我发现使用python
haversine
模块非常简单和准确:
从haversine导入haversine;距离公里=哈弗斯线((纬度a,液化天然气纬度a),(纬度b,液化天然气纬度b))
<代码>pip安装haversine
一如既往;)对于500米,他/她会没事的。@mirosval我不想要精确的距离,有误差的近似距离对我来说是可以的。也许吧,但这种有缺陷的逻辑仍然会导致问题。明天我将需要计算更大的距离,我将重新使用这个解决方案,因为我已经忘记了这一切…@mirosval那么正确的方法是什么?使用一个已经解决了这个问题并经过良好测试的库。我在答案中链接的GeoPy库在PyPI上标记为Production/Stable。通过重新发明轮子并自己编写,它可以避免任何数字错误,此外,它还实现了一种计算地球距离的方法。
points_1 = (19.114315,72.911174)
points_2 = (19.112398,72.912743)
threshold_km = 0.5


distance_km = haversine(points_1[0], points_1[1], points_2[0], points_2[1])
if distance_km < threshold_km:
    print('within range')
else:
    print('outside range')