Raster Calculator Expressions – Creating Dynamic Map Algebra Expressions in Raster Calculator

arcgis-10.0arcpygeoprocessingmodelbuilder

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

Best Answer

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.