How to convert a raster to a shapefile using RasterConversionOp and RasterDescriptor


This sample code demonstrates how to convert a raster to a shapefile using a field other than "Value"

How to use

  1. Start ArcMap and paste the code into VBA.
  2. Add a thematic raster into ArcMap.
  3. Make sure the Spatial Analyst Extension is checked.
  4. Set reference to ESRI GeoAnalyst Object Library.
  5. Change the field name (sFieldName) and output directory in the code to fit your data.
  6. Change the output file name (sFileName) if necessary.
  7. Run the sub from the Macros dialog.
[VBA]
Sub RasterToFeatureUsingRD()
    '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
    End If
    
    Set pRasLayer = pLayer
    Set pInRaster = pRasLayer.Raster
    
    'Create RasterDecriptor
    Dim sFieldName As String
    sFieldName = "Landuse" 'change field in raster table
    
    Dim pRasDescriptor As IRasterDescriptor
    Set pRasDescriptor = New RasterDescriptor
    pRasDescriptor.Create pInRaster, Nothing, sFieldName
    
    'Create a RasterConversionOp operator
    Dim pConversionOp As IConversionOp
    Set pConversionOp = New RasterConversionOp
    
    'Create workspace for shapefile output
    Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New ShapefileWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("c:\temp", 0)
    
    'Convert Raster to a polygon shapefile
    Dim sFileName As String
    sFileName = "ConOut.shp" 'change name of the output shapefile
    
    Dim pOutFClass As IFeatureClass
    Set pOutFClass = pConversionOp.RasterDataToPolygonFeatureData(pRasDescriptor, pWS, sFileName, True)
    
    'Add output into ArcMap as a feature layer
    Dim pOutFLayer As IFeatureLayer
    Set pOutFLayer = New FeatureLayer
    Set pOutFLayer.FeatureClass = pOutFClass
    pOutFLayer.Name = sFileName
    pMap.AddLayer pOutFLayer
End Sub