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"% + ...

Example Model

  • 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. Commented Jul 13, 2013 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. Commented Jul 14, 2013 at 16:35

1 Answer 1


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.

  • 1
    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. Commented Oct 19, 2011 at 23:03
  • 1
    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
    Commented Oct 19, 2011 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.