Select to view content in your preferred language

Using derived variables in the Raster Calculator tool?

1913
2
03-18-2012 12:46 PM
curtvprice
MVP Alum
This is a cross-post from the [post=181728]Geoprocessing forum:[/post]

Hello, I have constructed a model that uses an OLS regression towards the end.  I would like the values for two of the coefficients to be used as part of a raster calculator equation that rebuilds the regression and outputs to a raster.  I have entered the equation that I want to use in the Raster Calculator tool, but cannot figure out how to make the two coefficients from the OLS regression become part of the equation (they are not listed as options under the Layers and Variables section of Raster Calculator).  Part of the problem is that the model is designed to have those two coefficients be different every time the model is run.  Is there any way that I can have the two coefficients become part of the equation?  I have attached a screen shot of the model for clarity.  Any help is greatly appreciated...
[ATTACH=CONFIG]12790[/ATTACH]


There is some missing information here.... I'm assuming your "variables" output from the script tool is a string right? Say it's a space delimited text with two numbers: "234.2 -12.4".

Add two Calculate Value tools to your model (right click / model tools/ calculate value) and set "variables" as a precondition to both so they will run after your OLS script.

The expressions (one for each Calculate Value) would look like this, using the default output type (Variant) would be fine. Note the correct use of quotes and "%" characters in Calculate Value, this is important and took me a while to figure out. (The % substitution happens before any python interpretation happens, the quotes are part of Python syntax designating the values between the quotes compose a text string.)

"%variables%".split()[0]  # python sees "234.2 -12.4".split()[0] (== "234.2")
"%variables%".split()[1]  # == "-12.4"


(BTW, if you use path variables in Calculate Value you must use the syntax r"%input dataset%" to make sure the Windows path back-slashes are interpreted correctly by Python.)

ModelBuilder will give them default names "output_value" and "output_value (2)". Rename them to "regval1" and "regval2".

At this point you can precondition these two on a second calculate value expression, when does python string manipulation to create your map algebra expression, or you can enter the values %regval1% and %regval2% directly into the Map Algebra tool. I'm not sure which will work better in this situation, though I like the second approach better as you can connect raster inputs into the tool which makes for a prettier ModelBuilder presentation.
0 Kudos
2 Replies
MelissaSlater
Emerging Contributor
I am trying to do something very similar to the original posted thread but the variables are not strings within the same field. I have a table with 2 rows and 1 column (plus a header) and I need to utilize the value in each row in a raster calculator. Any suggestions?
Thanks,
Melissa



This is a cross-post from the [post=181728]Geoprocessing forum:[/post]



There is some missing information here.... I'm assuming your "variables" output from the script tool is a string right? Say it's a space delimited text with two numbers: "234.2 -12.4".

Add two Calculate Value tools to your model (right click / model tools/ calculate value) and set "variables" as a precondition to both so they will run after your OLS script.

The expressions (one for each Calculate Value) would look like this, using the default output type (Variant) would be fine. Note the correct use of quotes and "%" characters in Calculate Value, this is important and took me a while to figure out. (The % substitution happens before any python interpretation happens, the quotes are part of Python syntax designating the values between the quotes compose a text string.)

"%variables%".split()[0]  # python sees "234.2 -12.4".split()[0] (== "234.2")
"%variables%".split()[1]  # == "-12.4"


(BTW, if you use path variables in Calculate Value you must use the syntax r"%input dataset%" to make sure the Windows path back-slashes are interpreted correctly by Python.)

ModelBuilder will give them default names "output_value" and "output_value (2)". Rename them to "regval1" and "regval2".

At this point you can precondition these two on a second calculate value expression, when does python string manipulation to create your map algebra expression, or you can enter the values %regval1% and %regval2% directly into the Map Algebra tool. I'm not sure which will work better in this situation, though I like the second approach better as you can connect raster inputs into the tool which makes for a prettier ModelBuilder presentation.
0 Kudos
curtvprice
MVP Alum
I am trying to do something very similar to the original posted thread but the variables are not strings within the same field. I have a table with 2 rows and 1 column (plus a header) and I need to utilize the value in each row in a raster calculator.


The only way you can extract both values for use in a raster calculator expression I can think of is to run two Calculate Value tools that open a cursor and extract a value, using a cursor construct (or just reading the text file with open()) inside python functions:

file variable -> precondition -> Calculate Value (1) -> Value 1   
                \
                 precondition -> Calculate Value (2) -> Value 2   


Then use the variables %Value 1% and %Value 2% in the Raster Calculator. You need two Calculate Value tools because that tool can only return a single value.

Example Calculate Value expression:
f(r"%input file%")


Example code block:
def f(file):
  fil = open(file)
  recs = fil.readlines()
  # extract first item on second line of text file
  # (remove newline, convert to list, get first item)
  return recs[1].strip().split()[0]


From a file like this:
field
1
2


the above function will return "1".

WARNING - if you return zero, this is evaluated as False and the processing chain will halt. So I hope you don't need zero. You can work around this by transforming the value (say, add one) and transforming it back (subtracting one) in your map algebra expression.
0 Kudos