类文档

class rtree.index.Index(*args, **kwargs)

R树、MVR树或TPR树索引对象

__init__(*args, **kwargs)

创建新索引

参数
  • filename -- 构造函数中的第一个参数假定为一个文件名,用于确定应使用索引的基于文件的存储。如果第一个参数不是basestring类型,则假定它是ICustomStorage或派生类的实例。如果第一个参数既不是basestring类型,也不是icustomStorage实例,则假定它是输入索引项流。

  • stream --

    如果构造函数中的第一个参数不是basestring类型,则假定它是一个可迭代的数据流,将引发StopIteration。它必须采用 interleaved 索引的属性。下面的例子假设 interleaved 是假的:

    (id,
     (minx, maxx, miny, maxy, minz, maxz, ..., ..., mink, maxk),
     object)
    

    对象不能为空,但必须放置一个 None 在那里。

    For a TPR-Tree, this would be in the form:

    (id,
     ((minx, maxx, miny, maxy, ..., ..., mink, maxk),
      (minvx, maxvx, minvy, maxvy, ..., ..., minvk, maxvk),
      time),
     object)
    

  • storage -- 如果构造函数中的第一个参数是ICustomStorage的实例,则使用给定的自定义存储。

  • interleaved -- 正确或错误,默认为正确。此参数确定采用坐标的所有方法的坐标顺序。

  • properties -- 对象An index.Property 设置对象的创建和实例化属性,并将这些属性传递到libspatialindex中。一些属性是根据您喜欢的实例化参数生成的。例如``pagesize``和``overwrite``,以确保与以前版本的库兼容。 必须在对象上设置所有其他属性。

警告

The coordinate ordering for all functions are sensitive the index's interleaved data member. If interleaved is False, the coordinates must be in the form [xmin, xmax, ymin, ymax, ..., ..., kmin, kmax]. If interleaved is True, the coordinates must be in the form [xmin, ymin, ..., kmin, xmax, ymax, ..., kmax]. This also applies to velocities when using a TPR-Tree.

一个基本示例:

>>> from rtree import index
>>> p = index.Property()

>>> idx = index.Index(properties=p)
>>> idx  
rtree.index.Index(bounds=[1.7976931348623157e+308,
                        1.7976931348623157e+308,
                        -1.7976931348623157e+308,
                        -1.7976931348623157e+308],
                        size=0)

将项目插入索引::

