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.

Taras
  • 32,823
  • 4
  • 66
  • 137
BritishSteel
  • 6,637
  • 4
  • 38
  • 64

1 Answers1

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

xunilk
  • 29,891
  • 4
  • 41
  • 80
  • Incredible! This will be a heavily upvoted answer, as it is the only example on the entire web that actually works ;-) – BritishSteel Nov 02 '15 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? – BritishSteel Nov 02 '15 at 11:21
  • I used this documentation (for qgis 2.8; not for qgis 2.6): https://docs.qgis.org/2.8/en/docs/user_manual/processing/console.html – xunilk Nov 02 '15 at 11:25