3

In my project I have a vector layer and a raster layer. I want to remove all features of the vector layer where the feature's location has a height value of 0 in the same location in the raster layer.

This works fine by using "Select by expression" and using the below expression:

raster_value('layerid' ,1,make_point(x($geometry),y($geometry))) = 0

and then replacing 'layerid' with the ID of the raster layer.

I want to automate a lengthy workflow with the graphical modeler, where this is one of the tasks.

It currently looks like this: model

I'm trying to use "Extract by expression", with the same expression as above, and it works as long as the layerid parameter is changed prior to running the model, however I want to use the raster layer input heightmap to get the ID. Like so: expression

This isn't working. The expression returns null every time. I've tried using eval(@heightmap), as well, but no luck.

As an attempt of a workaround I have tried to use "Load layer into project" algorithm, and then using it's output variable in the expression, but this did the same.

How can I put the layerid of the heightmap input into the expression properly?

2
  • Have you checked for the exact layer ID in the field calculator/expression builder under map layers? Usually there's a lot of numbers after the layer name.
    – Erik
    Commented Sep 4, 2020 at 13:59
  • Yeah. As I explained that works. The point is to get this layerid from the selected layer in the model parameters, which in this case is called "heightmap".
    – henrikx
    Commented Sep 4, 2020 at 22:00

2 Answers 2

1

I have not found a solution to the issue of referring to a model input in expression, however I did find a solution which solved what I was trying to do.

The algorithm "Zonal Statistics" can add attributes to each feature of the vector layer which can contain the mean value of the raster layer for the polygon area. zonal-statistics

This allowed me to use that field as the comparison value in the "Extract by expression" algorithm extract-by-expression

To ensure that "Zonal statistics" runs before "Extract by expression", "Zonal Statistics" can be set as a dependency.

Note that you can also use "Extract by attribute" instead of "expression".

2
  • Glad you found a working workaround. To my knowledge, your original idea is just not possible. At least I came across similar issues, spending days trying to find a solution, but couldnt. Only a few algorithms, like advanced python field calculator, seem to do actually work with layerinputs as parameters like @heightmap... You can see this by the connections the modeler draws. If there is one, it will work, if there is no connection between layerinput and algorithm, it will not. Hopefully this feature will work with all algorithms someday.
    – MrXsquared
    Commented Sep 6, 2020 at 22:09
  • That's weird, because I tried to use the field calculator with python to add the heightvalue as an attribute, but could not get that to work either. Could be I just did it wrong though.
    – henrikx
    Commented Sep 8, 2020 at 11:24
0

(In 3.28.3) in Expression Dialog inside Model Designer there are special variables which represent layer id's for input map layers.

This works at least for calculating feature count using layer property function like this, where inp is the Description for input Parameter:

layer_property( @inp,'feature_count')

Sample project: sample project

Model setup: model setup

Result of executing the model: expression calculated

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.