>>> idx.insert(4321,
...            (34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...            obj=42)

查询:

>>> hits = idx.intersection((0, 0, 60, 60), objects=True)
>>> for i in hits:
...     if i.id == 4321:
...         i.object
...         i.bbox
... 
42
[34.37768294..., 26.73758537..., 49.37768294..., 41.73758537...]

使用自定义序列化程序::

>>> class JSONIndex(index.Index):
...     def dumps(self, obj):
...         # This import is nested so that the doctest doesn't
...         # require simplejson.
...         import simplejson
...         return simplejson.dumps(obj).encode('ascii')
...
...     def loads(self, string):
...         import simplejson
...         return simplejson.loads(string.decode('ascii'))

>>> stored_obj = {"nums": [23, 45], "letters": "abcd"}
>>> json_idx = JSONIndex()
>>> try:
...     json_idx.insert(1, (0, 1, 0, 1), stored_obj)
...     list(json_idx.nearest((0, 0), 1,
...                           objects="raw")) == [stored_obj]
... except ImportError:
...     True
True
property bounds

返回索引的边界

参数

coordinate_interleaved -- 如果为真,则坐标以[xmin,ymin,…,kmin,xmax,ymax,…,kmax]的形式转换,否则返回为[xmin,xmax,ymin,ymax,……,kmin,kmax]。如果未指定,则 interleaved 使用索引的成员,默认为true。

close()

强制将索引刷新到存储区。呈现索引不可访问。

count(coordinates)

返回与给定坐标相交的对象数。

参数

coordinates -- sequence or array This may be an object that satisfies the numpy array protocol, providing the index's dimension * 2 coordinate pairs representing the mink and maxk coordinates in each dimension defining the bounds of the query window. For a TPR-Tree, this must be a 3-element sequence including not only the positional coordinate pairs but also the velocity pairs minvk and maxvk and a time pair for the time range as a float.

以下示例查询任何对象的索引存储在索引中的任何对象与坐标中给定的边界相交:

>>> from rtree import index
>>> idx = index.Index()
>>> idx.insert(4321,
...            (34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...            obj=42)

>>> print(idx.count((0, 0, 60, 60)))
1

This example is similar for a TPR-Tree:

>>> p = index.Property(type=index.RT_TPRTree)  
>>> idx = index.Index(properties=p)  
>>> idx.insert(4321,
...            ((34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...             (0.5, 2, 1.5, 2.5),
...             3.0),
...            obj=42)  

>>> print(idx.count(((0, 0, 60, 60), (0, 0, 0, 0), (3, 5))))
... 
1
delete(id, coordinates)
Deletes an item from the index with the given 'id' and

coordinates given by the coordinates sequence. As the index can contain multiple items with the same ID and coordinates, deletion is not guaranteed to delete all items in the index with the given ID and coordinates.

参数
  • id -- long integer A long integer ID for the entry, which need not be unique. The index can contain multiple entries with identical IDs and coordinates. Uniqueness of items should be enforced at the application level by the user.

  • coordinates -- sequence or array Dimension * 2 coordinate pairs, representing the min and max coordinates in each dimension of the item to be deleted from the index. Their ordering will depend on the index's interleaved data member. These are not the coordinates of a space containing the item, but those of the item itself. Together with the id parameter, they determine which item will be deleted. This may be an object that satisfies the numpy array protocol. For a TPR-Tree, this must be a 3-element sequence including not only the positional coordinate pairs but also the velocity pairs minvk and maxvk and a time pair for the original time the object was inserted and the current time as a float.

例子::

>>> from rtree import index
>>> idx = index.Index()
>>> idx.delete(4321,
...            (34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734))

For the TPR-Tree:

>>> p = index.Property(type=index.RT_TPRTree)  
>>> idx = index.Index(properties=p)  
>>> idx.delete(4321,
...            ((34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...             (0.5, 2, 1.5, 2.5),
...             (3.0, 5.0)))  
insert(id, coordinates, obj=None)

在索引中插入具有给定坐标的项。

参数
  • id -- long integer此索引项的标识符的长整数。ID不需要是唯一的,就可以插入到索引中,并且如果需要,则由用户确定它们是唯一的。

  • coordinates -- sequence or array This may be an object that satisfies the numpy array protocol, providing the index's dimension * 2 coordinate pairs representing the mink and maxk coordinates in each dimension defining the bounds of the query window. For a TPR-Tree, this must be a 3-element sequence including not only the positional coordinate pairs but also the velocity pairs minvk and maxvk and a time value as a float.

  • obj -- 一个pickleable对象。 如果不是None,则此对象将使用:attr:`id`存储在索引中。

下面的示例将一个条目插入到id为“4321”的索引中,并且它与该id一起存储的对象是数字“42”。 此实例中的坐标排序是默认(interleaved = True)排序:

>>> from rtree import index
>>> idx = index.Index()
>>> idx.insert(4321,
...            (34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...            obj=42)

This example is inserting the same object for a TPR-Tree, additionally including a set of velocities at time 3:

>>> p = index.Property(type=index.RT_TPRTree)  
>>> idx = index.Index(properties=p)  
>>> idx.insert(4321,
...            ((34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...             (0.5, 2, 1.5, 2.5),
...            3.0),
...            obj=42)  
intersection(coordinates, objects=False)

返回索引中与给定坐标相交的ID或对象。

参数
  • coordinates -- sequence or array This may be an object that satisfies the numpy array protocol, providing the index's dimension * 2 coordinate pairs representing the mink and maxk coordinates in each dimension defining the bounds of the query window. For a TPR-Tree, this must be a 3-element sequence including not only the positional coordinate pairs but also the velocity pairs minvk and maxvk and a time pair for the time range as a float.

  • objects -- True、False或“Raw”中,如果为true,交集方法将返回与每个索引项一起存储时被pickle的索引对象,以及索引项的ID和边界。如果为“raw”,则返回的对象将不包含 rtree.index.Item 包装器。

以下示例查询任何对象的索引存储在索引中的任何对象与坐标中给定的边界相交:

>>> from rtree import index
>>> idx = index.Index()
>>> idx.insert(4321,
...            (34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...            obj=42)

>>> hits = list(idx.intersection((0, 0, 60, 60), objects=True))
>>> [(item.object, item.bbox) for item in hits if item.id == 4321]
... 
[(42, [34.37768294..., 26.73758537..., 49.37768294...,
       41.73758537...])]

如果 rtree.index.Item 未使用包装器,请求“原始”对象更快:

>>> list(idx.intersection((0, 0, 60, 60), objects="raw"))
[42]

Similar for the TPR-Tree:

>>> p = index.Property(type=index.RT_TPRTree)  
>>> idx = index.Index(properties=p)  
>>> idx.insert(4321,
...            ((34.3776829412, 26.7375853734, 49.3776829412,
...             41.7375853734),
...             (0.5, 2, 1.5, 2.5),
...             3.0),
...            obj=42)  

>>> hits = list(idx.intersection(
...     ((0, 0, 60, 60), (0, 0, 0, 0), (3, 5)), objects=True))
...  
>>> [(item.object, item.bbox) for item in hits if item.id == 4321]
... 
[(42, [34.37768294..., 26.73758537..., 49.37768294...,
       41.73758537...])]
nearest(coordinates, num_results=1, objects=False)

返回 k -与给定坐标最近的对象。

参数
  • coordinates -- sequence or array This may be an object that satisfies the numpy array protocol, providing the index's dimension * 2 coordinate pairs representing the mink and maxk coordinates in each dimension defining the bounds of the query window. For a TPR-Tree, this must be a 3-element sequence including not only the positional coordinate pairs but also the velocity pairs minvk and maxvk and a time pair for the time range as a float.

  • num_results -- integer返回的最接近给定坐标的结果数。如果两个索引项等距, both 返回。此属性意味着 num_results 可能返回超过指定数量的项目

  • objects -- True/False//Raw'中,如果为true,则最近的方法将返回与每个索引项一起存储时被pickle的索引对象,以及索引项的ID和边界。如果是“raw”,它将返回输入数据库的对象,而不返回 rtree.index.Item 包装器。

警告

This is currently not implemented for the TPR-Tree.

查找离此项最近的三个项的示例:

>>> from rtree import index
>>> idx = index.Index()
>>> idx.insert(4321, (34.37, 26.73, 49.37, 41.73), obj=42)
>>> hits = idx.nearest((0, 0, 10, 10), 3, objects=True)
class rtree.index.Property(handle=None, owned=True, **kwargs)

索引属性对象是包含许多可设置索引属性的容器。其中许多属性必须在索引创建时设置,而其他属性可用于调整性能或行为。

property buffering_capacity

缓冲能力

property custom_storage_callbacks

自定义存储回调

property custom_storage_callbacks_size

用于自定义存储的回调大小

property dat_extension

.dat文件的扩展名

property dimension

索引维度。必须大于0,尽管1的维度可能具有未定义的行为。

property filename

磁盘存储的索引文件名

property fill_factor

分支前索引节点填充因子

property idx_extension

.idx文件的扩展名

property index_capacity

指标能力

property index_id

第一个节点索引ID

property index_pool_capacity

索引池容量

property leaf_capacity

叶容量

property near_minimum_overlap_factor

MVRtrees的重叠因子

property overwrite

覆盖现有索引文件

property pagesize

使用磁盘存储时的页面大小。最好确保索引项适合单个页面以获得最佳性能。

property point_pool_capacity

点池容量

property region_pool_capacity

区域池容量

property reinsert_factor

Reinsert因子

property split_distribution_factor

分裂分布因子

property storage

索引存储。

数据:data:RT_DiskRT_Memory 或 :data:`RT_Custom`之中的一个 .

如果文件名作为第一个参数传递给:class:index.Index,则:data:RT_Disk。 如果传递了CustomStorage实例,则假定:data:RT_Custom。 否则,:data:`RT_Memory`是默认值。

property tight_mbr

使用最小限定矩形

property tpr_horizon

TPR视界

property type

索引类型。有效的索引类型值为 RT_RTreeRT_MVTreeRT_TPRTree . 此时,实际上只支持rt-rtree(默认值)。

property variant

索引变量。有效的索引变量值为 RT_LinearRT_QuadraticRT_Star

property writethrough

直写缓存

class rtree.index.Item(loads, handle, owned=False)

索引项的容器

__init__(loads, handle, owned=False)

不应该有任何理由自己来例示这些。调用时自动创建项目 rtree.index.Index.intersection() (或其他索引查询方法),如果给定函数的参数,则对象=真。

property bbox

返回索引项的边界框