8

In the QGIS GUI, the select-by-location algorithm has a choice to only use the selected features from the INTERSECT layer. How can I do this (or use any other algorithm) in a python script for QGIS 3?

I do not want to save temporal layer as mentioned in Run QGIS Model on selected features only?

I have been running this script but it does not use the selected features.

import processing
input_vlayer = 'Some path'
intersect_vlayer = 'Some path'
input_vlayer = iface.addVectorLayer(input_vlayer, 'input_vlayer', 'ogr')
intersect_vlayer = iface.addVectorLayer(intersect_vlayer, 'intersect_vlayer', 'ogr')
# Pass the first 5 features
for i in range(5):
 
 # Select the features
 intersect_vlayer.select(i)
 # Set input params
 params = {'INPUT':input_vlayer,
 'PREDICATE':0,
 'INTERSECT':intersect_vlayer, # USE HERE THE SELECTED FEATURES
 'METHOD':0}
 result = processing.run("qgis:selectbylocation", params)
 # Remove the current selection and then pass to the next one
 intersect_vlayer.removeSelection()

Also, from QGIS select by location doesn't use selected features only they mentioned to enable [Processing] > [Options] > [General] > [Use only selected features]. However I can't see this option in my qgis version. I'm using QGIS Madeira 3.4.3

enter image description here

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Feb 5, 2019 at 20:11
1
  • you can use QgsProcessingFeatureSourceDefinition and ecapsulate selected id Commented Feb 5, 2019 at 21:02

1 Answer 1

11

For only selected feature use the QgsProcessingFeatureSourceDefinition class.

Code below with the input of selected features only:

import processing
input_vlayer = 'Some path'
intersect_vlayer = 'Some path'
input_vlayer = iface.addVectorLayer(input_vlayer, 'input_vlayer', 'ogr')
intersect_vlayer = iface.addVectorLayer(intersect_vlayer, 'intersect_vlayer', 'ogr')
for i in range(5):
 # Select the features
 intersect_vlayer.select(i)
 # Set input params
 params = {'INPUT':input_vlayer,
 'PREDICATE':0,
 'INTERSECT':QgsProcessingFeatureSourceDefinition(intersect_vlayer.id(), True), # USE HERE THE SELECTED FEATURES
 'METHOD':0}
 result = processing.run("qgis:selectbylocation", params)
 # Remove the current selection and then pass to the next one
 intersect_vlayer.removeSelection()
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Feb 5, 2019 at 21:05
5
  • Thanks @Francisco Raga. Just one thing, I had to add QgsProcessingFeatureSourceDefinition(intersect_vlayer.id(), True) to get it work. Thanks. Commented Feb 5, 2019 at 21:36
  • Ooops!yes,your are correct!add this to my answer Commented Feb 5, 2019 at 21:40
  • 1
    When I add QgsProcessingFeatureSourceDefinition to the INPUT parameter it says me : Could not load source layer for INPUT: layerXXX not found Commented Feb 7, 2019 at 8:04
  • you can use this parameter in the INPUT flag!Put your code in other question,because probably the INPUT layer is not correct Commented Feb 7, 2019 at 9:46
  • I posted my question with my code : gis.stackexchange.com/questions/311336/… Commented Feb 7, 2019 at 10:14

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.