1

In our algorithm, we do three steps:

  1. call Algorithm A AlgoA,
  2. takeResultLayer into lyr from AlgoA output,
  3. for each feature of lyr we want to run Algorithm B AlgoB .

Code of my processAlgorithm looks like this:

 alg_params = {
 'Input': parameters['INPUT'],
 'native:intersection_1:OUT': 'memory:'
 }
 zta = processing.run('model:AlgoA', alg_params, context=context, feedback=feedback, is_child_algorithm=True)['native:intersection_1:OUT']
 
 lyr = context.takeResultLayer(zta) 
 features = lyr.getFeatures()
 
 for i, f in enumerate(features):
 # FeaturesIntersectionOperation
 alg_params = {
 'One_Feature': f,
 'native:intersection_1:out': 'memory:'
 }
 
 out = processing.run('model:AlgoB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)['native:intersection_1:out']

The error I get is:

Unable to execute algorithm Could not load source layer for One_Feature: invalid value

If I pass an entire layer lyr as 'One_Feature': lyr it all runs Ok. If I use my algorithm from QGis UI with iterate over layer features selected like this: enter image description here it also runs Ok.

So how to implement for each layer feature inside QGIS 3+ processAlgorithm script?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 10, 2021 at 23:36

1 Answer 1

1

Instead of

 for i, f in enumerate(features):
 # FeaturesIntersectionOperation
 alg_params = {
 'One_Feature': f,
 'native:intersection_1:out': 'memory:'
 }
 
 out = processing.run('model:AlgoB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)['native:intersection_1:out']

do

 # List features ids from layer
 ids = [f.id() for f in features]
 # Loop to select by id the layer
 for id in ids:
 lyr.selectByIds([id])
 # Use the selected layer with the following (see https://qgis.org/pyqgis/master/core/QgsProcessingFeatureSourceDefinition.html#qgis.core.QgsProcessingFeatureSourceDefinition for params)
 sourceDef = QgsProcessingFeatureSourceDefinition(lyr.id(), True)
 # FeaturesIntersectionOperation
 alg_params = {
 'One_Feature': sourceDef,
 'native:intersection_1:out': 'memory:'
 }
 
 out = processing.run('model:AlgoB', alg_params, context=context, feedback=feedback, is_child_algorithm=True)['native:intersection_1:out']
answered Dec 12, 2021 at 1:11
2
  • Sadly getting same Could not load source layer for One_Feature: output_32b4b3b6_f977_4ffd_9d19_5e366c1e0c41 not found error Commented Dec 12, 2021 at 1:59
  • How did you define your model:AlgoB? In particular what does processing.algorithmHelp("model:AlgoB") returns? Depending of your algo, our approach could be wrong (for both of us) Commented Dec 12, 2021 at 2:16

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.