How to compute the statistics of a raster band collection using RasterMultivariateOp


This sample code demonstrates how compute the statistics of a raster band collection using IMultivariateOp::BandCollectionStats.

How to use

  1. Paste the code into VBA.
  2. Make sure the Spatial Analyst extension is checked.
  3. Add reference to ESRI SpatialAnalyst Object Library.
  4. Add reference to ESRI GeoAnalyst Object Library.
  5. Modify output workspace and the output file name in the code.
  6. Call the sub ComputeBandCollectionStatistics from another sub or function and pass the raster band collection.
[VBA]
Sub ComputeBandCollectionStatistics(pRasBandColl As IRasterBandCollection)
    'Calculates the statistics for a raster band collection.
    
    On Error GoTo ErrorHandler
    
    'Create a MultivariateObject
    Dim pMultivariateOp As IMultivariateOp
    Set pMultivariateOp = New RasterMultivariateOp
    
    'Open a RasterWorkspace
    Dim pWSF As IWorkspaceFactory
    Dim pWS As IWorkspace
    Dim pRasWS As IRasterWorkspace
    
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("C:\temp\myworkspace", 0)
    Set pRasWS = pWS
    
    'Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pMultivariateOp
    Set pEnv.OutWorkspace = pRasWS
    
    'Compute band collection statistics
    Dim sStatOutFileName As String
    sStatOutFileName = "BandCollStats.txt"
    pMultivariateOp.BandCollectionStats pRasBandColl, sStatOutFileName, True
    
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub