【发布时间】:2018-10-08 10:20:59
【问题描述】:
如何使用 Geodjango 在内存中创建 r-tree 索引?
我有许多固定的多边形(硬编码),我想检查一个点属于这些多边形中的哪一个。
我想在内存中执行此操作以避免依赖于空间数据库。
【问题讨论】:
-
嘿@HelgiBorg,我在想,你觉得我的回答有帮助吗?
标签: python django geodjango r-tree
如何使用 Geodjango 在内存中创建 r-tree 索引?
我有许多固定的多边形(硬编码),我想检查一个点属于这些多边形中的哪一个。
我想在内存中执行此操作以避免依赖于空间数据库。
【问题讨论】:
标签: python django geodjango r-tree
您可以使用一个名为:Rtree 的库,它独立于启用空间的数据库。
# 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)))
分手:
(xmin, ymin, xmax, ymax) 或 (left, bottom, right, top)。关于文档是这样说的:
插入一个点,即
left == right&&top == bottom,本质上会在索引中插入一个点条目,而不是复制额外的坐标并插入它们。但是,没有明确插入单个点的捷径。
所以我们使用这个“怪癖”将我们的点应用于索引上的多边形intersection 查询。
【讨论】: