ArcObjects Library Reference  

CreateFileRasterMosaic

About the Mosaic raster datasets to a file raster format Sample

[C#]

CreateFileRasterMosaic.cs

using System;
using Microsoft.Win32;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.DataSourcesGDB;

namespace CreateFileRasterMosaic
{
    //Sample creating a file raster mosaic from rasters in a folder and its subfolders
    //Steps:
    //  1. Create an unmanaged PGDB raster catalog
    //  2. Load rasters in the input folder and its subfolders to the new raster catalog
    //  3. Create a mosaic file raster dataset from the unmanaged raster catalog

    class CreateFileRasterMosaic
    {
        //Local variables for data path
        //The TEMP directory will be used to create temporary raster catalog and output raster dataset
        //Remove temp.mdb in TEMP directory if it exists
        //You can substitute the paths with your data location

        static string inputFolder = @"C:\data";
        static string outputFolder = @"C:\Temp";
        static string outputName = "mosaic.tif";
        static string tempRasterCatalog = "temp_rc";
        static string tempPGDB = "temp.mdb";
        static string tempPGDBPath = outputFolder + "\\" + tempPGDB;
        static string tempRasterCatalogPath = tempPGDBPath + "\\" + tempRasterCatalog;

        static void Main(string[] args)
        {
            ESRI.ArcGIS.esriSystem.AoInitialize aoInit = null;
            try
            {
                ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
                aoInit = new AoInitializeClass();
                esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
                Console.WriteLine("License Checkout successful.");
            }
            catch (Exception exc)
            {
                // If it fails at this point, shutdown the test and ignore any subsequent errors.
                Console.WriteLine(exc.Message);
            }

            try
            {
                //Create temporary unmanaged raster catalog and load all rasters
                CreateUnmanagedRasterCatalog();

                //Open raster catalog
                IRasterWorkspaceEx rasterWorkspaceEx = (IRasterWorkspaceEx)OpenRasterPGDBWorkspace(tempPGDBPath);
                IRasterCatalog rasterCatalog = rasterWorkspaceEx.OpenRasterCatalog(tempRasterCatalog);

                //Mosaic rasters in the raster catalog
                Mosaic(rasterCatalog);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }

            Console.Write("Please press any key to close the application.");
            Console.ReadKey();

            //Do not make any call to ArcObjects after ShutDown() call
            aoInit.Shutdown();
        }

        static void CreateUnmanagedRasterCatalog()
        {
            try
            {
                //Use geoprocessing to create the geodatabase, the raster catalog, and load our directory
                //to the raster catalog.
                Geoprocessor geoprocessor = new Geoprocessor();

                //Create personal GDB in the TEMP directory
                CreatePersonalGDB createPersonalGDB = new CreatePersonalGDB();
                createPersonalGDB.out_folder_path = outputFolder;
                createPersonalGDB.out_name = tempPGDB;

                geoprocessor.Execute(createPersonalGDB, null);

                //Create an unmanaged raster catalog in the newly created personal GDB
                CreateRasterCatalog createRasterCatalog = new CreateRasterCatalog();

                createRasterCatalog.out_path = tempPGDBPath;
                createRasterCatalog.out_name = tempRasterCatalog;
                createRasterCatalog.raster_management_type = "unmanaged";

                geoprocessor.Execute(createRasterCatalog, null);

                //Load data into the unmanaged raster catalog
                WorkspaceToRasterCatalog wsToRasterCatalog = new WorkspaceToRasterCatalog();

                wsToRasterCatalog.in_raster_catalog = tempRasterCatalogPath;
                wsToRasterCatalog.in_workspace = inputFolder;
                wsToRasterCatalog.include_subdirectories = "INCLUDE_SUBDIRECTORIES";

                geoprocessor.Execute(wsToRasterCatalog, null);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
        }

        static void Mosaic(IRasterCatalog rasterCatalog)
        {
            try
            {
                //Mosaics all rasters in the raster catalog to an output raster dataset
                IMosaicRaster mosaicRaster = new MosaicRasterClass();
                mosaicRaster.RasterCatalog = rasterCatalog;

                //Set mosaicking options, you may not need to set these for your data
                mosaicRaster.MosaicColormapMode = rstMosaicColormapMode.MM_MATCH;
                mosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_LAST;

                //Open output workspace
                IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
                IWorkspace workspace = workspaceFactory.OpenFromFile(outputFolder, 0);

                //Save out to a target raster dataset
                //It can be saved to TIFF, IMG, GRID, BMP, GIF, JPEG2000, JPEG, Geodatabase, ect.
                ISaveAs saveas = (ISaveAs)mosaicRaster;
                saveas.SaveAs(outputName, workspace, "TIFF");
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
        }

        static IWorkspace OpenRasterPGDBWorkspace(string connStr)
        {
            Type t = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
            System.Object obj = Activator.CreateInstance(t);
            IWorkspaceFactory2 workspaceFactory = obj as IWorkspaceFactory2;
            return workspaceFactory.OpenFromFile(connStr, 0);
        }
    }
}

[Visual Basic .NET]

CreateFileRasterMosaic.vb

Imports Microsoft.Win32
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.DataSourcesGDB

'Sample creating a file raster mosaic from rasters in a folder and its subfolders
'Steps:
'  1. Create an unmanaged PGDB raster catalog
'  2. Load rasters in the input folder and its subfolders to the new raster catalog
'  3. Create a mosaic file raster dataset from the unmanaged raster catalog

Module CreateFileRasterMosaic
    'Local variables for data path
    'The TEMP directory will be used to create temporary raster catalog and output raster dataset
    'Remove temp.mdb in TEMP directory if it exists
    'You can substitute the paths with your data location

    Private inputFolder As String = "C:\data"
    Private outputFolder As String = "C:\Temp"
    Private outputName As String = "mosaic.tif"
    Private tempRasterCatalog As String = "temp_rc"
    Private tempPGDB As String = "temp.mdb"
    Private tempPGDBPath As String = outputFolder + "\" + tempPGDB
    Private tempRasterCatalogPath As String = tempPGDBPath + "\" + tempRasterCatalog

    Sub Main(ByVal args As String())
        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.esriLicenseProductCodeBasic)
            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

        Try
            'Create temporary unmanaged raster catalog and load all rasters
            CreateUnmanagedRasterCatalog()

            'Open raster catalog
            Dim rasterWorkspaceEx As IRasterWorkspaceEx = CType(OpenRasterPGDBWorkspace(tempPGDBPath), IRasterWorkspaceEx)
            Dim rasterCatalog As IRasterCatalog = rasterWorkspaceEx.OpenRasterCatalog(tempRasterCatalog)

            'Mosaic rasters in the raster catalog
            Mosaic(rasterCatalog)

        Catch exc As Exception
            Console.WriteLine(exc.Message)
        End Try

        Console.WriteLine("Please press any key to close the application.")
        Console.ReadKey()

        'Do not make any call to ArcObjects after ShutDown() call
        aoInit.Shutdown()
    End Sub

    Sub CreateUnmanagedRasterCatalog()
        Try
            'Use geoprocessing to create the geodatabase, the raster catalog, and load our directory
            'to the raster catalog.
            Dim geoprocessor As New Geoprocessor()

            'Create personal GDB in the TEMP directory
            Dim createPersonalGDB As New CreatePersonalGDB()
            createPersonalGDB.out_folder_path = outputFolder
            createPersonalGDB.out_name = tempPGDB

            geoprocessor.Execute(createPersonalGDB, Nothing)

            'Create an unmanaged raster catalog in the newly created personal GDB
            Dim createRasterCatalog As New CreateRasterCatalog()

            createRasterCatalog.out_path = tempPGDBPath
            createRasterCatalog.out_name = tempRasterCatalog
            createRasterCatalog.raster_management_type = "unmanaged"

            geoprocessor.Execute(createRasterCatalog, Nothing)

            'Load data into the unmanaged raster catalog
            Dim wsToRasterCatalog As New WorkspaceToRasterCatalog()

            wsToRasterCatalog.in_raster_catalog = tempRasterCatalogPath
            wsToRasterCatalog.in_workspace = inputFolder
            wsToRasterCatalog.include_subdirectories = "INCLUDE_SUBDIRECTORIES"

            geoprocessor.Execute(wsToRasterCatalog, Nothing)
        Catch exc As Exception
            Console.WriteLine(exc.Message)
        End Try
    End Sub

    Sub Mosaic(ByVal rasterCatalog As IRasterCatalog)
        Try
            'Mosaics all rasters in the raster catalog to an output raster dataset
            Dim mosaicRaster As IMosaicRaster = New MosaicRasterClass()
            mosaicRaster.RasterCatalog = rasterCatalog

            'Set mosaicking options, you may not need to set these for your data
            mosaicRaster.MosaicColormapMode = rstMosaicColormapMode.MM_MATCH
            mosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_LAST

            'Open output workspace
            Dim workspaceFactory As IWorkspaceFactory = New RasterWorkspaceFactoryClass()
            Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(outputFolder, 0)

            'Save out to a target raster dataset
            'It can be saved to TIFF, IMG, GRID, BMP, GIF, JPEG2000, JPEG, Geodatabase, ect.
            Dim saveas As ISaveAs = CType(mosaicRaster, ISaveAs)
            saveas.SaveAs(outputName, workspace, "TIFF")
        Catch exc As Exception
            Console.WriteLine(exc.Message)
        End Try
    End Sub

    Function OpenRasterPGDBWorkspace(ByVal connStr As String) As IWorkspace
        '        Dim workspaceFactory As IWorkspaceFactory2 = New AccessWorkspaceFactoryClass()
        Dim t As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
        Dim obj As System.Object = Activator.CreateInstance(t)
        Dim workspaceFactory As IWorkspaceFactory2 = obj
        Return workspaceFactory.OpenFromFile(connStr, 0)
    End Function
End Module