4

I need to do a processing model that has several optional vector inputs, but that produces only one output. I have separate conditional processing branches for all inputs, but in the end I would need to merge all of the created branch outputs into one. Here comes the problem. As the merge needs to be done after all the conditional branches are done, I need to set it as dependent of all of the branches but because of that, the merge tool is not completed at all if even one of the conditional branches above is not completed. So, as a result, the model doesn't produce anything if some of the inputs are not set.

Does anyone have a solution for this?

Even workarounds do. My QGIS version is 3.16.9.

A screenshot of the model.

The picture should clarify my idea. The model is meant to calculate all kinds of statistics to the given point objects ("Kohteet"). "Oksien ehtolauseet" is the conditional branch tool checking which inputs are given and so starting or not starting separate processing branches for the inputs. The merge tool in the end should merge what there is to be merged after the branches with inputs are processed, but it at the moment fails if all inputs are not given and thus all branches are not completed.

So, if I give an input to both "Koulut" and "Kaupat" the both branches are completed and finally merged as they should. If I, however, give an input to only "Kaupat" the model doesn't perform merge and thus doesn't return any output either, because the branch of "Koulut" is not performed and the merge tool is dependent of it. It should, however, in that case merge the result of the branch of "Kaupat" with nothing and return that as an output.

(For the sake of fast testing, the merge is at the moment set after the first tools of the first two branches even though the branches are meant to continue after removing the null geometries and there are several other branches and inputs in the model as well.)

My Python skills are limited to very basic coding, so I seek some kind of a solution in graphical modeler or some piece of code that I could add to Python script to make it work.

1
  • Welcome to GIS.SE! First of all: Your screenshot is great artwork ;-) I think your inputs are all mandatory, don't you encounter errors when "none" inputs are used in further processing? I do so, and I'm therefore little confuse how to deal with mandatory inputs at all (perhaps I should address this in another topic). Furthermore, I think that you don't need to define dependencies in your merge-algorithm, because imho this is only needed for algorithms that are not executed directly one after the other. May 20, 2022 at 7:18

2 Answers 2

1

I had the same here, the merge algorithm didn't work. So I added different merge algorithms according to the brenches with the same 'Merged' output names. It works fine on QGIS 3.18.

enter image description here

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 5, 2022 at 14:21
1

You can solve this by using a CASE with different arrays using the conditional booleans that determine which algorithms run and which don't. For instance, let's say you have 2 Booleans: Bool1 makes Algorithm1 run and Bool2 makes Algorithm2 run. This means that there are 4 possible combinations (this of course grows more difficult the more Booleans you have):

  • Both are TRUE (Both Alg1 and Alg2 run)
  • Both are FALSE (Neither Alg1 nor Alg2 run)
  • Bool1 is TRUE and Bool2 is FALSE (Only Alg1 runs)
  • Bool1 is FALSE and Bool2 is TRUE (Only Alg2 runs)

Now all you need to do is have the input for your merge (I recommend you use GDAL's Merge) set to Pre-calculated value. The input requests an array of inputs, so the formula needs to look a bit like this:

array( 'Output from Algorithm1', 'Output from Algorithm2', 'Output for Algorithm3', etc.)

or, in Graphical modeler form:

array( @Output_from_Algorithm1, @Output_from_Algorithm2, @Output_from_Algorithm3, etc.)

All that's left is for you to make the relevant cases, using CASE

CASE
WHEN Boolean1 IS TRUE AND Boolean2 IS TRUE THEN array(@Output_from_Algorithm1, @Output_from_Algorithm2, @All_other_relevant_algorithms)
WHEN Boolean1 IS TRUE AND Boolean2 IS FALSE THEN array(@Output_from_Algorithm1,  @All_other_relevant_algorithms)
WHEN Boolean1 IS FALSE AND Boolean2 IS TRUE THEN array(@Output_from_Algorithm2, @All_other_relevant_algorithms)
ELSE array(@All_other_relevant_algorithms)
END

Happy coding!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.