12

I want to use the Field Calculator inside the Graphical Modeler to create a new column in an input vector layer (LAYER1) using data in another layer (LAYER2). LAYER2 is the output of another algorithm inside the model.

The formula is as follows:

attribute(get_feature("LAYER2", 'WKT', geom_to_wkt(start_point($geometry))) , 'NODEID')

Actually, I am trying to implement an algorithm similar to what's described in Getting points-touching-a-line attributes back to lines layer using QGIS?.

When used outside the model, it works perfectly.

When used inside the model, the column is always created with NULL values.

Any ideas on how to reference LAYER2 inside the Field Calculator for this to work?

11
  • Hit the same problem. Tried using the saved name of the layer, the one presented in the GUI ("X from algorithm Y"), but neither are found. The dependencies are set up properly. Log shows temporary names only — any way to catch those? Commented Sep 9, 2018 at 12:20
  • 2
    What about a join? Field Calculator always works on only one single layer. Commented Sep 9, 2018 at 15:20
  • Maybe there's a way to save the desired 'WKT' value from "LAYER2" as a variable within the model. Then you could use that variable in the Field Calculator instead of your attribute(get_feature()) formula.
    – csk
    Commented Sep 9, 2018 at 16:07
  • @Stefan_Fairphone It can work on multiple, that's why the functions. The problem is just in the modeller context. If you have a concrete idea with a join, please provide an answer. Commented Sep 10, 2018 at 17:41
  • 1
    You would need a key attribute that is the same for a feature in both layers. Do you have such a key attribute (some kind of ID, etc.)? Commented Sep 10, 2018 at 21:30

2 Answers 2

3

I found a possible workaround:

Seems that high level variables (e.g. input model parameters) can't be resolved inside nested expressions.

The trick is to do evaluation at higher level with some kind of lambda (e.g. string concatenation) then refer concatenation result in nested expressions.

I didn't investigate if the reason is that model scope is not propagated or not checked during expression evaluation.

2
+50

The idea would be to create the join and then create the field:

  1. Create geometry from WKT on LAYER2 using Geometry by expression
  2. Extract the start point of LAYER2 using Extract specific vertices and specify first vertex (index 0).
  3. Join using Join attributes by location using the output of previous alg and of LAYER1.
  4. Use the Field Calculator using the joined field
3
  • This can easily hit the same referencing problem at #1. Commented Sep 11, 2018 at 6:44
  • If you know the field name you can use it directly in the expression since it is joined. Or am I missing something? Commented Sep 11, 2018 at 13:02
  • I think I just got confused, you've switched OP's layer 1 and 2. Commented Sep 11, 2018 at 21:07

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.