This sample code demonstrates how to convert a raster to a shapefile using a field other than "Value"
How to use
- Start ArcMap and paste the code into VBA.
- Add a thematic raster into ArcMap.
- Make sure the Spatial Analyst Extension is checked.
- Set reference to ESRI GeoAnalyst Object Library.
- Change the field name (sFieldName) and output directory in the code to fit your data.
- Change the output file name (sFileName) if necessary.
- Run the sub from the Macros dialog.
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