load raster data through path

Discussion created by FooFighters on Mar 25, 2011
Latest reply on Mar 31, 2011 by VPrasad-esristaff
Hi community,

I need some help... I have a VBA script that can load raster data with the path in the attribute table. It looks like:
Private Sub LoadImageForSelectedFeat()

    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pFeatureLayer As IFeatureLayer
    Dim pFeatureSelection As IFeatureSelection
    Dim pFeatureCursor As IFeatureCursor
    Dim pSelectedFeature As IFeature
    Dim pSelectionSet As ISelectionSet
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Set pFeatureLayer = pMap.Layer(0) ' Layer index of the shape file
    Set pFeatureSelection = pFeatureLayer
    Set pSelectionSet = pFeatureSelection.SelectionSet
    pSelectionSet.Search Nothing, False, pFeatureCursor
    Set pSelectedFeature = pFeatureCursor.NextFeature
    'I assume that you have single feature selected
    If Not pSelectedFeature Is Nothing Then
        Dim strFullPath As String
        strFullPath = pSelectedFeature.Value(pSelectedFeature.Fields.FindField("LOCATION"))  ' You can specify the field name from where you accessing the path
        Dim strPath As String
        Dim strFilename As String
        Dim strArray() As String
        Dim intI As Integer
        strArray = Split(strFullPath, "\")
        strFilename = strArray(UBound(strArray))
        strPath = Mid(strFullPath, 1, Len(strFullPath) - Len(strFilename))
        Dim pWorkspaceFactory As IWorkspaceFactory
        Set pWorkspaceFactory = New RasterWorkspaceFactory
        Dim pRasterWorkspace As IRasterWorkspace
        Set pRasterWorkspace = pWorkspaceFactory.OpenFromFile(strPath, 0)
        Dim pRasterDataset As IRasterDataset
        Set pRasterDataset = pRasterWorkspace.OpenRasterDataset(strFilename)
        Dim pRasterLayer As IRasterLayer
        Set pRasterLayer = New RasterLayer
        pRasterLayer.CreateFromDataset pRasterDataset
        pMap.AddLayer pRasterLayer
    End If
End Sub

The problem is that I'm new to scripting and I need some additional functionality:

    - This script is only able to load one selected raster layer at the time. It will be much better for      
      me to load several layers at the same time.

    - The feature layer with the path has to be the first layer in the layer index. But if I load a       
       raster, this is the first layer in the layer index, so I have to put my layer back on the top of
       the layer index.

I hope you can understand the problems. Maybe someone will find a solution to this.

Thanks in Advance