2017-01-18 114 views
0

我對使用QGIS非常陌生,我擁有的是一個shape shape文件和一個多邊形shape文件。我想選擇其中至少有一個點的所有多邊形。我遇到的問題是需要多長時間。我有100萬個點和大約32萬個多邊形,所以使用空間查詢需要很長時間。我聽說我需要編寫一個帶有空間索引的Python腳本來獲得一個可行的快速結果,但我不知道如何解決這個問題。任何幫助將不勝感激。QGIS選擇與python相交的多邊形

我一直試圖從其他堆棧溢出問題湊齊是:

pointProvider = self.pointLayer.dataProvider() 
all_point = pointProvider.getFeatures() 
delta = 0.1 

for point in all_point: 

    searchRectangle = QgsRectangle(point.x() - delta, point.y() - delta, point.x() + delta, point.y() + delta) 

    candidateIDs = line_index.intesects(searchRectangle) 

    for candidateID in candidateIDs: 
     candFeature == rotateProvider.getFeatures(QgsFeatureRequest(candidateID)).next() 
     if candFeature.geometry().contains(point): 

      break 

這引發了一個NameError:名字「自我」是沒有定義

回答

0

我找到答案了基於GIS堆棧交換,這你可以找到here

我使用的代碼是:

from qgis.core import * 
import processing 

layer1 = processing.getObject('MyPointsLayer') 
layer2 = processing.getObject('MyPolygonsLayer') 

index = QgsSpatialIndex() # Spatial index 
for ft in layer1.getFeatures(): 
    index.insertFeature(ft) 

selection = [] # This list stores the features which contains at least one point 
for feat in layer2.getFeatures(): 
    inGeom = feat.geometry() 
    idsList = index.intersects(inGeom.boundingBox()) 
    if idsList: 
     selection.append(feat) 

# Select all the polygon features which contains at least one point 
layer2.setSelectedFeatures([k.id() for k in selection])