我正在为应用引擎数据存储寻找一个替代库,它将执行最近 n 或盒装地理查询,目前我使用的是 GeoModel 0.2,它运行速度很慢(在某些情况下 > 1.5s)。有没有人有什么建议?
谢谢!
我正在为应用引擎数据存储寻找一个替代库,它将执行最近 n 或盒装地理查询,目前我使用的是 GeoModel 0.2,它运行速度很慢(在某些情况下 > 1.5s)。有没有人有什么建议?
谢谢!
我对地理模型有同样的问题。为了更正它,我使用 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
不要使用 geomodel 0.2.0 版本,而是使用 withasync 分支(参见
http://code.google.com/p/geomodel/source/browse/#svn/branches/withasync)。这将允许您使用 asynctools 并行运行查询,这对于许多查询来说将明显更快。
确保你的 app/pythonpath 中也有 asynctools。
我无法为您指出性能更好的现有库,但我记得,GeoModel 是开源的,代码不难理解。我们发现我们可以通过调整代码以适应我们的场景来提高速度。
例如,如果您不需要最近的 n,您只需要来自特定边界框或半径内的 X 个结果,您可能可以提高 GeoModel 的速度,因为 GeoModel 当前必须在适当的 geohash 中获取每条记录,然后为记忆中最接近的。(该实现的细节留给读者作为练习。)
您还可以考虑调整您正在使用的 geohash 级别。如果您有大量密集数据并在小范围内进行查询,则可以通过保持 16 级而不是 8 或 12 级来显着提高性能。
(我现在不是在看 GeoModel 源代码,而是回想起几个月前我上次使用它的时间,所以请谨慎对待它并自己深入研究源代码。)