foofighters

Problems to rewrite a VBA script to Python

Discussion created by foofighters on Mar 15, 2011
Latest reply on Mar 18, 2011 by seanstock
Hello,

maybe someone can help me to rewrite a VBA script to python. I'am new to this whole programming thing and I really need some help.

So I have a folder with several hundred image tiles, and an index.shp file that has a coordinate that represents the centroid of each image tile. In the attribute table for the index.shp, there is a field called Hyperlink, which specifies the full folder path and filename of each image on my hard disk.

What I am looking for is a tool/script that can look at which features of the index.shp I have manually selected, and then load the appropriate image tiles into ArcMap. I found this script below, but i need this script for python. And there are some problems to fix:
  
     - The shape file needs to be the top-most layer, but each time I run the python macro the newly loaded image is inserted on top. I then have to re-arrange the layers to run it again.

     - It is only possible to load one raster at the time. But I need several more rasters.


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

I hope someone can help me!

Thanks in advance

Outcomes