I have a SAGA Raster Calculator algorithm inside a model that I'm building.

For the formula, I'm using the function (g1>20)*1 to give me a binary raster output. This categorises pixels to buildings (1) or ground (0) depending on whether the altitude is 20m or more. This works as expected!

enter image description here

I can link this string expression into a string parameter in my model, and use this when I run my model.

For ease-of-use, I'd rather provide the threshold value of 20 as a numerical input value. So rather than

a string parameter "Minimum Height Expression" of "(g1>20)*1"

It would be something like

a number parameter "Minimum Height" of 20

And the expression inside the algorithm would be something like

(g1>[Minimum Height])*1

Is token subsititution like this possible with Processing? Or is there another way to get around this?

  • 1
    Not sure if that is possible through the Modeler but should be doable if you convert it to a script. If you add in the parameters described here along with a number parameter then your Raster Calculator expression could be something like '(g1>' + number_parameter + ')*1'. – Joseph Oct 28 '15 at 13:19
  • 1
    Thanks @Joseph! That looks promising, I can do a python script to take number inputs and one format string, do the formatting, and output a string... in fact I've found an existing user script which I can use as a starting point for this (Batch String Replace via regex dictionary). I'll write it up when I've implemented it. :) – Steven Kay Oct 28 '15 at 15:13
  • Awesome buddy, I look forward to seeing your script :) – Joseph Oct 28 '15 at 15:17

Thanks to @Joseph, I've found a way to do this. It's inspired by the existing user script "Batch String Replace via regex dictionary".

It takes three numeric parameters (all optional) and three string parameters (again, all optional).

Finally, there's a format parameter, which is a string like this

  • $N (where N is 1,2 or 3) will be replaced with string parameter number N.
  • #N (where N is 1,2 or 3) will be replaced with numeric parameter number N.

Here's the code..

##Format string from multiple numeric and string parameters=name
##String_input_1=string None
##String_input_2=string None
##String_input_3=string None
##Number_input_1=number 0
##Number_input_2=number 0
##Number_input_3=number 0
##Format_string=string $0
##output=output string

import string
import re

# at least, return an empty string
if not Format_string: 
    Format_string = ''

# Set output string
output = Format_string

# replace tokens

dix = { "[$]1":String_input_1,
        "[#]3":Number_input_3 }
for key in dix:
    output = re.sub(key,str(dix[key]),output)

And the dialog...

enter image description here

And here's what it looks like inside a model.

enter image description here


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.