Python 地理编码按距离过滤

1 投票
4 回答
5716 浏览
提问于 2025-04-16 00:51

我需要筛选一些地理坐标,看看哪些离我现在的位置近。比如,我想从一堆餐馆的地理坐标中找出那些距离我当前位置10英里以内的餐馆。

有没有人能告诉我一个函数,可以把距离转换成纬度和经度的变化量?比如:

class GeoCode(object):
   """Simple class to store geocode as lat, lng attributes."""
   def __init__(self, lat=0, lng=0, tag=None):
      self.lat = lat
      self.lng = lng
      self.tag = None

def distance_to_deltas(geocode, max_distance):
   """Given a geocode and a distance, provides dlat, dlng
      such that

         |geocode.lat - dlat| <= max_distance
         |geocode.lng - dlng| <= max_distance
   """
   # implementation
   # uses inverse Haversine, or other function?
   return dlat, dlng

注意:我使用的是最上界范数来计算距离。

4 个回答

0

如果你把数据存储在MongoDB里,它会帮你很方便地进行地理位置搜索,而且比上面提到的纯Python解决方案要好,因为它会为你处理优化问题。

http://www.mongodb.org/display/DOCS/Geospatial+Indexing

1

这是使用哈弗辛公式计算经纬度之间距离的方法:

import math 

R = 6371 # km
dLat = (lat2-lat1) # Make sure it's in radians, not degrees
dLon = (lon2-lon1) # Idem 
a = math.sin(dLat/2) * math.sin(dLat/2) +
    math.cos(lat1) * math.cos(lat2) * 
    math.sin(dLon/2) * math.sin(dLon/2) 
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) 
d = R * c;

现在,测试“d”(同样是以公里为单位)是否超过你的阈值就变得很简单。如果你想要其他单位的距离,只需调整半径即可。

抱歉我不能给你一个现成的解决方案,但我不太理解你的代码框架(见评论)。

另外,现在你可能更想使用球面余弦定律,而不是哈弗辛公式。因为在数值稳定性方面的优势已经不再值得,而且球面余弦定律更简单易懂、编写和使用。

6

看起来之前没有一个好的Python实现。不过幸运的是,StackOverflow的“相关文章”侧边栏可以帮到我们。这篇SO文章提到了一篇很棒的文章,里面有数学公式和Java的实现。你需要的那个函数其实很短,已经嵌入在我下面的Python代码中。经过测试,效果如文中所示。请注意评论中的警告。

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

Earth_radius_km = 6371.0
RADIUS = Earth_radius_km

def haversine(angle_radians):
    return sin(angle_radians / 2.0) ** 2

def inverse_haversine(h):
    return 2 * asin(sqrt(h)) # radians

def 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

撰写回答