How to finding the boundingbox and the raster files from all rasters in a directory and subdirectories


This sample shows how to get the boundingbox from all rasters in a directory and its subdirecties, it is useful to know the top-left coordinates when creating a mosaic raster dataset in ArcSDE so the pyramid reference point can be set properly for partial pyramid updates to take place, it is also useful when creating a raster catalog which all the rasters will be inserted into, the bounding box can be a good start point for setting a proper x/y domain for the geometry column.

How to use

  1. Add these functions to your project.
  2. Call the top-level function (first one listed) from your code.
[VBA]
'+++ this program finds the bounding files in a directory and its subdirectories+++'
Option Explicit
Private xmin As Double, ymax As Double, xmax As Double, ymin As Double
Private file_x_min As String, file_x_max As String, file_y_min As String, file_y_max As String
Const sInDir = "d:\data"

Sub main()
    'Initialize'
    xmin = 1E+30
    xmax = -1E+30
    ymin = 1E+30
    ymax = -1E+30
    
    ' Loop through the directory and subdirectories
    LoopDir sInDir
    
    ' printout the bounding box and the files
    MsgBox "leftmost file is : " + file_x_min + " == " + xmin + vbNewLine _
        + "topmost file is : " + file_y_max + "==" + ymax + vbNewLine _
        + "rightmost file is : " + file_x_max + "==" + xmax + vbNewLine _
        + "bottom file is : " + file_y_min + "==" + ymin
End Sub

Public Sub LoopDir(sDir As String)
    
    ' loop through a directory and subdirectories
    Dim fso As New FileSystemObject
    Dim pFLD As Folder
    Dim pSubFldr As Folder
    
    ' find the bounding box
    FindBounds sDir
    Set pFLD = fso.GetFolder(sDir)
    
    ' drill down to subfolders
    For Each pSubFldr In pFLD.SubFolders
        Call LoopDir(sDir + "\" + pSubFldr.Name)
    Next pSubFldr
    
    Set fso = Nothing
    Set pFLD = Nothing
    Set pSubFldr = Nothing
    
End Sub

Public Sub FindBounds(sDir As String)
    ' find the bounding box value and the dataset
    Dim pws As IWorkspace
    Dim pWsFact As IWorkspaceFactory
    Set pWsFact = New RasterWorkspaceFactory
    Set pws = pWsFact.OpenFromFile(sDir, 0)
    
    Dim pEnum As IEnumDataset
    Set pEnum = pws.Datasets(esriDTRasterDataset)
    
    Dim pDs As IGeoDataset
    Set pDs = pEnum.Next
    
    Dim pDataset As IDataset
    Set pDataset = pDs
    
    Dim pExt As IEnvelope
    Do While Not pDs Is Nothing
        Set pExt = pDs.Extent
        ' if current xmin is smaller, get the file name
        If xmin > pExt.xmin Then
            xmin = pExt.xmin
            file_x_min = sDir + "\" + pDataset.Name
        End If
        
        ' if current ymax is larger, get the file name for maxy
        If ymax < pExt.ymax Then
            ymax = pExt.ymax
            file_y_max = sDir + "\" + pDataset.Name
        End If
        
        ' if current ymin is smaller, get the file name for miny
        If ymin > pExt.ymin Then
            ymin = pExt.ymin
            file_y_min = sDir + "\" + pDataset.Name
        End If
        
        ' if current xmax is larger, get the file name for maxx
        If xmax < pExt.xmax Then
            xmax = pExt.xmax
            file_x_max = sDir + "\" + pDataset.Name
        End If
        
        Set pDs = pEnum.Next
        Set pDataset = pDs
    Loop
    
    Set pws = Nothing
    Set pEnum = Nothing
    Set pDs = Nothing
    Set pDataset = Nothing
    Set pExt = Nothing
    
End Sub