8

I am trying to use the processing toolbox to perform a spatial query (points in polygon). I am not sure how to use the tool and I keep getting this Error:

Wrong number of parameters.

Here is my script:

polygon_path = r"D:\PythonTesting\SelectByLocation\mitte.shp"
points_path = r"D:\PythonTesting\SelectByLocation\punkte.shp"

polygon = QgsVectorLayer(polygon_path, 'Mitte', 'ogr')
points = QgsVectorLayer(points_path, 'Berlin Punkte', 'ogr')

processing.runalg('qgis:selectbylocation', points, polygon, False, False, False, 0)

I used the documentation, which indicates that I have to use 6 parameters.

However, if I call alghelp the following is returned, indicating 5 parameters:

ALGORITHM: Select by location
    INPUT <ParameterVector>
    INTERSECT <ParameterVector>
    PREDICATE <ParameterGeometryPredicate>
    METHOD <ParameterSelection>
    OUTPUT <OutputVector>


METHOD(Aktuelle Auswahl ändern mit)
    0 - creating new selection
    1 - adding to current selection
    2 - removing from current selection

Either way I am not getting it to run correctly.

1

1 Answer 1

12

I used this code:

mapcanvas = iface.mapCanvas()

layers = mapcanvas.layers()

processing.runalg('qgis:selectbylocation', layers[0], layers[1], u'within', 0)

with this situation:

enter image description here

and it worked:

enter image description here

Updating for QGIS 3:

Following code works in QGIS 3.20 (Odense):

mapcanvas = iface.mapCanvas()

layers = mapcanvas.layers()

parameters = { 'INPUT' : layers[0], 
               'INTERSECT' : layers[1], 
               'METHOD' : 0, 
               'PREDICATE' : [0] }

processing.run('qgis:selectbylocation', parameters )

I tried it out as it can be observed in following picture.

enter image description here

3
  • Incredible! This will be a heavily upvoted answer, as it is the only example on the entire web that actually works ;-) Commented Nov 2, 2015 at 11:17
  • I still can't figure out how to read the documentation. It looks like there should be two parameters in between layers[1] and 0. But it won't run if you go about it that way... is it just wrong or do I not know how to read it? Commented Nov 2, 2015 at 11:21
  • I used this documentation (for qgis 2.8; not for qgis 2.6): docs.qgis.org/2.8/en/docs/user_manual/processing/console.html
    – xunilk
    Commented Nov 2, 2015 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.