How to reclassify a raster using a numeric field


This sample code demonstrates i) how to reclassify a raster using a numeric field, ii) how to use RasterReclassOp, NumberRemap and RasterDescriptor objects

How to use

  1. Start ArcMap and add a raster dataset that has a numeric field.
  2. Paste the code into VBA.
  3. Make sure the Spatial Analyst extension is checked.
  4. Add reference to ESRI SpatialAnalyst Object Library.
  5. Add reference to ESRI GeoAnalyst Object Library.
  6. Change field name (sFieldName) and remap string (pNumberRemap)in the code.
  7. Run the sub from the Macros dialog.
[VBA]
Sub ReclassifyByNumericField()
    'Get the focused map from MapDocument
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    
    'Get the input raster from the first layer in ArcMap
    Dim pLayer As ILayer
    Set pLayer = pMap.Layer(0)
    If Not TypeOf pLayer Is IRasterLayer Then
        Exit Sub
    End If
    
    Dim pRasLayer As IRasterLayer
    Set pRasLayer = pLayer
    Dim pInRaster As IRaster
    Set pInRaster = pRasLayer.Raster
    
    'Create a raster descriptor and specify the field to be used for reclassify
    Dim sFieldName As String
    sFieldName = "Density"
    
    Dim pRasDescriptor As IRasterDescriptor
    Set pRasDescriptor = New RasterDescriptor
    pRasDescriptor.Create pInRaster, Nothing, sFieldName
    
    'Create a RasterReclassOp operator
    Dim pReclassOp As IReclassOp
    Set pReclassOp = New RasterReclassOp
    
    'Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pReclassOp
    Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("c:\temp", 0)
    Set pEnv.OutWorkspace = pWS
    
    'Create a NumberRemap object and specify remap
    Dim pNumberRemap As INumberRemap
    Set pNumberRemap = New NumberRemap
    pNumberRemap.MapRange 0, 10, 1
    pNumberRemap.MapRange 10, 20, 2
    pNumberRemap.MapRange 20, 30, 3
    pNumberRemap.MapRange 30, 40, 4
    pNumberRemap.MapRange 40, 100, 5
    
    'Reclassify the input raster
    Dim pOutRaster As IRaster
    Set pOutRaster = pReclassOp.ReclassByRemap(pRasDescriptor, pNumberRemap, False)
    
    'Add output into ArcMap as a raster layer
    Dim pOutRasLayer As IRasterLayer
    Set pOutRasLayer = New RasterLayer
    pOutRasLayer.CreateFromRaster pOutRaster
    pMap.AddLayer pOutRasLayer
End Sub