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:
-
Create a GetTopLeft function, which takes a folder path as input and creates the initial top left extent coordinates in double.
-
Initialize the geoprocessor.
-
Create the GetRasterProperties object and GPUtilities class.
-
Create a DirectoryInfo object to get the parent directory and the subdirectories list.
-
Set the geoprocessing Workspace environment variable with the parent directory.
-
Use the IGeoprocessor.ListRaster method to get raster files to GpEnumList.
-
Loop through the list.
-
For each file raster, set Raster properties to left, then use the Geoprocessor and GetRasterProperties objects to retrieve the left coordinate in double.
-
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.
-
For each file raster, set Raster properties to top, then use the Geoprocessor and GetRasterProperties objects to retrieve the left coordinate in double.
-
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.
-
Loop to the next raster in the list.
-
Confirm that it looped through all the subdirectories; if not, use the GetTopLeft function again. If so, exit the function.
-
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):
- System
- System.IO
- ESRI.ArcGIS.Geoprocessing
- ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
- ESRI.ArcGIS.DataManagementTools
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 |