# Raster Calculator dynamic map algebra expression

Is it possible to dynamically construct a Map Algebra expression that is dependent on the output of another tool, either using model builder or arcpy? For example, say I have a model that performs a raster reclassification that has a list of raster inputs and their respective outputs, i.e.

rasterOne -> reclassification -> outputRasterOne

rasterTwo -> reclassification -> outputRasterTwo

EDIT: To clarify, the number of input rasters is not known and therefore the number and names of the output rasters are not either. Because of this, I cannot hardcode them into the map algebra expression.

I would then want the map algebra expression to be similar to:

``````%"outputRasterOne"% + %"outputRasterTwo"% + ...
`````` • I am stuck with the similar problem but couldn't understand how to implement the FOR loop in the map algebra. Can someone revise the answer please. – Uttam Paudel Jul 13 '13 at 15:26
• It would be advisable to ask a new question and provide a link to this one. You should provide your current code for the best response. – Baskinomics Jul 14 '13 at 16:35

I've accomplished this in .NET by declaring optional inputs and then using an IF statement to decide whether or not to add additional rasters.

``````      Public Function CalculateMapAlgebra(ByVal sMapAlgebra As String, ByVal R1 As IGeoDataset, Optional ByVal R2 As IGeoDataset = Nothing, Optional ByVal R3 As IGeoDataset = Nothing) As IRaster
.
.
.
.
'Bind a raster
pMapAlgebraOp.BindRaster(R1, "R1")
If Not R2 Is Nothing Then
pMapAlgebraOp.BindRaster(R2, "R2")
End If

If Not R3 Is Nothing Then
pMapAlgebraOp.BindRaster(R3, "R3")
End If

Dim rasOut As IRaster = pMapAlgebraOp.Execute(sMapAlgebra)

return rasOut

End Function
``````

You have to do some fiddling with the input Map Algebra string (sMapAlgebra in my example) so that the optional parameters are either included or excluded. I do this outside of the method above, but you could create the string based on the IF statement. If you have a known limit to the number of input rasters (e.g. 3) you could just create 3 possible strings and choose one based on the number of actual parameters. However, this solution becomes bulky if you have many (say... > 10) possible input rasters.

In the same way you could determine the number of output parameters (e.g. IF you have 3 input rasters create 3 associated output rasters, maybe by appending "out" to the raster name, e.g. "R1_out" in my example. You could just create these inside of the conditional statement.

• The number of input rasters will not be known. To be more precise, how can I use a conditional statement using ArcPy or ModelBuilder to construct the map algebra expression. – Baskinomics Oct 19 '11 at 23:03
• You could get a count of the number of rasters when the script is to be run and then write a statement to write the map algebra calculation string(e.g. append output paths to a string) using a FOR loop. For example, 'for each raster create a complimentary output raster and append its path to the MapAlgebra string'. – Radar Oct 19 '11 at 23:11