2

My model is based on a vector layer which contains a Field with layer names. The Plan is to pick the input raster (from a directory or directly from the project layers) according to an attribute value of the current object. Until now I got a working result when concatenating the two parts of the path using the expression calculator and just manually writing the file name from the Attribute table:

concat('C:/Users/Johannes/Documents/Betrieb/Onesoil/Sentinel/' , 'NDVI_26-07-2019','.tif')

The issue is to replace the second part of the function ('NDVI_26-07-2019') with the string value of a certain Field ("Sommer_I") of the input vector layer. My initial plan looked like this:

concat('C:/Users/Johannes/Documents/Betrieb/Onesoil/Sentinel/' , map_get(attributes($currentfeature), "Sommer_I"),'.tif')

Resulting in the error: "Field 'Sommer_I' not found"

enter image description here

A very similar question was asked here almost a year ago:

Using an input string in a file path for a input vector layer in QGIS model

The clipped raster is used for a simple regression analysis with yield data (point vector) from a combine harvester. The Regression Result is then clipped by the feature. The same model will be run for 'Sommer_II' and 'Sommer_III', the average of the three regressions should give reasonably exact yield predictions for every area/ (agricultural) field. In my amateurish opinion I think the array function in your last paragraph is not necessary.

enter image description here

1
  • Please clarify your question, see comment below the answer. If possible, share your model to see what exactly your intended workflow is.
    – Babel
    Jan 22 at 16:01

1 Answer 1

1

Syntax

The correct syntax looks like this:

'C:/Users/Johannes/Documents/Betrieb/Onesoil/Sentinel/'  
||  attribute (get_feature_by_id ('Zusammengeführt', 1), 'Sommer_I')  
|| '.tif'

Where Zusammengeführt is the name of the layer containing the field named Sommer_I.

Functions to use

I use pipes operator || for concatenation. You use map_get(), but this makes no sense as you don't have a map (key:value pairs), but you want to access a field on a layer. So you have to use get_feature_by_id() to be able to select a layer and a feature, then get the value of a certain attribute with attribute().

Flexible input to select rasters to use

To be flexible to run the model not only for the first feature of the layer (as defined in the expression above, second argument of get_feature_by_id), create a new numerical input, called im my case integer number. Then in the expression dialog this input is available as a variable based on this name (you see it in the expression dialog window): @integer_number - see screenshot and replace 1 in the expression above with this variable:

My layer containing the raster file names is called select, the attribute field is called raster_file_name: enter image description here

When you run the model now, you can insert the number/feature you want to run for, so which raster layer should be clipped: if you enter 2, the raster file with the name defined in feature 2 is used.

Batch mode to run for more than one or all rasters at once

To clip more then one raster layer or all rasters defined in your layer at once, run the model in batch mode. Use Autofill... > Add Values by Expression... to create an array with all those features (containing the raster file names you want to clip) for which you want to run the model, e.g. array(1,3) to run for features 1 and 3. With the following expression, you can run it for all rasters you have defined in the layer Zusammengeführt:

aggregate ('Zusammengeführt','array_agg',$id)

Make sure to check Load layers on completion at the bottom left if the clipped rasters should be loaded in QGIS.

enter image description here

5
  • I get the impression that the Zusammengeführt layer is the input layer for the model (which runs Extract by Expression and then Buffer), and OP wants to use the result of these consecutive processes to both (a) get the corresponding raster file (based on the Sommen_1 field) , and (b) clip the raster. How does your solution ensure that the final batch process (clip raster by mask layer) will only run on the same features as filtered in the model (using extract by expression)? Wouldn't you need to re-use the same filter in the array expression?
    – she_weeds
    Jan 22 at 12:55
  • I'm not really sure what the model does and where the layer comes from. Maybe OP should clarify and best share the model. Otherwise, it's difficult as we all have different ideas what might be the case.
    – Babel
    Jan 22 at 15:59
  • Added some info to the question, hope that is what you wanted to know. Thanks you for the answer, helped me a lot to get on with the model and to understand how these functions work.
    – farmgis
    Jan 22 at 23:37
  • OK, so you solved the problem, did I understand you correctly?
    – Babel
    Jan 23 at 10:26
  • Yes the model has some other issues but the part about concatenation is solved. Thanks again for the great support.
    – farmgis
    Jan 24 at 7:19

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.