How to convert a raster to a shapefile using RasterConvertHelper


This sample code demonstrates how to convert a Raster to a shapefile using RasterConvertHelper object.

How to use

  1. Start ArcMap.
  2. Add a raster into ArcMap.
  3. Make sure that the Spatial Analyst or 3D Extension is turned on.
  4. Paste the following code into VBA.
  5. Set a reference to the ESRI GeoAnalyst Object Library.
  6. Run the sub from the Macros dialog.
[VBA]
Sub RasterConvertHelper()
    'Get the focused Map from MapDocument
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    
    'Get the input raster from the first layer in ArcMap
    Dim pLayer As ILayer
    Dim pRasLayer As IRasterLayer
    Dim pInRaster As IRaster
    
    Set pLayer = pMap.Layer(0)
    If Not TypeOf pLayer Is IRasterLayer Then Exit Sub
    
    Set pRasLayer = pLayer
    Set pInRaster = pRasLayer.Raster
    
    'Create a RasterConvertHelper operator
    Dim pRasConvertHelper As IRasterConvertHelper
    Set pRasConvertHelper = New RasterConvertHelper
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = New RasterAnalysis
    pEnv.SetCellSize esriRasterEnvMaxOf
    
    'Perform ToShapefile operation
    Dim pOutFClass As IFeatureClass
    Set pOutFClass = pRasConvertHelper.ToShapefile(pInRaster, esriGeometryAny, pEnv)
    
    'Create a feature layer from output and add it into ArcMap
    Dim pOutFeatureLayer As IFeatureLayer
    Set pOutFeatureLayer = New FeatureLayer
    Set pOutFeatureLayer.FeatureClass = pOutFClass
    pOutFeatureLayer.Name = "My new feature layer"
    pMap.AddLayer pOutFeatureLayer
End Sub