Multiply two or more rasters

317
2
05-10-2011 04:49 PM
MeghanAlexander1
New Contributor
Dear All,

I need to know how i can multiply more than 2 rasters. I have the code for times two rasters (see below) but when i declare a third inputdataset i get a compile error; 'wrong number of arguments or invalid property assignment'. I'm working in 9.3.

Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap

   Dim pWSFact As IWorkspaceFactory

  Set pWSFact = New AccessWorkspaceFactory
  Dim pWS2 As IRasterWorkspaceEx
  Set pWS2 = pWSFact.OpenFromFile(databasepath.Caption, 0)

 
 
   Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("F:\PhD\GIS\Temp", 0)


'Create the MathOps object
Dim pMathOp As IMathOp
Set pMathOp = New RasterMathOps



' Declare the input geoDataset1 object
Dim pinputdataset1 As IRasterDataset
Set pinputdataset1 = pWS2.OpenRasterDataset("elderlyh")

Dim pInputDataset2 As IRasterDataset
Set pInputDataset2 = pWS2.OpenRasterDataset("elderly")


    'Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pMathOp

  
    Set pEnv.OutWorkspace = pWS

    'Perform the operation
    Dim pOutRaster As IRaster
    Set pOutRaster = pMathOp.Times(pinputdataset1, pInputDataset2)

    'Create a raster layer and add it into ArcMap
    Dim pOutRasLayer As IRasterLayer
    Set pOutRasLayer = New RasterLayer
    pOutRasLayer.CreateFromRaster pOutRaster
    pOutRasLayer.name = "VulnScore"
    pMap.AddLayer pOutRasLayer


End Sub
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
The Times method will only take two inputs, so what you'll have to do is create a new pMathOp and use pOutRaster as the first input and the third raster as the second input.
0 Kudos
MeghanAlexander1
New Contributor
Thank you Ken, I've figured this out now.

I have another issue: can soneone tell me how to create a loop for declaring the inputdatasets - i have a user form whereby the user selects the importance of the raster in question in terms of their decision making (i.e. by ticking a checkbox), a raster may be selected as 'no importance' - in which case, I do not want it to be included within the mathop. How can I write the code to include a loop for this?

**************************
Private Sub CommandButton50_Click()
Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap

   Dim pWSFact As IWorkspaceFactory

  Set pWSFact = New AccessWorkspaceFactory
  Dim pWS2 As IRasterWorkspaceEx
  Set pWS2 = pWSFact.OpenFromFile(databasepath.Caption, 0)

 
 
   Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("F:\PhD\GIS\Temp", 0)


'Create the MathOps object
Dim pMathOp As IMathOp
Set pMathOp = New RasterMathOps

'*****

If Eh.Value = True Then
Set pinputdataset = pWS2.OpenRasterDataset("elderlyh")
ElseIf Eh.Value = False Then
End If
If Em.Value = True Then
Set pinputdataset = pWS2.OpenRasterDataset("elderlym")
ElseIf Em.Value = False Then
End If
If El.Value = True Then
Set pinputdataset = pWS2.OpenRasterDataset("elderly")
End If

'declare input dataset2
If Lh.Value = True Then
Dim pInputDataset2 As IRasterDataset
Set pInputDataset2 = pWS2.OpenRasterDataset("parenth")
ElseIf Lh.Value = False Then
End If
If Lm.Value = True Then
Set pInputDataset2 = pWS2.OpenRasterDataset("parentm")
ElseIf Lm.Value = False Then
End If
If Lo.Value = True Then
Set pInputDataset2 = pWS2.OpenRasterDataset("parent")
End If

'declare input dataset3
If Vh.Value = True Then
Dim pInputDataset3 As IRasterDataset
Set pInputDataset3 = pWS2.OpenRasterDataset("carh")
ElseIf Vh.Value = False Then
End If
If Vm.Value = True Then
Set pInputDataset3 = pWS2.OpenRasterDataset("carm")
ElseIf Vm.Value = False Then
End If
If Vl.Value = True Then
Set pInputDataset3 = pWS2.OpenRasterDataset("car")
End If


   'Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pMathOp
    Set pEnv.OutWorkspace = pWS

Dim pOutRaster As IRaster
Dim pOutRaster2 As IRaster
Set pOutRaster = pMathOp.Times(pinputdataset1, pInputDataset2)
Set pOutRaster2 = pMathOp.Times(pOutRaster, pInputDataset3)


'Create a raster layer and add it into ArcMap
Dim pOutRasLayer As IRasterLayer
Set pOutRasLayer = New RasterLayer
pOutRasLayer.CreateFromRaster pOutRaster2
pOutRasLayer.name = "VulnScore"
pMap.AddLayer pOutRasLayer
0 Kudos