【问题标题】:Geodjango - in memory r-tree indexGeodjango - 内存中的 r-tree 索引
【发布时间】:2018-10-08 10:20:59
【问题描述】:

如何使用 Geodjango 在内存中创建 r-tree 索引?

我有许多固定的多边形(硬编码),我想检查一个点属于这些多边形中的哪一个。

我想在内存中执行此操作以避免依赖于空间数据库。

【问题讨论】:

  • 嘿@HelgiBorg,我在想,你觉得我的回答有帮助吗?

标签: python django geodjango r-tree


【解决方案1】:

您可以使用一个名为:Rtree 的库,它独立于启用空间的数据库。

来自tutorial:

# Import the library and create an index:
from rtree import index
idx = index.Index()

# Insert your fixed polygons to the index by the envelope (bounding box)
# coordinates of those polygons:
for index, polygon in enumerate(my_polygon_list):
    idx.insert(index, polygon.envelope)

# To query with a point you can do the following (explanation after the example):
potential_polys = list(idx.intersection((point.x, point.y, point.x, point.y)))

分手:

  • 教程中关于如何设置 Rtree 索引的部分非常简单。
  • 几何的边界框(或信封)坐标必须采用以下格式:
    (xmin, ymin, xmax, ymax)(left, bottom, right, top)
  • 关于文档是这样说的:

    插入一个点,即left == right && top == bottom,本质上会在索引中插入一个点条目,而不是复制额外的坐标并插入它们。但是,没有明确插入单个点的捷径。

    所以我们使用这个“怪癖”将我们的点应用于索引上的多边形intersection 查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多