我编写了这段代码,以使用pyqgis在多边形内选择点。
using System.IO;
仅当我的多边形shapefile仅由一个要素合成时,该方法才起作用,否则会出现此错误
string str = @"c:\users\john\desktop\test.txt";
Console.WriteLine(Path.GetFileName(str));
我试图以此修改最后一部分:
polygonFile = '/home/polygon.shp'
pointFile = '/home/points.shp'
polygonLayer = QgsVectorLayer(polygonFile, 'poligoni', 'ogr')
pointLayer = QgsVectorLayer(pointFile, 'punti', 'ogr')
polygonFeatureList = [plfeat for plfeat in polygonLayer.getFeatures()]
pointFeatureList = [ptfeat for ptfeat in pointLayer.getFeatures()]
polygonFeatureNumber = polygonLayer.featureCount()
pointFeatureNumber = pointLayer.featureCount()
QgsMapLayerRegistry.instance().addMapLayer(polygonLayer)
QgsMapLayerRegistry.instance().addMapLayer(pointLayer)
for ptfeat in pointFeatureList:
ptGeometry = ptfeat.geometry()
for plfeat in polygonFeatureList:
plGeometry = plfeat.geometry()
if plGeometry.contains(ptGeometry):
pointLayer.select(ptfeat.id)
但是这次我遇到这种错误
TypeError: arguments did not match any overloaded call:
QgsVectorLayer.select(QgsRectangle, bool): argument 1 has unexpected type 'builtin_function_or_method'
QgsVectorLayer.select(int): argument 1 has unexpected type 'builtin_function_or_method'
QgsVectorLayer.select(unknown-type): argument 1 has unexpected type 'builtin_function_or_method'
到目前为止,我还没有找到在空间查询中正确使用“选择”功能的方法。
答案 0 :(得分:0)
我找到了两种解决方案
这是第一个
polygonFile = '/path/poligoni.shp'
pointFile = '/path/punti.shp'
polygonLayer = QgsVectorLayer(polygonFile, 'poligoni', 'ogr')
pointLayer = QgsVectorLayer(pointFile, 'punti', 'ogr')
polygonFeatureList = [plFeat for plFeat in polygonLayer.getFeatures()]
pointFeatureList = [ptFeat for ptFeat in pointLayer.getFeatures()]
QgsMapLayerRegistry.instance().addMapLayer(polygonLayer)
QgsMapLayerRegistry.instance().addMapLayer(pointLayer)
for ptFeat in pointFeatureList:
ptGeom = ptFeat.geometry()
for plFeat in polygonFeatureList:
plGeom = plFeat.geometry()
if plGeom.contains(ptGeom):
pointLayer.select(ptFeat.id())
这是第二个解决方案
import processing
polygonFile = '/path/poligoni.shp'
pointFile = '/path/punti.shp'
polygonLayer = QgsVectorLayer(polygonFile, 'poligoni', 'ogr')
pointLayer = QgsVectorLayer(pointFile, 'punti', 'ogr')
polygonFeatureList = [plFeat for plFeat in polygonLayer.getFeatures()]
pointFeatureList = [ptFeat for ptFeat in pointLayer.getFeatures()]
QgsMapLayerRegistry.instance().addMapLayer(polygonLayer)
QgsMapLayerRegistry.instance().addMapLayer(pointLayer)
processing.runalg('qgis:selectbylocation', pointLayer, polygonLayer, u'contains', 0, 0)