How to create a raster attribute table with joined fields


This sample shows how to join a table to a raster attribute table for a given raster dataset and save it out to a new raster dataset. The raster dataset can be file-based raster dataset or geodatabase raster dataset.

How to use

  1. Call this procedure from VBA.
[VBA]
Sub join_vat(pRasterDataset As IRasterDataset, ptable As ITable, pOutWs As IWorkspace)
    
    Dim praster As IRaster2
    Set praster = pRasterDataset.CreateDefaultRaster
    
    ' ++ Get raster attribute table
    
    Dim pAttTable As ITable
    Set pAttTable = praster.AttributeTable
    
    ' ++ Create the MemoryRelationshipClass that defines what is to be joined
    
    Dim pMemRelClassFact As IMemoryRelationshipClassFactory
    Set pMemRelClassFact = New MemoryRelationshipClassFactory
    Dim pRelClass As IRelationshipClass
    Set pRelClass = pMemRelClassFact.Open("join_vat", ptable, _
                    "value", pAttTable, "value", "forward", "backward", esriRelCardinalityOneToOne)
    
    ' ++ Perform the join
    
    Dim pRelQueryTableFact As IRelQueryTableFactory
    Dim pRelQueryTab As ITable
    Set pRelQueryTableFact = New RelQueryTableFactory
    Set pRelQueryTab = pRelQueryTableFact.Open(pRelClass, True, Nothing, Nothing, "", True, True)
    
    ' ++ Save the raster to a new raster dataset
    
    Dim pSaveAs As ISaveAs
    Set pSaveAs = praster
    Set praster.AttributeTable = pRelQueryTab
    
    pSaveAs.saveas "vat_join.img", pOutWs, "IMAGINE Image"
    
End Sub