I can run the below processing algorithm
myresult = processing.run("qgis:selectbylocation",
{ 'INPUT': selection_layer.name() ,
'PREDICATE': 0,
'INTERSECT': w_r_t_selection_layer.name(),
'METHOD': 0,
}
)
But how to run the above process only on selected rows of INTERSECT layer?
When we run the same processing via QGIS GUI, there is checkbox to obtain that function but I cannot find any way to do same via Python scripting.
1 Answer 1
Execute the tool in in QGIS gui with box checked and then look in processing history for correct syntax. Looks like you should be using
QgsProcessingFeatureSourceDefinition
See for example Run pyQGIS algorithm on selected features in layer
Example:
layerlist = [layer for layer in QgsProject.instance().mapLayers().values()]
layerlist.sort(key=lambda x: x.name())
polys, points = layerlist
#Select one point
processing.run("qgis:selectbyexpression",
{'INPUT':points.name(),'EXPRESSION':' \"id\" = 33 ','METHOD':0})
print('Selected point(s): {0}'.format([f['id'] for f in points.getSelectedFeatures()]))
#Select the polygon overlapping the selected point
myresult = processing.run("qgis:selectbylocation",
{ 'INPUT': polys.name() ,
'PREDICATE': 0,
'INTERSECT': QgsProcessingFeatureSourceDefinition(points.id(), True),
'METHOD': 0,
}
)
print('Selected polygon(s): {0}'.format([f['KOMMUNKOD'] for f in polys.getSelectedFeatures()]))