一个简单的库,用于执行地理空间数据的快速邻近搜索。
geoindex的Python项目详细描述
一个简单的库,用于执行地理空间数据的快速邻近搜索。
要求
- python geohash
它是在Python2.7上编写和测试的。
安装
从pypi:
pip install geoindex
或github:
pip install -e git://github.com/gusdan/geoindex.git#egg=geoindex
简单使用
如果我们有100000个地理坐标,我们必须找到附近的位置 对于给定的点和半径,我们可以这样做:
from geoindex import GeoGridIndex, GeoPoint geo_index = GeoGridIndex() for _ in range(10000): lat = random.random()*180 - 90 lng = random.random()*360 - 180 index.add_point(GeoPoint(lat, lng)) center_point = GeoPoint(37.7772448, -122.3955118) for distance, point in index.get_nearest_points(center_point, 10, 'km'): print("We found {0} in {1} km".format(point, distance))
使用相关数据搜索
当我们填充索引时,我们可以将ref作为对某个对象的引用传递给geopoint 并在以下时间后使用:
from geoindex import GeoGridIndex, GeoPoint index = GeoGridIndex() for airport in get_all_airports(): index.add_point(GeoPoint(lat, lng, ref=airport)) center_point = GeoPoint(37.7772448, -122.3955118) for distance, point in index.get_nearest_points(center_point, 10, 'km'): print("We airport {0} in {1} km".format(point.ref, distance))
性能
创建包含10000个随机点的索引,并对每个点进行附近搜索 大约400毫秒。有关详细信息,请参阅tests/test_performance.py。
它是如何工作的
用于执行快速搜索geogridindex Geohash为每个点存储 在内部字典里。当我们初始化geogridindex时,我们将精度传递给 构造函数,基于它,我们用网格划分所有空间并存储每个点 在那个格子里。 当我们搜索最近的点时,我们用中心点和8个邻居定义单元 并检查这些单元格中的所有点到中心的距离。
每个单元格的大小取决于精度,如下所示可以找到网格的单元格大小 以及精确性。
Precision | Cell size |
---|---|
1 | 5000 |
2 | 1260 |
3 | 156 |
4 | 40 |
5 | 4.8 |
6 | 1.22 |
7 | 0.152 |
8 | 0.038 |
如果创建的geogridindex的精度为4,则表示所有点 将分配给单元格大小为40 km的网格。我们可以用 半径小于40/2km。如果我们想制造更大的半径,我们应该创造 精度较低的索引3。
但在另一方面,如果我们用最小的精度(1)创建索引并运行 用小半径(例如1公里)搜索(得到最近的点)它会起作用 很好,但它会检查9个5000公里的网格单元内的所有点 不能这么快。