简体   繁体   中英

How to select points inside the areas of a multiple polygons vector layer?

I wrote this code to select points within polygons using pyqgis.

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)

It works only when my polygon shapefile is compsed by only one feature, otherwise I get this error

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'

I tried to modify the last part with this:

for pt in (range(0, pointLayer.featureCount()):
    ptFeat = pointFeatureList[pt] 
    ptGeometry = ptFeat.geometry()
    for pl in (range(0, polygonLayer.featureCount()):
        plFeat = polygonFeatureList[pl]
        plGeometry = plFeat.geometry()
        if plGeometry.contains(ptGeometry):
            pointLayer.select(ptFeat, id)

but this time I get this kind of error

TypeError: arguments did not match any overloaded call:
  QgsVectorLayer.select(QgsRectangle, bool): argument 1 has unexpected type 'QgsFeature'
  QgsVectorLayer.select(int): argument 1 has unexpected type 'QgsFeature'
  QgsVectorLayer.select(unknown-type): argument 1 has unexpected type 'QgsFeature'

So far I didn't find the way to make a correct use of the "select" function in a spatial query.

I found two solutions

This is the first

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())

This is the second solution

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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