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,4373 gold badges21 silver badges41 bronze badges
asked Apr 1, 2020 at 5:38

1 Answer 1

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

answered Apr 1, 2020 at 6:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.