1

I have a lot of (75) .asc files which I want to do the same simple raster calculation on each of (in QGIS). My calculation works when I use the raster calculator on one layer/file, but I am struggling with how to get it to run as a batch process across all 75.

When I open the raster calculator from the processing toolbox and click 'run as batch process', I assume I need to be entering my formula in the 'expression string builder'. Is that right?

If so, I don't see how to enter simple expressions (e.g. log10 of the value). If I should be doing the calculation on the 'raster_value', (a) what should I enter for its 'point' argument? (b) how do I avoid having to type the layer name manually for each of the 75 files and (c) how do I get it to set a the output name automatically (i.e. not have to manually type a different one 75 times)?

I've tried reading the manual/searching here without success.

Or would it be better to do this outside of QGIS?

Thanks very much!

John

[EDIT - added screenshots to show what I'm talking about. First is batch process window of raster calculator; second is what I assume to be the way to add an expression; third is the window where I assume you enter the expression, but as per the question above, this is where I'm stuck. Thanks!]

enter image description here enter image description here enter image description here

3
  • Thanks for this - that should help with the renaming. I can't seem to add a screenshot here so will edit the question and add there. Thanks! – JohnH May 25 at 20:51
  • I hope this solution works for you. I'm curious: what can a raster calculated with log10 be used for? – Babel May 26 at 12:08
  • 1
    Easier to show the result - will share it with you as soon as I've done it :) Thanks again for your help. – JohnH May 29 at 9:29
2

To use the QGIS raster calculator in batch mode, you must adapt the expression to use to the syntax used in "single" mode. So have a look first there and create a valid expression for what you want to do. To use log10, a valid syntax is: log10 ( "gt30e020n40@1") where @1 stands for band 1 of raster layer gt30e020n40. With this in mind, open raster calculator in batch mode.

  1. In the second column use a method to select the raster-layers you want to use, e.g with Select from Open Layers…, than selecting all or only those you want to use:

    enter image description here

  2. Now go to the first column where you set the expression the raster calculator should use. Click Autofill... / Calculate by Expression…. In the expression string builder that opens, you have to build an expression that creates (when evaluated) the expression to be used for each raster layer. What worked for me is this expression:

array_get (
    array_foreach (
        array_foreach (
            (@layers ),
            (layer_property( @element,'name'))
        ),
        'log10("' || @element  || '@1")' 
    ),
    @row_number 
)

Explanation:

  • @layers (line 4) returns an array of all map layers of the current project.

  • For each element of the array (=for each layer), we get it's name with array_foreach (line 3) and layer_property (line 5).

  • For each of these names (again we use array_foreach, line 2) we create a string that builds the raster calculator expression.

  • This raster calculator expression can be found on line 7: everything has to be declared as string, using single quotes ', expect the layer-name from the array which we refer by @element. We have thus 3 elements, concatenated by pipes ||: 'log10("' (1) @element (2) and '@1")' (3). When evaluated, @element will be replaced by the layer name and the whole string will be concatenated to what you can see in the screenshot: our raster calculator styled expression!

  • We still have an array, but must get a single output per line. Thus we use array_get to get the n-th element. For n, we use row-number with variabel @row_number (line 9): 1st element (=expression for layer-name 1) for line 1, 2nd element for line 2 etc.

  1. Now use autofill again to complete the remaining columns, especially the last one, a path for the Output.

The batch dialog should now look something like this. As you can see, the expression from step one is evaluated and returns a "raster-calculator-styled" expression as output, created individually for each row on the basis of the expression in step 2. If you run it, it will create a raster with log10 in the defined output folder.

enter image description here

1
  • 1
    Amazing - thanks so much for putting the time into both giving an answer and explaining why it works!! Much appreciated. Not sure how you knew how to do that/worked it out, but it worked perfectly. – JohnH May 29 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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