python - 序列化和反序列化以及 Python RTree

标签 python serialization r-tree

我一直在尝试调试使用 Pythons RTree version 0.8.2 的软件包中的一些奇怪行为.

为了跟踪问题,我需要每隔几分钟序列化一次 RTree 实例,当问题发生时我可以得到一个非常准确的 RTree 快照。

我正在为转储使用以下代码:

def _dump_tree(self, filename, tree):
    try:
        dump_file = Rtree(filename)
        for item in tree.intersection(tree.bounds, objects=True):
            dump_file.insert(item.id, item.bbox, item.object)
    except RTreeError:
        pass

此函数获取 RTree 并将其复制到具有给定文件名的新 RTree。

每次调用都会创建以下对:

2015-10-01---14-21-16_items.dat
2015-10-01---14-21-16_items.idx
...

如何将 dat/idx 对反序列化回 Python 对象?

最佳答案

只需从相同的文件名打开 RTree:

>>> from rtree import index
>>> idx = index.Index('2015-10-01---14-21-16_items')
>>> idx.count(idx.bounds)
42 # The number of items you had in the original tree

关于python - 序列化和反序列化以及 Python RTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890116/

相关文章:

python - xmpp 机器人接受新 friend 吗?

.net - 在.NET中序列化许多对象时出现SerializationException

R-Tree vs R+-Tree vs R*-Tree

.net - 从字符串构造 C# Type 以进行序列化/反序列化的最佳方法

database - 我如何使用 ELKI 中的索引结构?

postgresql - LAB 颜色的 Postgres 空间索引?

Python3导入错误: No module named '_tkinter' on Ubuntu

python - 在python opencv中写入和/或读取后的图像像素值

python - 包含检查枚举值

serialization - 如何序列化图结构?