ndenahan

VBA IRasterModel help

Discussion created by ndenahan on Apr 13, 2011
Latest reply on Apr 15, 2011 by Hornbydd
I trying to use variables in an iRasterModel.script
Is it possible to do? and what what would be the syntax?

I trying to create a script to let me input raster layers with weight and have the script to all the calculations. The problem I'm having is I can't figure out how to include the weighting. I'm using an iRasterModel to do the calculations. Because the I'm using inputs for the weighting I need to do the calculation using a variable as double.

Thanks for any help


'Check Inputs
'Dimension Variables
Dim dblTotalweight As Double
dblTotalweight = lblTotalResult

'Check Weight Total
If dblTotalweight <> 100 Then
MsgBox "Total weight must equal 100%"
Exit Sub
End If

'Get Map
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

Dim pRasterWetland As IRaster
Set pRasterWetland = returnIRaster(cmbWetlandInput.Text)

Dim pRasterRoad As IRaster
Set pRasterRoad = returnIRaster(cmbRoadInput.Text)

Dim pRasterTline As IRaster
Set pRasterTline = returnIRaster(cmbTlineInput.Text)

Dim pRasterSchool As IRaster
Set pRasterSchool = returnIRaster(cmbSchoolInput.Text)

Dim pRasterParcel As IRaster
Set pRasterParcel = returnIRaster(cmbParcelInput.Text)

Dim pRasterBird As IRaster
Set pRasterBird = returnIRaster(cmbBirdInput.Text)

Dim pRasterConservation As IRaster
Set pRasterConservation = returnIRaster(cmbConservationInput.Text)

'Calculation

'Creat Raster Model object
Dim pRasterModel As IRasterModel
Set pRasterModel = New RasterModel

'Set output workspace
Dim pEnvironment As IRasterAnalysisEnvironment
Set pEnvironment = pRasterModel

Dim pWorkspace As IWorkspace
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New RasterWorkspaceFactory
Set pWorkspace = pWorkspaceFactory.OpenFromFile("K:\URP6271.20111\Sections\4277\Students\Denahan,Nathan B\Final_Project\Data\Raster", 0)
Set pEnvironment.OutWorkspace = pWorkspace

'Bind the input Raster
pRasterModel.BindRaster pRasterWetland, "Wetland"
pRasterModel.BindRaster pRasterRoad, "Road"
pRasterModel.BindRaster pRasterTline, "Tline"
pRasterModel.BindRaster pRasterSchool, "School"
pRasterModel.BindRaster pRasterParcel, "Parcel"
pRasterModel.BindRaster pRasterBird, "Bird"
pRasterModel.BindRaster pRasterConservation, "Conservation"

'set Weights as Numbers
Dim dblWetlandMath As Double
dblWetlandMath = txtWetlandWeight.Value / 100

Dim dblRoadMath As Double
dblRoadMath = txtRoadWeight.Value / 100

Dim dblTlineMath As Double
dblTlineMath = txtTlineWeight.Value / 100

Dim dblSchoolMath As Double
dblSchoolMath = txtSchoolWeight.Value / 100

Dim dblParcelMath As Double
dblParcelMath = txtParcelWeight.Value / 100

Dim dblBirdMath As Double
dblBirdMath = txtBirdWeight.Value / 100

Dim dblConservationMath As Double
dblConservationMath = txtConservationWeight.Value / 100

'Specify map algebra expression
'HERE I NEED TO ADD VARIABLES SOMETHING LIKE
'.SCRIPT = "[output] = ([Wetland] * dblWetlandMath) + ([road] * dblRoadMath)
' etc...
pRasterModel.Script = "[output] = [Wetland] + [road]"

'Execute Expression
pRasterModel.Execute

'Get output
Dim pOutputRaster As IRaster
Set pOutputRaster = pRasterModel.BoundRaster("output")

'Unbind input Rasters
pRasterModel.UnbindSymbol "Wetland"
pRasterModel.UnbindSymbol "Road"
pRasterModel.UnbindSymbol "Tline"
pRasterModel.UnbindSymbol "School"
pRasterModel.UnbindSymbol "Parcel"
pRasterModel.UnbindSymbol "Bird"
pRasterModel.UnbindSymbol "Conservation"

Dim pOutputRasterLayer As IRasterLayer
Set pOutputRasterLayer = New RasterLayer
pOutputRasterLayer.CreateFromRaster pOutputRaster

pMap.AddLayer pOutputRasterLayer
pMxDoc.ActivatedView.Refresh
pMxDoc.UpdateContents

End Sub

Outcomes