3
在PyQGIS中循环执行空间查询
我所试图做的事:循环通过一个点shapefile,然后选择属于每个点为一个多边形。 以下代码受我在一本书中发现的空间查询示例的启发: mitte_path = r"D:\PythonTesting\SelectByLocation\mitte.shp" punkte_path = r"D:\PythonTesting\SelectByLocation\punkte.shp" polygon = QgsVectorLayer(mitte_path, 'Mitte', 'ogr') points = QgsVectorLayer(punkte_path, 'Berlin Punkte', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(polygon) QgsMapLayerRegistry.instance().addMapLayer(points) polyFeatures = polygon.getFeatures() pointsCount = 0 for poly_feat in polyFeatures: polyGeom = poly_feat.geometry() pointFeatures = points.getFeatures(QgsFeatureRequest().setFilterRect(polyGeom.boundingBox())) for point_feat in pointFeatures: points.select(point_feat.id()) pointsCount += 1 print 'Total:',pointsCount 这有效,并且确实选择了数据集,但是问题在于它是通过bounding box选择的,因此显然返回的点我不感兴趣: 如何不使用qgis:selectbylocation仅返回多边形内的点? …