About the Create a raster catalog using the geoprocessor Sample
[C#]
CreateRasterCatalog_GP.cs
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
namespace CreateRasterCatalogs
{
class CreateRasterCatalogApp
{
//Set variables
static string sdePath = @"Database Connections\Connection to tiny.sde";
static string rasterFolder = @"C:\Temp\data";
static string catalogName = "rc_1";
static string outRC = sdePath + "\\" + catalogName;
static void Main(string[] args)
{
#region Initialize the license
ESRI.ArcGIS.esriSystem.AoInitialize aoInit = null;
try
{
Console.WriteLine("Obtaining license");
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
aoInit = new AoInitializeClass();
esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
Console.WriteLine("Ready with license.");
}
catch (Exception exc)
{
// If it fails at this point, shutdown the test and ignore any subsequent errors.
Console.WriteLine(exc.Message);
}
#endregion
//Coordinate system for raster column
IGPCoordinateSystem rSR = new GPCoordinateSystemClass();
rSR.SpatialReference = CreateSpatialReference((int)esriSRProjCSType.esriSRProjCS_World_Mercator);
//Coordinate system for geometry column
IGPSpatialReference gSR = new GPSpatialReferenceClass();
gSR.SpatialReference = CreateSpatialReference((int)esriSRProjCSType.esriSRProjCS_World_Mercator);
//Creates raster catalog
CreateRasterCatalog_GP(rSR, gSR);
//Loads rasters in the given directory to raster catalog
LoadDirToRasterCatalog(outRC, rasterFolder);
System.Console.WriteLine("Loading completed");
System.Console.ReadLine();//waiting user to click a key to finish
//Do not make any call to ArcObjects after license is shut down.
aoInit.Shutdown();
}
//Creates raster catalog using GP CreateRasterCatalog class
static void CreateRasterCatalog_GP(object rasterCoordSys, object geometryCoordsys)
{
//Initialize GeoProcessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
//CreateRasterCatalog GP tool
CreateRasterCatalog createRasterCatalog = new CreateRasterCatalog();
//Set parameters
createRasterCatalog.out_path = sdePath;
createRasterCatalog.out_name = catalogName;
createRasterCatalog.raster_spatial_reference = rasterCoordSys;
createRasterCatalog.spatial_reference = geometryCoordsys;
//Execute the tool to create a raster catalog
geoProcessor.Execute(createRasterCatalog, null);
ReturnMessages(geoProcessor);
}
//GP message handling
private static void ReturnMessages(Geoprocessor gp)
{
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
Console.WriteLine(gp.GetMessage(Count));
}
}
}
static void LoadDirToRasterCatalog(string outRasterCatalog, string inputDir)
{
//Initialize GeoProcessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
//Set parameters
IVariantArray parameters = new VarArrayClass();
//Set input folder
parameters.Add(inputDir);
//Set target GDB raster catalog
parameters.Add(outRasterCatalog);
//Execute the tool to load rasters in the directory to raster catalog
geoProcessor.Execute("WorkspaceToRasterCatalog", parameters, null);
ReturnMessages(geoProcessor);
}
//Create a spatial reference with given factory code
static ISpatialReference CreateSpatialReference(int code)
{
ISpatialReferenceFactory2 spatialReferenceFact = new SpatialReferenceEnvironmentClass();
try
{
return spatialReferenceFact.CreateSpatialReference(code);
}
catch
{
return new UnknownCoordinateSystemClass();
}
}
}
}
[Visual Basic .NET]
CreateRasterCatalog_GP.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.DataSourcesGDB
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.GeomeTry
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.esriSystem
Module CreateRasterCatalog1
'Set variables
Private sdePath As String = "Database Connections\Connection to tiny.sde"
Private rasterFolder As String = "c:\temp\review"
Private catalogName As String = "rc_3"
Private outRC As String = sdePath + "\" + catalogName
Sub Main(ByVal args() As String)
'Initialize the license
Dim aoInit As ESRI.ArcGIS.esriSystem.AoInitialize = Nothing
Try
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)
aoInit = New AoInitializeClass()
Dim licStatus As esriLicenseStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor)
Console.WriteLine("License checkout successful.")
Catch exc As Exception
' If it fails at this point, shutdown the test and ignore any subsequent errors.
Console.WriteLine(exc.Message)
End Try
'Coordinate system for raster column
Dim rSR As IGPCoordinateSystem = New GPCoordinateSystemClass()
rSR.SpatialReference = CreateSpatialReference(CInt(esriSRProjCSType.esriSRProjCS_World_Mercator))
'Coordinate system for geometry column
Dim gSR As IGPSpatialReference = New GPSpatialReferenceClass()
gSR.SpatialReference = CreateSpatialReference(CInt(esriSRProjCSType.esriSRProjCS_World_Mercator))
'Creates raster catalog
CreateRasterCatalog_GP(rSR, gSR)
'Loads rasters in the given directory to raster catalog
LoadDirtoRasterCatalog(outRC, rasterFolder)
System.Console.WriteLine("Loading completed")
System.Console.ReadLine() ' click a key to finish
'Do not make any call to ArcObjects after Shutdown()
aoInit.Shutdown()
End Sub
Private Sub CreateRasterCatalog_GP(ByVal rasterCoordSys As Object, ByVal geometryCoordsys As Object)
'Initialize GeoProcessor
Dim geoProcessor As New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
'CreateRasterCatalog GP tool
Dim createRasterCatalog As New CreateRasterCatalog()
'Set parameters
createRasterCatalog.out_path = sdePath
createRasterCatalog.out_name = catalogName
createRasterCatalog.raster_spatial_reference = rasterCoordSys
createRasterCatalog.spatial_reference = geometryCoordsys
'Execute the tool to create a raster catalog
geoProcessor.Execute(createRasterCatalog, Nothing)
ReturnMessages(geoProcessor)
End Sub
'GP message handling
Private Sub ReturnMessages(ByVal gp As ESRI.ArcGIS.Geoprocessor.Geoprocessor)
If gp.MessageCount > 0 Then
Dim Count As Integer
For Count = 0 To gp.MessageCount - 1 Step Count + 1
Console.WriteLine(gp.GetMessage(Count))
Next
End If
End Sub
Private Sub LoadDirToRasterCatalog(ByVal outRasterCatalog As String, ByVal inputDir As String)
'Initialize GeoProcessor
Dim geoProcessor As New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
'Set parameters
Dim parameters As IVariantArray = New VarArrayClass()
'Set input folder
parameters.Add(inputDir)
'Set target GDB raster catalog
parameters.Add(outRasterCatalog)
'Execute the tool to load rasters in the directory to raster catalog
geoProcessor.Execute("WorkspaceToRasterCatalog", parameters, Nothing)
ReturnMessages(geoProcessor)
End Sub
'Create a spatial reference with given factory code
Private Function CreateSpatialReference(ByVal code As Integer) As ISpatialReference
Dim spatialReferenceFact As ISpatialReferenceFactory2 = New SpatialReferenceEnvironmentClass()
Try
Return spatialReferenceFact.CreateSpatialReference(code)
Catch
Return New UnknownCoordinateSystemClass()
End Try
End Function
End Module