mapping.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:show-me-earthquakes 作者: compciv 项目源码 文件源码
def haversine(pt_a, pt_b):
    """
    `pt_a` and `pt_b` are tuples with two float numbers each, i.e.
    representing lng/lat pairs:
    (99,100)  (-42, 85)

    The geo distance between the two points is returned in kilometers
    """
    from math import radians, cos, sin, asin, sqrt

    lng_a = radians(float(pt_a[0]))
    lat_a = radians(float(pt_a[1]))

    lng_b = radians(float(pt_b[0]))
    lat_b = radians(float(pt_b[1]))
    # haversine formula
    dlng = lng_b - lng_a
    dlat = lat_b - lat_a
    whatevs = sin(dlat /2 ) ** 2 + cos(lat_a) * cos(lat_b) * sin(dlng / 2) ** 2
    c = 2 * asin(sqrt(whatevs))
    r = 6371 # Radius of earth in kilometers.
    # return the final calculation
    return c * r
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号