地理索引:根据纬度/经度有效计算邻近度

发布于 2024-08-25 19:41:27 字数 223 浏览 1 评论 0原文

我的简单 Web 应用程序(WSGI、Python)支持文本查询以查找数据库中的项目。 现在我想扩展它以允许诸如“查找 {lat,long} 1 英里内的所有项目”之类的查询。

当然,如果考虑效率的话,这是一项复杂的工作,所以我正在考虑一个专用的外部模块来为地理坐标建立索引 - 有点像 Lucene 对文本的索引。

我假设这样的通用组件已经存在,但到目前为止还没有找到任何东西。任何帮助将不胜感激。

My simple web app (WSGI, Python) supports text queries to find items in the database.
Now I'd like to extend this to allow for queries like "find all items within 1 mile of {lat,long}".

Of course that's a complex job if efficiency is a concern, so I'm thinking of a dedicated external module that does indexing for geo-coordinates - sort of like Lucene would for text.

I assume a generic component like this already exists, but haven't been able to find anything so far. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-09-01 19:41:27

您检查过 mongo db 吗,它们有地理索引功能。 http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Have you checked out mongo db, they have a geo indexing feature. http://www.mongodb.org/display/DOCS/Geospatial+Indexing

南渊 2024-09-01 19:41:27

如果你打算直接用Python实现的话,我只能想到半暴力攻击,我已经出于类似的目的这样做了:

#!/usr/bin/python
from math import *
def distance(p1,p2):  # uses the haversine function and an ellipsoid model
    lat1, long1 = p1; lat2, long2 = p2
    lat1=radians(lat1); long1=radians(long1); lat2=radians(lat2); long2=radians(long2)
    maior=6378.137; menor=6356.7523142
    R=(maior*menor)/sqrt((maior*cos(lat1))**2 + (menor*sin(lat1))**2)
    d_lat = lat2 - lat1; d_long = long2 - long1
    a = sin(d_lat/2)**2 + cos(lat1) * cos(lat2) * sin(d_long/2)**2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    length = R * c
    x = sin(d_long) * cos(lat2)
    y = cos(lat2) * sin(lat1) - sin(lat2) * cos (lat1) * cos(d_long)
    bearing = 90-(degrees(atan2(y, -x)))
    return length, bearing

对于距离点的筛选,你可以首先找到“x”和“的候选点” y" 坐标位于以测试位置为中心的正方形内(速度更快),然后测试实际的测地距离。

希望有帮助!

I could only think of a semi-brute-force attack if you plan to implement it directly with Python, which I already did with similar purposes:

#!/usr/bin/python
from math import *
def distance(p1,p2):  # uses the haversine function and an ellipsoid model
    lat1, long1 = p1; lat2, long2 = p2
    lat1=radians(lat1); long1=radians(long1); lat2=radians(lat2); long2=radians(long2)
    maior=6378.137; menor=6356.7523142
    R=(maior*menor)/sqrt((maior*cos(lat1))**2 + (menor*sin(lat1))**2)
    d_lat = lat2 - lat1; d_long = long2 - long1
    a = sin(d_lat/2)**2 + cos(lat1) * cos(lat2) * sin(d_long/2)**2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    length = R * c
    x = sin(d_long) * cos(lat2)
    y = cos(lat2) * sin(lat1) - sin(lat2) * cos (lat1) * cos(d_long)
    bearing = 90-(degrees(atan2(y, -x)))
    return length, bearing

For the screening of points for distance, you can first find candidate points whose "x" and "y" coordinates are inside a square centered on your testing position (much faster) and just then test for actual geodesic distance.

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文