Saturday, 1 September 2018

python - How to use qgis:selectbylocation in PyQGIS?


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 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
INTERSECT
PREDICATE

METHOD
OUTPUT


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.




Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...