[GIS] How to add TIFF files to one layer/raster catalog

arcgis-10.0arcgis-enginearcobjectscraster

We have several TIFF images (already tagged with position information) we want to load as a single layer into the map. It's the first time I dig into this raster/topology thing, but at least I managed to create a layer with one image as an IRasterDataset:

Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
IWorkspaceFactory wsFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IRasterWorkspace2 rasterWS = (IRasterWorkspace2)wsFactory.OpenFromFile(@"c:\sourcefiles\");
IRasterDataset rds = rasterWS.OpenRasterDataset(@"example.tif");    
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromDataset(rds);
_map.AddLayer(rasterLayer);

This works well but I need to add all TIFF files to this raster layer. So I assume that IRasterCatalog could suit my needs. But my attempt to use it fails already on creation of the catalog object (taken from sample):

Geoprocessor geoProcessor = new Geoprocessor();    
CreateRasterCatalog createRasterCatalog = new ESRI.ArcGIS.DataManagementTools.CreateRasterCatalog();

//Set parameters
createRasterCatalog.out_path = outPath;
createRasterCatalog.out_name = catalogName;
createRasterCatalog.raster_spatial_reference = map.SpatialReference;
createRasterCatalog.spatial_reference = = map.SpatialReference;    

//Execute the tool to create a raster catalog
geoProcessor.Execute(createRasterCatalog, null);

On Exceute() I get a COMException, which isn't very helpfull. I tried to set the SpatialReference to IGPCoordinateSystem objects. I made sure that the output path exists.
Still the COMException raises. Has anyone a clue for me?

Is this the right approach at all?

Best Answer

I found a solution to this:

Type factoryType = ype.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory mdWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspaceName wsName = mdWorkspaceFactory.Create(@"C:\temp\rastercat_test", "rastercat_test.gdb", null, 0);

IName name = (IName)wsName;
IWorkspace mdWorkspace = (IWorkspace)name.Open();   
IRasterWorkspaceEx workspaceEx = (IRasterWorkspaceEx)(mdWorkspace);

IFields fields = CreateRasterCatalogFields(map.SpatialReference);
IRasterCatalog catalog = workspaceEx.CreateRasterCatalog("TestRC", fields, "Shape", "Raster", "");

Then you can create RasterDatasets for every TIFF file and add them to the RasterCatalog:

IDataset dataset = (IDataset)featureClass;
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)dataset.Workspace;
workspaceEdit.StartEditing(false);
workspaceEdit.StartEditOperation();
IFeature feature = featureClass.CreateFeature();

//Find raster field index.
int iRasterField = feature.Fields.FindField("Raster");

//Create raster value with input raster dataset.
IRasterValue rasterValue = new RasterValueClass();
rasterValue.RasterDataset = rasterDataset;

//Set raster value to the raster field.
feature.set_Value(iRasterField, rasterValue);
feature.Store();

//Stop editing and save edits.
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);