In our algorithm, we do three steps:
- call Algorithm A
AlgoA
, takeResultLayer
intolyr
fromAlgoA
output,- for each feature of
lyr
we want torun
Algorithm BAlgoB
.
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?
1 Answer 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']
-
Sadly getting same Could not load source layer for One_Feature: output_32b4b3b6_f977_4ffd_9d19_5e366c1e0c41 not found errorDuckQueen– DuckQueen2021年12月12日 01:59:01 +00:00Commented Dec 12, 2021 at 1:59
-
How did you define your
model:AlgoB
? In particular what doesprocessing.algorithmHelp("model:AlgoB")
returns? Depending of your algo, our approach could be wrong (for both of us)ThomasG77– ThomasG772021年12月12日 02:16:36 +00:00Commented Dec 12, 2021 at 2:16