gpt4 book ai didi

python - 是否可以 pickle Rtree 空间索引?

转载 作者:太空宇宙 更新时间:2023-11-03 12:01:38 25 4
gpt4 key购买 nike

Pickling Rtree 看起来并不简单,因为它是一个 ctypes 包装器。 This comment在 SO 秒假设。

但是,在(很多)旧的@sgillies post (这个库的作者),在评论部分,他建议这确实是可行的。

然而,当我在本地重新创建这些步骤时,结果表明并非如此:

>>> idx = rtree.index.Index()
>>> idx.insert(10, (1,2,3,4))
>>> list(idx.intersection((0,0,5,5)))
# [10]
>>> f = open('foo.p', 'wb')
>>> pickle.dump(idx, f)
>>> a = pickle.load( open( "foo.p", "rb" ) )
>>> a.get_bounds()
# [1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308]]
>>> list(a.intersection((0,0,5,5)))
# []

问题:是否存在我未能正确执行以启用空间索引 pickle 的操作?如果可以进行空间索引,那么正确的方法是什么?

有趣的是,我能够通过 pickle 过程( pickle 由 Dask.distributed 执行)成功传递 GeoPandas.GeoDataFrame.sindex。我知道它使用 cloudpickle 或 pickle(视情况而定),但从 GeoPandas 方面来看,sindex's SpatialIndex class似乎只是 rtree.index.Index 的包装器。我还没有更深入地了解为什么会这样,但想先在这里检查一下,看看其他人是否有洞察力。

最佳答案

看起来您可以将索引写入文件,因此应该可以将其存储为字符串:https://gis.stackexchange.com/questions/254781/saving-python-rtree-spatial-index-to-file/254785

您也可以试试 Dill,我相信它能够“pickle ”更多种类的对象,或者至少可以保存解释器的状态:https://pypi.python.org/pypi/dill

关于python - 是否可以 pickle Rtree 空间索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46457640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com