Shapely 1.8.5 中文文档

  1. 主页
  2. 文档
  3. Shapely 1.8.5 中文文档
  4. User Manual
  5. STR-packed R-tree(STR算法构建R树)

STR-packed R-tree(STR算法构建R树)

Shapely provides an interface to the query-only GEOS R-tree packed using the Sort-Tile-Recursive algorithm. Pass a list of geometry objects to the STRtree constructor to create a spatial index that you can query with another geometric object. Query-only means that once created, the STRtree is immutable. You cannot add or remove geometries.

Shapely提供了一个使用Sort-Tile-Recursive算法封装的GEOS R-tree的查询接口。将一个几何对象的列表传递给STRtree构造函数来创建一个空间索引,你可以用另一个几何对象来查询。仅限查询意味着一旦创建,STRtree是不可改变的。你不能添加或删除几何对象。

  • class strtree.STRtree(geometries)

The STRtree constructor takes a sequence of geometric objects.

STRtree构造函数接收一连串的几何对象。

References to these geometric objects are kept and stored in the R-tree.

这些几何对象的引用被保留并存储在R树中。

New in version 1.4.0.

strtree.query(geom)

Returns a list of all geometries in the strtree whose extents intersect the extent of geom. This means that a subsequent search through the returned subset using the desired binary predicate (eg. intersects, crosses, contains, overlaps) may be necessary to further filter the results according to their specific spatial relationships.

返回str tree中extents与geom的extent相交的所有几何对象的列表。这意味着随后可能需要使用所需的二元谓词(例如: intersects, crosses, contains, overlaps)对返回的子集进行搜索,以根据其特定的空间关系进一步过滤结果。

>>> from shapely.strtree import STRtree
>>> points = [Point(i, i) for i in range(10)]
>>> tree = STRtree(points)
>>> query_geom = Point(2,2).buffer(0.99)
>>> [o.wkt for o in tree.query(query_geom)]
['POINT (2 2)']
>>> query_geom = Point(2, 2).buffer(1.0)
>>> [o.wkt for o in tree.query(query_geom)]
['POINT (1 1)', 'POINT (2 2)', 'POINT (3 3)']
>>> [o.wkt for o in tree.query(query_geom) if o.intersects(query_geom)]
['POINT (2 2)']
Note
To get the original indexes of the query results, create an auxiliary dictionary. But use the geometry ids  as keys since the shapely geometries themselves are not hashable.
为了获得查询结果的原始索引,创建一个辅助字典。但要用几何对象的id作为键,因为几何对象本身是不可哈希的。
>>> index_by_id = dict((id(pt), i) for i, pt in enumerate(points))
>>> [(index_by_id[id(pt)], pt.wkt) for pt in tree.query(Point(2,2).buffer(1.0))]
[(1, 'POINT (1 1)'), (2, 'POINT (2 2)'), (3, 'POINT (3 3)')]

strtree.nearest(geom)

Returns the nearest geometry in strtree to geom.

返回strtree中与geom最近的几何对象。

>>> tree = STRtree([Point(i, i) for i in range(10)])
>>> tree.nearest(Point(2.2, 2.2)).wkt
'Point (2 2)'
标签 ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here