简体   繁体   English

为QGIS 2空间连接(PyQGIS)创建空间索引

[英]Creating a spatial index for QGIS 2 spatial join (PyQGIS)

I've written a bit of code to do a simple spatial join in QGIS 2 and 2.2 (points that lie within a buffer to take attribute of the buffer). 我编写了一些代码在QGIS 2和2.2中进行简单的空间连接(位于缓冲区内的点采用缓冲区的属性)。 However, I'd like to employ a QgsSpatialIndex in order to speed things up a bit. 但是,我想使用QgsSpatialIndex来加快速度。 Where can I go from here: 我可以从这里去哪里:

pointProvider = self.pointLayer.dataProvider()
rotateProvider = self.rotateBUFF.dataProvider()

all_point = pointProvider.getFeatures()
point_spIndex = QgsSpatialIndex()
for feat in all_point:
    point_spIndex.insertFeature(feat)

all_line = rotateProvider.getFeatures()
line_spIndex = QgsSpatialIndex()
for feat in all_line:
    line_spIndex.insertFeature(feat)

rotate_IDX = self.rotateBUFF.fieldNameIndex('bearing')
point_IDX = self.pointLayer.fieldNameIndex('bearing')

self.pointLayer.startEditing()
for rotatefeat in self.rotateBUFF.getFeatures():
    for pointfeat in self.pointLayer.getFeatures():
        if pointfeat.geometry().intersects(rotatefeat.geometry()) == True:
            pointID = pointfeat.id()
bearing = rotatefeat.attributes()[rotate_IDX]
self.pointLayer.changeAttributeValue(pointID, point_IDX, bearing)
self.pointLayer.commitChanges()

To do this kind of spatial join, you can use the QgsSpatialIndex ( http://www.qgis.org/api/classQgsSpatialIndex.html ) intersects(QgsRectangle) function to get a list of candidate featureIDs or the nearestNeighbor (QgsPoint,n) function to get the list of the n nearest neighbours as featureIDs. 要进行这种空间连接,可以使用QgsSpatialIndex( http://www.qgis.org/api/classQgsSpatialIndex.html )相交(QgsRectangle)函数来获取候选特征ID列表或最近的邻居(QgsPoint,n)函数获取n个最近邻居的列表作为featureID。

Since you only want the points that lie within the buffer, the intersects function seems most suitable. 由于只需要缓冲区中的点,因此相交函数似乎最合适。 I have not tested if a degenerate bbox (point) can be used. 我尚未测试是否可以使用退化的bbox(点)。 If not, just make a very small bounding box around your point. 如果没有,只需在您的点周围做一个很小的边界框。

The intersects function returns all features that have a bounding box that intersects the given rectangle, so you will have to test these candidate features for a true intersection. 相交函数返回所有具有与给定矩形相交的边界框的要素,因此您必须测试这些候选要素是否为真交集。

Your outer loop should be on the points (you want to to add attribute values to each point from their containing buffer). 您的外部循环应该在这些点上(您要向其包含缓冲区的每个点添加属性值)。

# If degenerate rectangles are allowed, delta could be 0,
# if not, choose a suitable, small value
delta = 0.1
# Loop through the points
for point in all_point:
    # Create a search rectangle
    # Assuming that all_point consist of QgsPoint
    searchRectangle = QgsRectangle(point.x() - delta, point.y()  - delta, point.x() + delta, point.y() + delta)
    # Use the search rectangle to get candidate buffers from the buffer index
    candidateIDs = line_index.intesects(searchRectangle)
    # Loop through the candidate buffers to find the first one that contains the point
    for candidateID in candidateIDs:
        candFeature == rotateProvider.getFeatures(QgsFeatureRequest(candidateID)).next()
        if candFeature.geometry().contains(point):
            # Do something useful with the point - buffer pair

            # No need to look further, so break
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM