English

How To: use IInterpolationOp::IDW in Spatial Analyst Extension

Summary

This VBA code sample demonstrates how to use the IDW method on IInterpolationOp.

Procedure

 

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    Note:
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy the below code into the UIButtonControl's click event.
Code:
 
''Get the first layer in the ActiveView (point)
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pMap As IMap
Set pMap = pMxDoc.ActiveView

Dim pFLayer As IFeatureLayer
Set pFLayer = pMap.Layer(0)

Dim pFClass As IFeatureClass
Set pFClass = pFLayer.FeatureClass

Dim pAV As IActiveView
Set pAV = pMap
Dim pREnv As IEnvelope
Set pREnv = pAV.Extent

''************
' Create FeatureClassDescriptor using a value field
Dim pFDescr As IFeatureClassDescriptor
Set pFDescr = New FeatureClassDescriptor

''' This string "Elevation" must be a field in your featureclass
'' this value will be used to interpolate.  Or substitute a field
'' name that does exist.
pFDescr.Create pFClass, Nothing, "Elevation"

' Create RasterInterpolationOp object
Dim pIntOp As IInterpolationOp
Set pIntOp = New RasterInterpolationOp

' Set cell size for output raster. The extent of the output raster is
' defualted to as same as input. The output working directory uses Default
Dim pEnv As IRasterAnalysisEnvironment
Set pEnv = pIntOp

pEnv.SetCellSize esriRasterEnvValue, pREnv.Width / 250
pEnv.SetExtent esriRasterEnvValue, pREnv

' Create raster radius using variable distance
Dim pRadius As IRasterRadius
Set pRadius = New RasterRadius
pRadius.SetVariable 12

' Using FeatureClassDescriptor as an input to the IInterpolationOp and
' Perform the interpolation
Dim pOutRaster As IRaster
Set pOutRaster = pIntOp.IDW(pFDescr, 2, pRadius)

Dim strData As String
'''Notice the GRID will be named "MyIDW"
strData = "C:\temp\MyIDW"
Call createFileFromRaster(pOutRaster, strData)
  1. Copy the below code into the same module as the UIButtonControl.
Code:

Sub createFileFromRaster(pRaster As IRaster, OutFN As String)

   Dim prasterBandC As IRasterBandCollection
   Set prasterBandC = pRaster
   prasterBandC.SaveAs OutFN, Nothing, "GRID"

End Sub
  1. Click Tools > References. Click Browse and navigate to the ESRI Spatial Analyst Shared Object Library and check it on.
     
    Note:
    The library must be added, or the procedure will not work.
  2. Click AddData to load a point FeatureClass into ArcMap.
  3. Modify the line of code that calls the Create method on IFeatureClassDescriptor.
    Provide the name of the field that contains the Z-values to be interpolated.
  4. Click the button to run your code.