Raster Calculator - add in line variable

3164
5
04-25-2012 04:09 AM
jamiefinney
New Contributor III
got my self in a muddle while trying to automate a tool that works for a single point. now trying to automate for many points.

%ID% - the point number i'm using (these range from 1-70)
%max% - a number

in the raster calculator i am making dtm values less than %max% into null values using the following

SetNull("%dtm_1%" < %min%,"%dtm_1%")

This worked for my example when i used dtm_1, however i now want to run this for dtm_1, dtm_2, dtm_3... using dtm_%ID%

if i try SetNull("%dtm_%ID%%" < %min%,"%dtm_%ID%%") i get an error message


Start Time: Wed Apr 25 13:07:51 2012
SetNull("%dtm_%ID%%" < 69.555,"%dtm_%ID%%")
ERROR 000539: Error running expression: rcexec() <type 'exceptions.RuntimeError'>: ERROR 000732: Input Raster: Dataset %dtm_%ID%% does not exist or is not supported
Failed to execute (Raster Calculator).
Failed at Wed Apr 25 13:07:52 2012 (Elapsed Time: 1.00 seconds)

how do i get this to work properly?

thanks in advance
0 Kudos
5 Replies
SimonMitchell1
New Contributor
Hi Jamie,

I am getting the same error when trying to do a similar calculation involving in line variables.
Similar to you, when only using one input raster, everything is fine.
Only when trying to run with multiple inputs do I get the error (I am using a for loop).
My first thought was around the placement of the %, e.g. having double %% in the line.

I tried moving the variable to the middle of the name, but with no luck.

Con(IsNull("%Rand_Pt%n%_flt%"),0,("%Rand_Pt%n%_flt%"*5))
ERROR 000539: Error running expression: rcexec() <type 'exceptions.RuntimeError'>: ERROR 000732: Input Raster: Dataset %Rand_Pt%n%_flt% does not exist or is not supported
Failed to execute (Raster Calculator).

If you have, or anyone else has, a solution, I would much appreciate a response.

Many thanks

SM
0 Kudos
DuncanHornby
MVP Frequent Contributor
I don't think you can have an in-line substitution within an in-line substitution.  So for example if you had:

%name% = "fred"
%id% = "33"

Then a valid output name would be %name%_%id% which would create fred_33

An invalid scenario would be %name%id%% to correct this do %name%%id%

Duncan
0 Kudos
curtvprice
MVP Esteemed Contributor
got my self in a muddle while trying to automate a tool that works for a single point. now trying to automate for many points.

%ID% - the point number i'm using (these range from 1-70)
%max% - a number

in the raster calculator i am making dtm values less than %max% into null values using the following

SetNull("%dtm_1%" < %min%,"%dtm_1%")


This worked for my example when i used dtm_1, however i now want to run this for dtm_1, dtm_2, dtm_3... using dtm_%ID%

if i try SetNull("%dtm_%ID%%" < %min%,"%dtm_%ID%%")


i get an error message

Start Time: Wed Apr 25 13:07:51 2012
SetNull("%dtm_%ID%%" < 69.555,"%dtm_%ID%%")
ERROR 000539: Error running expression: rcexec() <type 'exceptions.RuntimeError'>: ERROR 000732: Input Raster: Dataset %dtm_%ID%% does not exist or is not supported
Failed to execute (Raster Calculator).
Failed at Wed Apr 25 13:07:52 2012 (Elapsed Time: 1.00 seconds)


how do i get this to work properly?


The other posters are right, you cannot "nest" these things. The easiest approach I can think of using is to use the Calculate Value tool with this expression; set the output to be a raster calculator expression, and connect it to the Raster Calculator tool:

'SetNull("{0}_{1} < 69.555, "{0}_{1}")'.format("%dtm_%",%ID%)


I'm assuming you have model elements named "dtm_" and "ID" that will be used in the above expression at runtime, creating a string like:

# %dtm_% = "raster"
# %ID% = 1
SetNull("raster1" < 69.555, "raster1")


Don't forget if all these rasters are in a folder, you can use Iterate Rasters to generate the values of "raster", then you could use a simpler expression like

'SetNull("{0}" < 69.555, "{0}")'.format(r"%raster%")


(the 'r' prefix is required because Iterate Rasters generates pathnames with backslashes that must not be misinterpreted as escape codes)

Hope this helps...
0 Kudos
SimonMitchell1
New Contributor
Hello Duncan and Curt,

Thank you for your responses.
Although I am not sure how to apply it to my situation (I am not an expert).

What I failed to mention in my post is that the input raster for the Raster Calculator is an output from another function within my for-loop iteration.
My process is:
FOR (n=1 to 20)
1. Generate a random point (Output: Rand_Pt_%n%)
2. Convert random point to a raster (Output: Rand_Pt_Rast_%n%)
3. Use (2) as input into the raster calculator.

Ideally, I would like to keep the intermediate steps, and why I am stuck with the error.
But I can work around it by not giving the output from (2) the variable in the name (unfortunately, this will mean overwriting).

Many thanks for any solutions that you can give me.

SM
0 Kudos
curtvprice
MVP Esteemed Contributor
Have you tried renaming the variable Rand_pt_Rast_%n% (which it is named by default when you set it up) to a legal name without the % signs? (The variable's value will be unchanged when you edit its name.)

Right click the variable, Rename, edit to: Rand_pt_rast_n.

After you validate the model (click the check mark in ModelBuilder), you should be able pick "Rand_pt_rast_n" from the list in the Raster Calculator tool dialog.

Hope this helps!
0 Kudos