1

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.

enter image description here

Noura
  • 3,429
  • 3
  • 20
  • 41
Abhijit Gujar
  • 2,750
  • 3
  • 29
  • 43

1 Answers1

2

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

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161