栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python Geocode按距离过滤

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Geocode按距离过滤

似乎还没有一个好的Python实现。幸运的是,SO“相关文章”侧边栏是我们的朋友。这篇SO文章指向了一篇出色的文章,提供了数学和Java实现。您所需的实际功能相当简短,并嵌入下面的Python代码中。测试到显示的程度。阅读评论中的警告。

from math import sin, cos, asin, sqrt, degrees, radiansEarth_radius_km = 6371.0RADIUS = Earth_radius_kmdef haversine(angle_radians):    return sin(angle_radians / 2.0) ** 2def inverse_haversine(h):    return 2 * asin(sqrt(h)) # radiansdef distance_between_points(lat1, lon1, lat2, lon2):    # all args are in degrees    # WARNING: loss of absolute precision when points are near-antipodal    lat1 = radians(lat1)    lat2 = radians(lat2)    dlat = lat2 - lat1    dlon = radians(lon2 - lon1)    h = haversine(dlat) + cos(lat1) * cos(lat2) * haversine(dlon)    return RADIUS * inverse_haversine(h)def bounding_box(lat, lon, distance):    # Input and output lats/longs are in degrees.    # Distance arg must be in same units as RADIUS.    # Returns (dlat, dlon) such that    # no points outside lat +/- dlat or outside lon +/- dlon    # are <= "distance" from the (lat, lon) point.    # Derived from: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates    # WARNING: problems if North/South Pole is in circle of interest    # WARNING: problems if longitude meridian +/-180 degrees intersects circle of interest    # See quoted article for how to detect and overcome the above problems.    # Note: the result is independent of the longitude of the central point, so the    # "lon" arg is not used.    dlat = distance / RADIUS    dlon = asin(sin(dlat) / cos(radians(lat)))    return degrees(dlat), degrees(dlon)if __name__ == "__main__":    # Examples from Jan Matuschek's article    def test(lat, lon, dist):        print "test bounding box", lat, lon, dist        dlat, dlon = bounding_box(lat, lon, dist)        print "dlat, dlon degrees", dlat, dlon        print "lat min/max rads", map(radians, (lat - dlat, lat + dlat))        print "lon min/max rads", map(radians, (lon - dlon, lon + dlon))    print "liberty to eiffel"    print distance_between_points(40.6892, -74.0444, 48.8583, 2.2945) # about 5837 km    print    print "calc min/max lat/lon"    degs = map(degrees, (1.3963, -0.6981))    test(*degs, dist=1000)    print    degs = map(degrees, (1.3963, -0.6981, 1.4618, -1.6021))    print degs, "distance", distance_between_points(*degs) # 872 km


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659938.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号