Different input values for raster calculator

432
5
09-18-2012 01:59 PM
RobinCheskin
New Contributor III
Hello all,

I am building a sub-model that needs to take a raster from a dataset and multiply it by a value that I input into the model as a parameter.  This is easy to do with a single raster, but I want to iterate through multiple rasters in a dataset, and for each individual raster I need to multiply it by a different value, e.g. Raster 1 * 2.5, Raster 2 * 4.7, etc...

The values need to be parameters that I can input each time I run the model.  The only thing I have come up with so far is to make the value a parameter, but the model will use that value for every raster in the dataset (see image).

[ATTACH=CONFIG]17788[/ATTACH]

Can I write a python script that prompts each value before the model runs?  Or is this doable through model builder?

Thanks,
Robin
0 Kudos
5 Replies
JamieKass
Occasional Contributor
Add and link in the CalculateValue tool into your chain between BinRaster and RasterCalculator, and paste this code inside. The Data Type should be "Double".

Expression:
rasterVal(BinRaster)


Code Block:
def rasterVal(BinRaster):
 if BinRaster == 'rastername1':
  return val1
 elif BinRaster == 'rastername2':
  return val2


.... and so on. The 'val1', etc variables should be entered as numbers, not strings. This code assumes that the "BinRaster" product is a string name. If not, use "arcpy.Describe(BinRaster).name", which will give you the name of the raster. You'll need to hardcode in the multipliers and raster names each time you want to change them, but this is an easy solution.
0 Kudos
RobinCheskin
New Contributor III
Thanks for the advice Kass.  While this will certainly work, I would prefer not to hardcode the raster names and/or raster multipliers in the python code block window every time I run the model.  Can I enter them into something easier to use?  This needs to be usable by a wide variety of audiences (its a tool for predicting crime).

Can you think of a more elegant solution?

Thanks,
Robin
0 Kudos
RobinCheskin
New Contributor III
So I tried this in the Code Block
def rasterVal (BinRaster):
  if BinRaster == 'Bin_Rec_VMC_JAS_10':
    return 1.866
  elif BinRaster == 'Bin_Rec_CADSHJAS10':
    return 1.634


Keep getting the same error that BinRaster is not defined. I then tried arcpy.Describe(BinRaster).name in the second line replacing BinRaster, but I get the same error...
0 Kudos
JamieKass
Occasional Contributor
Sorry, it's been a while since I've used ModelBuilder. The correct syntax for the expression is:
rasterVal("%Name%")


The code block can remain the same, as the variable in the function definition can be anything you choose. This should work.
0 Kudos
RobinCheskin
New Contributor III
Thanks for the assistance Kass, it worked! 🙂
0 Kudos