Python GeoModel 替代方案

6 投票
3 回答
1382 浏览
提问于 2025-04-16 05:53

我在找一个可以替代应用引擎数据存储的库,想要能进行最近邻或盒子型的地理查询。目前我在用GeoModel 0.2,但运行得很慢,有时候超过1.5秒。有没有人能给我推荐一下?

谢谢!

3 个回答

2

我不能给你推荐一个性能更好的现成库,但我记得GeoModel是开源的,代码也不难理解。我们发现通过调整代码来适应我们的需求,可以提高一些速度。

比如说,如果你不需要找最近的几个结果,只是想要在特定的边界框或半径内得到X个结果,那么你可能可以提高GeoModel的速度。因为GeoModel现在需要获取所有在合适的地理哈希中的记录,然后在内存中排序找出最近的。(具体的实现细节留给你自己去研究。)

你也可以考虑调整你使用的地理哈希的层级。如果你的数据很密集,并且查询的区域很小,保持16层的地理哈希而不是8层或12层,可能会显著提高性能。

(我现在没有查看GeoModel的源代码,只是回忆几个月前使用时的情况,所以这部分内容仅供参考,建议你自己去看看源代码。)

4

与其使用geomodel 0.2.0版本,不如使用withasync这个分支(可以查看这里:

http://code.google.com/p/geomodel/source/browse/#svn/branches/withasync)。这样你就可以使用asynctools来并行运行查询,这样对于很多查询来说会快很多。

确保你的应用程序或python路径中也有asynctools。

6

我遇到了和geomodel一样的问题。为了解决这个问题,我使用了4的分辨率,并且用Python进行了排序和过滤。

SEARCHED_LOCATION = db.GeoPt("48.8566667, 2.3509871") # Location of Paris.
DISTANCE = 50000 #Between 10000 and 150000.
MAX_RESULTS = 300

# Resolution '4' is about 150 kilometers i suppose it's a good compromise.                                                                                                                            
bbox = geocell.compute_box(geocell.compute(SEARCHED_LOCATION, resolution=4))
cell = geocell.best_bbox_search_cells(bbox, geomodel.default_cost_function)

query.filter('location_geocells IN', cell)

# Python filters
def _func(x):
  """Private method used to set the distance of the model to the searched location
  and return this distance.
  """
  x.dist = geomath.distance(SEARCHED_LOCATION, x.location)
  return x.dist

results = sorted(query.fetch(MAX_RESULTS), key=_func) # Order the result by distance
results = [x for x in results if x.dist <= DISTANCE]  # Filter the result

撰写回答