Select to view content in your preferred language

load raster data through path

548
5
03-25-2011 11:25 AM
JannWendt
Emerging Contributor
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
       
        pMxDoc.UpdateContents
        pMxDoc.ActiveView.Refresh
    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
Jann
0 Kudos
5 Replies
VivekPrasad
Deactivated User
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:


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
Jann


Hi,

Please test the below modified code. You just need to change the layer name at the specified line.

Private Sub LoadImageForSelectedFeat()

    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pLayer As ILayer
    Dim pLayer_Desired As ILayer
    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
   
    Dim intLayerCount As Integer
    For intLayerCount = 0 To pMap.LayerCount - 1
        Set pLayer = pMap.Layer(intLayerCount)
        If pLayer.Name = "ABC" Then ' Change the layer name from ABC as per your layer name
            Exit For
        End If
    Next
   
    If TypeOf pLayer Is IFeatureLayer Then
        Set pFeatureLayer = pLayer
    End If
   
    Set pFeatureSelection = pFeatureLayer
    Set pSelectionSet = pFeatureSelection.SelectionSet
   
    pSelectionSet.Search Nothing, False, pFeatureCursor
    Set pSelectedFeature = pFeatureCursor.NextFeature
   
    While Not pSelectedFeature Is Nothing
        '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
       
        Set pSelectedFeature = pFeatureCursor.NextFeature
    Wend
   
    pMxDoc.UpdateContents
    pMxDoc.ActiveView.Refresh
    'End If

End Sub
0 Kudos
JannWendt
Emerging Contributor
Thanks it works perfect!!
0 Kudos
VivekPrasad
Deactivated User
Coool....!!!
0 Kudos
JannWendt
Emerging Contributor
I tested it today and it works really good, but there is one more wish I have. The rasters are very big and they need pyramids because of their size. In most cases they don't have pyramids. Is it possible to add some code to the script which build pyramids, when the selected raster don't have any?

Thanks for your help!
0 Kudos
VivekPrasad
Deactivated User
I tested it today and it works really good, but there is one more wish I have. The rasters are very big and they need pyramids because of their size. In most cases they don't have pyramids. Is it possible to add some code to the script which build pyramids, when the selected raster don't have any?

Thanks for your help!


Hi,

If you have not come across please have a look at the below link.

http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriDatasourcesRaster/Raster_Pyramids_NET.htm
0 Kudos