Help with iteration with Raster Calculator in Model Builder

2146
2
09-19-2012 11:07 AM
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
Tags (2)
0 Kudos
2 Replies
Helder_Antonio_BentoDa_Costa
New Contributor
Hi Robin,

I have similar problem. I need to convert digital number of a raster dataset into reflectance. Each band has different gains and offset that I need to multiply to make the conversion. I want to run a raster iteration in Model Builder and than take each output and convert them (by multiplying with their respective gains and offset) into reflectance using raster calculator.
Like you said it's easily done with single band raster but I don't know if it can be done on multiple rasters at once.
Let me know if you have found the solution.

Cheers,
Helder
0 Kudos
ChristopherBlinn1
Occasional Contributor III
Robin,

I think using sub-models can get tricky, so I tend to do similar processes strictly in python.  I do however like using ModelBuilder to get my python script going, but for this case it may be best to just use python.

If each Raster you iterate has a unique value to multiply by then there are a couple of hurdles.  First, you could create an array which stores these values.  The trick here would be to have each value in the exact order that the iterate raster tool will work.  To test this, you could create a table and add the name of the raster as a row as it iterates through the workspace.  The resulting table will list the order the rasters will be processed.

In python, iterators are just for loops:

rasters = arcpy.ListRasters()

for raster in rasters:
     Whatever you want to do with each raster


The array would call the value by using the iteration number as the array index number:

rasters = arcpy.ListRasters()
array = array([value1, value 2, value 3... value n])

i = 0

for raster in rasters
     raster * array  (This is not the correct syntax, it is just showing you how the i variable would be called)
     i = i + 1


If implemented correctly, the raster will be multiplied by the correct array index value.  Some work to do so but in theory, should work.

To help you get going, you can use the very same model you have made in your photo, export the python script, edit the script by removing the TBX reference and replacing the iterate rasters tool with the for loop.  Loops from model builder don't translate very well to python.

Hope this helps!
Chris B.
0 Kudos