【问题标题】:how to find user location within 500 meters from given lat and long in python如何在python中从给定的经纬度500米内找到用户位置
【发布时间】:2015-12-14 08:54:12
【问题描述】:

我想在 Python 中找到距给定纬度和经度 500 米范围内的用户位置。

给定纬度和经度 = 19.114315,72.911174

我想检查新的经纬度是否在给定经纬度 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.sin()math.cos() 想要 弧度,而不是
  • 可以在python中转成弧度吗?
  • 是的,你可以在 python 中进行乘法运算。如果你将一个数乘以度数,用 ~0.0174532925 得到它的弧度.. (2*PI / 360)
  • @user2927983 查看this question 接受的答案。基本上,使用math.radians(degrees)
  • 不是一个真正的答案,但我发现使用 python haversine 模块非常简单和准确:from haversine import haversine; distance_km = haversine((lat_a, lng_a), (lat_b, lng_b)) .. pip install haversine 一如既往;)

标签: python geocoding


【解决方案1】:

您可以使用Haversine 公式来获得两点之间的大圆距离(沿球体)。远距离将地球视为一个球体存在一些问题,但对于 500 米,您可能没问题(假设您不是试图将医疗包裹丢在船上或其他东西上)。

from math import radians, sin, cos, asin, sqrt

def haversine(lat1, long1, lat2, long2, EARTH_RADIUS_KM=6372.8):

    # get distance between the points
    phi_Lat = radians(lat2 - lat1)
    phi_Long = radians(long2 - long1)

    lat1 = radians(lat1)
    lat2 = radians(lat2)

    a = sin(phi_Lat/2)**2 + \
        cos(lat1) * cos(lat2) * \
        sin(phi_Long/2)**2

    c = 2 * asin(sqrt(a))
    return EARTH_RADIUS_KM * c

如果两点之间的距离小于你的阈值,它在范围内:

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')

【讨论】:

    【解决方案2】:

    提示:编写可重用的代码,并清理你的数学。 看来你有一个错误,就在该公式的末尾附近..

    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)
    

    【讨论】:

      【解决方案3】:

      对此要非常小心!您不能只使用cossin 来获取使用 GPS 坐标的距离,因为距离会不正确!

      https://en.wikipedia.org/wiki/Geodesy#Geodetic_problems

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

      查看GeoPy 进行这些计算,您真的不想自己实现。

      【讨论】:

      • 500米,他/她会没事的。
      • @mirosval 我不想要精确的范围,有一些错误的近似范围对我来说很好。
      • 也许吧,但是这种有缺陷的逻辑仍然会导致问题发生。明天我需要计算更大的距离,我将重用这个解决方案,忘记了这一切......
      • @mirosval 那么正确的方法是什么?
      • 使用已经解决了这个问题并且经过良好测试的库。我在答案中链接的 GeoPy 库在 PyPI 上标记为 Production/Stable。通过重新发明轮子并自己编写它,它可以避免您可能犯的任何数字错误,此外它还实现了计算地球距离的more accurate method
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 2018-01-08
      • 2018-10-26
      • 1970-01-01
      • 2011-09-09
      • 2014-05-31
      相关资源
      最近更新 更多