This document is archived and information here might be outdated.  Recommended version.


How to find the top left extent of the rasters in a folder and its subfolders (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Managing data > Working with image and raster data > Processing raster data > Geodata transformations > How to find the top left extent of the rasters in a folder and its subfolders (ArcObjects .NET 10.6 SDK)

How to find the top left extent of the rasters in a folder and its subfolders


Summary
When creating a raster dataset in a file or an ArcSDE geodatabase, it is important to set the pyramid origin correctly to take advantage of the partial pyramid updating feature. This topic shows how to loop through all the rasters in a folder and its subfolders and find the top left point of the union extent. You can use this point to set the pyramid origin point when creating a raster dataset in a file or an ArcSDE geodatabase and mosaic all the rasters into it.

Finding the top left extent of the rasters in a folder and its subfolders

To find the top left extent of the rasters in a folder and its subfolders, follow these steps:
  1. Create a GetTopLeft function, which takes a folder path as input and creates the initial top left extent coordinates in double.
  2. Initialize the geoprocessor.
  3. Create the GetRasterProperties object and GPUtilities class.
  4. Create a DirectoryInfo object to get the parent directory and the subdirectories list.
  5. Set the geoprocessing Workspace environment variable with the parent directory.
  6. Use the IGeoprocessor.ListRaster method to get raster files to GpEnumList.
  7. Loop through the list.
  8. For each file raster, set Raster properties to left, then use the Geoprocessor and GetRasterProperties objects to retrieve the left coordinate in double.
  9. Compare with the current minimum coordinate to the left. If it's smaller than the minimum, set the current value to minimum; otherwise do nothing.
  10. For each file raster, set Raster properties to top, then use the Geoprocessor and GetRasterProperties objects to retrieve the left coordinate in double.
  11. Compare with the current maximum coordinate to the top. If it's larger than the maximum, set the current value to maximum; otherwise do nothing.
  12. Loop to the next raster in the list.
  13. Confirm that it looped through all the subdirectories; if not, use the GetTopLeft function again. If so, exit the function.
  14. Return and print out the top left coordinate of all the rasters in the folder.
You need to change the path in the code to your local path. The top left coordinates are printed out to the command window. See the following code:
[C#]
static void Main(string[] args)
{
    double minx=100000000000;
    double maxy= - 100000000000;

    GetTopLeft(@"C:\tempdata\datasets.gdb", ref minx, ref maxy);

    Console.WriteLine("left is {0}; top is {1}", minx, maxy);
    System.Console.WriteLine("hit any key to return...");
    System.Console.ReadLine();
}

static void GetTopLeft(string Folder, ref double minx, ref double maxy)
{
    //Get the top left most point from rasters in a folder and its subfolders.
    try
    {
        //Initialize geoprocessor.
        Geoprocessor geoprocessor=new Geoprocessor();
        GetRasterProperties getRasterProperties=new GetRasterProperties();
        GPUtilities gpUtil=new GPUtilitiesClass();

        //Get subdirectories.
        DirectoryInfo dir=new DirectoryInfo(Folder);
        DirectoryInfo[] subDirs=dir.GetDirectories();

        //Set environment.
        geoprocessor.SetEnvironmentValue("workspace", dir.FullName);
        IGpEnumList files=geoprocessor.ListRasters("", "");

        //Get first raster.
        string raster=files.Next();
        while (raster.Length > 0)
        {
            Console.WriteLine(System.IO.Path.Combine(dir.FullName, raster));
            getRasterProperties.in_raster=raster;

            //Get xmin.
            getRasterProperties.property_type="LEFT";
            geoprocessor.Execute(getRasterProperties, null);
            double x=getRasterProperties.property;

            if (x < minx)
                minx=x;

            //Get ymax.
            getRasterProperties.property_type="TOP";
            geoprocessor.Execute(getRasterProperties, null);
            double y=getRasterProperties.property;
            if (y > maxy)
                maxy=y;

            //Get next raster.
            raster=files.Next();
        }

        if (subDirs != null)
        {
            foreach (DirectoryInfo sd in subDirs)
            {
                GetTopLeft(System.IO.Path.Combine(Folder, sd.Name), ref minx, ref
                    maxy);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
[VB.NET]
Sub Main()
    
    Dim minx As Double=10000000000
    Dim maxy As Double=-10000000000
    
    GetTopLeft("C:\temp", minx, maxy)
    
    System.Console.WriteLine(minx)
    System.Console.WriteLine(maxy)
    System.Console.WriteLine("Press enter to return")
    System.Console.ReadLine()
    
End Sub

Public Sub GetTopLeft(ByVal Folder As String, ByRef minx As Double, ByRef maxy As Double)
    'Get the top left most point from rasters in a folder and its subfolders.
    Try
    'Initialize geoprocessor.
    Dim geoprocessor As ESRI.ArcGIS.Geoprocessor.Geoprocessor=New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
    Dim getRasterProperties As GetRasterProperties=New GetRasterProperties()
    Dim gpUtil As GPUtilities=New GPUtilitiesClass()
    
    'Get subdirectories.
    Dim Dir As DirectoryInfo=New DirectoryInfo(Folder)
    Dim subDirs() As DirectoryInfo=Dir.GetDirectories()
    
    'Set environment.
    geoprocessor.SetEnvironmentValue("workspace", Dir.FullName)
    Dim Files As IGpEnumList=geoprocessor.ListRasters("", "")
    
    'Get first raster.
    Dim raster As String=Files.Next()
    
    While raster.Length > 0
        Console.WriteLine(Dir.FullName + "\" + raster)
        getRasterProperties.in_raster=raster
        
        'Get xmin.
        getRasterProperties.property_type="LEFT"
        geoprocessor.Execute(getRasterProperties, Nothing)
        Dim x As Double=getRasterProperties.Property
        If x < minx Then
            minx=x
        End If
        
        'Get ymax.
        getRasterProperties.property_type="TOP"
        geoprocessor.Execute(getRasterProperties, Nothing)
        Dim y As Double=getRasterProperties.Property
        If y > maxy Then
            maxy=y
        End If
        
        'Get next raster.
        raster=Files.Next()
    End While
    
    If Not subDirs Is Nothing Then
        Dim sd As DirectoryInfo
        For Each sd In subDirs
            GetTopLeft(Folder + "\" + sd.Name, minx, maxy)
        Next
    End If
    
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    End Try
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine