ArcObjects Library Reference  

CreateMosaicDataset

About the Create a Mosaic dataset Sample

[C#]

CreateMosaicDataset.cs

using System;
using System.IO;
using ESRI.ArcGIS;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace RasterSamples
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            #region Initialize 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.esriLicenseProductCodeAdvanced);
                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

            try
            {
                #region Setup MD Parameters
                MDParameters.gdbParentFolder = @"c:\temp\CreateMD";
                // Choose which type of gdb to create/open.
                // 0 - Create File Gdb
                // 1 - Create Personal Gdb
                // 2 - Open SDE
                int gdbOption = 0;
                // Provide the proper extension based on the gdb you want to create. 
                // e.g. MDParameters.gdbName = "samplePGdb.mdb" to create a personal gdb.
                // To use an SDE, set SDE connection properties below.
                MDParameters.gdbName = @"sampleGdb.gdb";
                MDParameters.mosaicDatasetName = @"sampleMD";

                // Specify the srs of the mosaic dataset
                ISpatialReferenceFactory spatialrefFactory = new SpatialReferenceEnvironmentClass();
                MDParameters.mosaicDatasetSrs = spatialrefFactory.CreateProjectedCoordinateSystem(
                    (int)(esriSRProjCSType.esriSRProjCS_World_Mercator));

                // 0 and PT_UNKNOWN for bits and bands = use defaults.
                MDParameters.mosaicDatasetBands = 0;
                MDParameters.mosaicDatasetBits = rstPixelType.PT_UNKNOWN;
                MDParameters.configKeyword = "";
                
                // Product Definition key choices:
                // None
                // NATURAL_COLOR_RGB
                // NATURAL_COLOR_RGBI
                // FALSE_COLOR_IRG
                // FORMOSAT-2_4BANDS
                // GEOEYE-1_4BANDS
                // IKONOS_4BANDS
                // KOMPSAT-2_4BANDS
                // LANDSAT_6BANDS
                // LANDSAT_MSS_4BANDS
                // QUICKBIRD_4BANDS
                // RAPIDEYE_5BANDS
                // SPOT-5_4BANDS
                // WORLDVIEW-2_8BANDS

                // Setting this property ensures any data added to the MD with its
                // metadata defined gets added with the correct band combination.
                MDParameters.productDefinitionKey = "FALSE_COLOR_IRG";

                MDParameters.rasterTypeName = "QuickBird";
                // The next two properties can be left blank for defaults
                // The product filter defines which specific product of the raster
                // type to add, e.g. To specfiy Quickbird Basic use value "Basic"
                MDParameters.rasterTypeProductFilter = "";
                // The product name specifies which template to use when adding data.
                // e.g. "Pansharpen and Multispectral" means both multispectral and 
                // pansharpened rasters are added to the mosaic dataset.
                MDParameters.rasterTypeProductName = "Multispectral";

                // Data source from which to read the data.
                MDParameters.dataSource = //@"\\Qalab_server\data\Raster\DOTNET\Input\DatasourcesRaster\FunctionRasterDataset\RasterDataset";
                @"\\qalab_server\data\Raster\DOTNET\Input\DatasourcesRaster\MosaicDataset\QB\Torino";
                //@"\\isdemo1\0_sampledata\SampleData\DataSets\QBBasic1b\Baghdad\X0388";
                MDParameters.dataSourceFilter = @"";
                // No need to set if data source has an srs or if you want to use the MD srs as data source srs.
                MDParameters.dataSourceSrs = null;

                MDParameters.buildOverviews = true;
                #endregion

                MDParameters.emptyFgdbFolder = true;
                MDParameters.createFgdbParentFolder = false;
                #region Empty/Create Output Directory
                if (MDParameters.emptyFgdbFolder)
                {
                    try
                    {
                        Console.WriteLine("Emptying Output Directory");
                        Directory.Delete(MDParameters.gdbParentFolder, true);
                        Directory.CreateDirectory(MDParameters.gdbParentFolder);
                    }
                    catch (Exception)
                    {
                    }
                }
                if (MDParameters.createFgdbParentFolder && !System.IO.Directory.Exists(MDParameters.gdbParentFolder))
                {
                    Console.WriteLine("Creating Output Directory");
                    Directory.CreateDirectory(MDParameters.gdbParentFolder);
                }
                #endregion

                CreateMD createMD = new CreateMD();

                if (gdbOption == 0)
                {
                    #region Create MD in File GDB
                    Console.WriteLine("Creating File GDB: " + MDParameters.gdbName);
                    IWorkspace fgdbWorkspace = CreateFileGdbWorkspace(MDParameters.gdbParentFolder, MDParameters.gdbName);
                    createMD.CreateMosaicDataset(fgdbWorkspace);
                    #endregion
                }
                else if (gdbOption == 1)
                {
                    #region Create MD in Personal GDB
                    Console.WriteLine("Creating Personal GDB: " + MDParameters.gdbName);
                    IWorkspace pGdbWorkspace = CreateAccessWorkspace(MDParameters.gdbParentFolder, MDParameters.gdbName);
                    createMD.CreateMosaicDataset(pGdbWorkspace);
                    #endregion
                }
                else if (gdbOption == 2)
                {
                    #region Open SDE GDB
                    // Set SDE connection properties.
                    IPropertySet sdeProperties = new PropertySetClass();
                    sdeProperties.SetProperty("SERVER", "barbados");
                    sdeProperties.SetProperty("INSTANCE", "9411");
                    sdeProperties.SetProperty("VERSION", "sde.DEFAULT");
                    sdeProperties.SetProperty("USER", "gdb");
                    sdeProperties.SetProperty("PASSWORD", "gdb");
                    sdeProperties.SetProperty("DATABASE", "VTEST");
                    IWorkspace sdeWorkspace = CreateSdeWorkspace(sdeProperties);
                    if (sdeWorkspace == null)
                    {
                        Console.WriteLine("Could not open SDE workspace: ");
                        return;
                    }

                    #endregion

                    #region Create MD in SDE
                    MDParameters.mosaicDatasetName = @"sampleMD";
                    createMD.CreateMosaicDataset(sdeWorkspace);
                    #endregion
                }

                #region Shutdown
                Console.WriteLine("Press any key...");
                Console.ReadKey();
                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }
            catch (Exception exc)
            {
                #region Report
                Console.WriteLine("Exception Caught in Main: " + exc.Message);
                Console.WriteLine("Shutting down.");
                #endregion

                #region Shutdown
                Console.WriteLine("Press any key...");
                Console.ReadKey();
                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }
        }

        /// <summary>
        /// Create a File Geodatabase given the name and parent folder.
        /// </summary>
        /// <param name="gdbParentFolder">Folder to create the new gdb in.</param>
        /// <param name="gdbName">Name of the gdb to be created.</param>
        /// <returns>Workspace reference to the new geodatabase.</returns>
        public static IWorkspace CreateFileGdbWorkspace(string gdbParentFolder, string gdbName)
        {
            // Instantiate a file geodatabase workspace factory and create a file geodatabase.
            // The Create method returns a workspace object.
            Type factoryType = Type.GetTypeFromProgID(
              "esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
              Activator.CreateInstance(factoryType);
            IWorkspaceName workspaceName = workspaceFactory.Create(gdbParentFolder,
                gdbName, null, 0);

            // Cast the workspace name object to the IName interface and open the workspace.
            IName name = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)name.Open();
            return workspace;
        }

        /// <summary>
        /// Create a Personal Geodatabase given the name and parent folder.
        /// </summary>
        /// <param name="gdbParentFolder">Folder to create the new gdb in.</param>
        /// <param name="gdbName">Name of the gdb to be created.</param>
        /// <returns>Workspace reference to the new geodatabase.</returns>
        public static IWorkspace CreateAccessWorkspace(string gdbParentFolder, string gdbName)
        {
            // Instantiate an Access workspace factory and create a personal geodatabase.
            // The Create method returns a workspace object.
            Type factoryType = Type.GetTypeFromProgID(
              "esriDataSourcesGDB.AccessWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
              Activator.CreateInstance(factoryType);
            IWorkspaceName workspaceName = workspaceFactory.Create(gdbParentFolder,
                gdbName, null, 0);

            // Cast the workspace name object to the IName interface and open the workspace.
            IName name = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)name.Open();
            return workspace;
        }

        /// <summary>
        /// Retrieves an SDE workspace using the specified property set.
        /// </summary>
        /// <param name="propertySet">The connection parameters.</param>
        /// <returns>An IWorkspace reference to an SDE workspace.</returns>
        public static IWorkspace CreateSdeWorkspace(IPropertySet propertySet)
        {
            // Create the workspace factory and connect to the workspace.
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            IWorkspace workspace = workspaceFactory.Open(propertySet, 0);                        
            return workspace;
        }

    };

    public static class MDParameters
    {
        // Define the folder in which the GDB resides or is to be created
        public static string gdbParentFolder;
        // Name of the GDB
        public static string gdbName;
        // Configuration keyword for the Gdb (optional)
        public static string configKeyword;

        // Mosaic Dataset Properties
        public static string mosaicDatasetName; // Name of the Mosaic Dataset to create.
        public static ISpatialReference mosaicDatasetSrs; // Srs for the Mosaic Dataset
        public static int mosaicDatasetBands; // Number of bands of the Mosaic Dataset
        public static rstPixelType mosaicDatasetBits; // Pixel Type of the Mosaic Dataset
        public static string productDefinitionKey; // The product definition key.
        public static IArray productDefinitionProps; // Properties of the product definition (Bands names and wavelengths).

        // Raster Type Properties
        public static string rasterTypeName; // Name of the Raster type to use (or path to the .art file)
        public static string rasterTypeProductFilter; // The product filter to set on the Raster Type
        public static string rasterTypeProductName; // The name of the product to create from the added data
        public static bool rasterTypeAddDEM; // Flag to specify whether to add a DEM to a Raster Type.
        public static string rasterTypeDemPath; // Path to the DEM if previous property is true.

        // Crawler Properties
        public static string dataSource; // Path to the data.
        public static string dataSourceFilter; // File filter to use to crawl data.
        // Srs of the input data if the input does not contain an srs 
        // and it is different from the srs of the mosaic dataset(optional)
        public static ISpatialReference dataSourceSrs;

        // Operational flags
        public static bool buildOverviews; // Generate overviews for the Mosaic Dataset
        public static bool emptyFgdbFolder; // Delete the parent folder for the GDB
        public static bool createFgdbParentFolder; // Create the Parent folder for the GDB
    };

    public class CreateMD
    {
        /// <summary>
        /// Create a Mosaic Dataset in the geodatabase provided using the parameters defined by MDParamaters.
        /// </summary>
        /// <param name="gdbWorkspace">Geodatabase to create the Mosaic dataser in.</param>
        public void CreateMosaicDataset(IWorkspace gdbWorkspace)
        {
            try
            {
                #region Global Declarations
                IMosaicDataset theMosaicDataset = null;
                IMosaicDatasetOperation theMosaicDatasetOperation = null;
                IMosaicWorkspaceExtensionHelper mosaicExtHelper = null;
                IMosaicWorkspaceExtension mosaicExt = null;
                #endregion

                #region CreateMosaicDataset
                try
                {
                    Console.WriteLine("Create Mosaic Dataset: " + MDParameters.mosaicDatasetName + ".amd");
                    /// Setup workspaces.
                    /// Create Srs
                    ISpatialReferenceFactory spatialrefFactory = new SpatialReferenceEnvironmentClass();

                    // Create the mosaic dataset creation parameters object.
                    ICreateMosaicDatasetParameters creationPars = new CreateMosaicDatasetParametersClass();
                    // Set the number of bands for the mosaic dataset.
                    // If defined as zero leave defaults
                    if (MDParameters.mosaicDatasetBands != 0)
                        creationPars.BandCount = MDParameters.mosaicDatasetBands;
                    // Set the pixel type of the mosaic dataset.
                    // If defined as unknown leave defaults
                    if (MDParameters.mosaicDatasetBits != rstPixelType.PT_UNKNOWN)
                        creationPars.PixelType = MDParameters.mosaicDatasetBits;
                    // Create the mosaic workspace extension helper class.
                    mosaicExtHelper = new MosaicWorkspaceExtensionHelperClass();
                    // Find the right extension from the workspace.
                    mosaicExt = mosaicExtHelper.FindExtension(gdbWorkspace);

                    // Default is none.
                    if (MDParameters.productDefinitionKey.ToLower() != "none")
                    {
                        // Set the product definition keyword and properties.
                        // (The property is called band definition keyword and band properties in the object).
                        ((ICreateMosaicDatasetParameters2)creationPars).BandDefinitionKeyword = MDParameters.productDefinitionKey;
                        MDParameters.productDefinitionProps = SetBandProperties(MDParameters.productDefinitionKey);
                        if (MDParameters.productDefinitionProps.Count == 0)
                        {
                            Console.WriteLine("Setting production definition properties failed.");
                            return;
                        }
                        ((ICreateMosaicDatasetParameters2)creationPars).BandProperties = MDParameters.productDefinitionProps;
                    }
                    
                    // Use the extension to create a new mosaic dataset, supplying the 
                    // spatial reference and the creation parameters object created above.
                    theMosaicDataset = mosaicExt.CreateMosaicDataset(MDParameters.mosaicDatasetName,
                        MDParameters.mosaicDatasetSrs, creationPars, MDParameters.configKeyword);
                }
                catch (Exception exc)
                {
                    Console.WriteLine("Exception Caught while creating Mosaic Dataset: " + exc.Message);
                    return;
                }
                #endregion

                #region OpenMosaicDataset
                Console.WriteLine("Opening Mosaic Dataset");
                theMosaicDataset = null;
                // Use the extension to open the mosaic dataset.
                theMosaicDataset = mosaicExt.OpenMosaicDataset(MDParameters.mosaicDatasetName);
                // The mosaic dataset operation interface is used to perform operations on 
                // a mosaic dataset.
                theMosaicDatasetOperation = (IMosaicDatasetOperation)(theMosaicDataset);
                #endregion

                #region Preparing Raster Type
                Console.WriteLine("Preparing Raster Type");
                // Create a Raster Type Name object.
                IRasterTypeName theRasterTypeName = new RasterTypeNameClass();
                // Assign the name of the Raster Type to the name object.
                // The Name field accepts a path to an .art file as well 
                // the name for a built in Raster Type.
                theRasterTypeName.Name = MDParameters.rasterTypeName;
                // Use the Open function from the IName interface to get the Raster Type object.
                IRasterType theRasterType = (IRasterType)(((IName)theRasterTypeName).Open());
                if (theRasterType == null)
                    Console.WriteLine("Raster Type not found " + MDParameters.rasterTypeName);

                // Set the URI Filter on the loaded raster type.
                if (MDParameters.rasterTypeProductFilter != "")
                {
                    // Get the supported URI filters from the raster type object using the 
                    // raster type properties interface.
                    IArray mySuppFilters = ((IRasterTypeProperties)theRasterType).SupportedURIFilters;
                    IItemURIFilter productFilter = null;
                    for (int i = 0; i < mySuppFilters.Count; ++i)
                    {
                        // Set the desired filter from the supported filters.
                        productFilter = (IItemURIFilter)mySuppFilters.get_Element(i);
                        if (productFilter.Name == MDParameters.rasterTypeProductFilter)
                            theRasterType.URIFilter = productFilter;
                    }
                }
                // Enable the correct templates in the raster type.
                string[] rasterProductNames = MDParameters.rasterTypeProductName.Split(';');
                bool enableTemplate = false;
                if (rasterProductNames.Length >= 1 && (rasterProductNames[0] != ""))
                {
                    // Get the supported item templates from the raster type.
                    IItemTemplateArray templateArray = theRasterType.ItemTemplates;
                    for (int i = 0; i < templateArray.Count; ++i)
                    {
                        // Go through the supported item templates and enable the ones needed.
                        IItemTemplate template = templateArray.get_Element(i);
                        enableTemplate = false;
                        for (int j = 0; j < rasterProductNames.Length; ++j)

                            if (template.Name == rasterProductNames[j])
                                enableTemplate = true;
                        if (enableTemplate)
                            template.Enabled = true;
                        else
                            template.Enabled = false;
                    }
                }

                if (MDParameters.dataSourceSrs != null)
                {
                    ((IRasterTypeProperties)theRasterType).SynchronizeParameters.DefaultSpatialReference =
                        MDParameters.dataSourceSrs;
                }
                #endregion

                #region Add DEM To Raster Type
                if (MDParameters.rasterTypeAddDEM && ((IRasterTypeProperties)theRasterType).SupportsOrthorectification)
                {
                    // Open the Raster Dataset
                    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
                    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                    IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(
                        System.IO.Path.GetDirectoryName(MDParameters.rasterTypeDemPath), 0); ;
                    IRasterDataset myRasterDataset = rasterWorkspace.OpenRasterDataset(
                        System.IO.Path.GetFileName(MDParameters.rasterTypeDemPath));

                    IGeometricFunctionArguments geometricFunctionArguments =
                        new GeometricFunctionArgumentsClass();
                    geometricFunctionArguments.DEM = myRasterDataset;
                    ((IRasterTypeProperties)theRasterType).OrthorectificationParameters =
                        geometricFunctionArguments;
                }
                #endregion

                #region Preparing Data Source Crawler
                Console.WriteLine("Preparing Data Source Crawler");
                // Create a new property set to specify crawler properties.
                IPropertySet crawlerProps = new PropertySetClass();
                // Specify a file filter
                crawlerProps.SetProperty("Filter", MDParameters.dataSourceFilter);
                // Specify whether to search subdirectories.
                crawlerProps.SetProperty("Recurse", true);
                // Specify the source path.
                crawlerProps.SetProperty("Source", MDParameters.dataSource);
                // Get the recommended crawler from the raster type based on the specified 
                // properties using the IRasterBuilder interface.
                IDataSourceCrawler theCrawler = ((IRasterBuilder)theRasterType).GetRecommendedCrawler(crawlerProps);
                #endregion

                #region Add Rasters
                Console.WriteLine("Adding Rasters");
                // Create a AddRaster parameters object.
                IAddRastersParameters AddRastersArgs = new AddRastersParametersClass();
                // Specify the data crawler to be used to crawl the data.
                AddRastersArgs.Crawler = theCrawler;
                // Specify the raster type to be used to add the data.
                AddRastersArgs.RasterType = theRasterType;
                // Use the mosaic dataset operation interface to add 
                // rasters to the mosaic dataset.
                theMosaicDatasetOperation.AddRasters(AddRastersArgs, null);
                #endregion

                #region Compute Pixel Size Ranges
                Console.WriteLine("Computing Pixel Size Ranges");
                // Create a calculate cellsize ranges parameters object.
                ICalculateCellSizeRangesParameters computeArgs = new CalculateCellSizeRangesParametersClass();
                // Use the mosaic dataset operation interface to calculate cellsize ranges.
                theMosaicDatasetOperation.CalculateCellSizeRanges(computeArgs, null);
                #endregion

                #region Building Boundary
                Console.WriteLine("Building Boundary");
                // Create a build boundary parameters object.
                IBuildBoundaryParameters boundaryArgs = new BuildBoundaryParametersClass();
                // Set flags that control boundary generation.
                boundaryArgs.AppendToExistingBoundary = true;
                // Use the mosaic dataset operation interface to build boundary.
                theMosaicDatasetOperation.BuildBoundary(boundaryArgs, null);
                #endregion

                if (MDParameters.buildOverviews)
                {
                    #region Defining Overviews
                    Console.WriteLine("Defining Overviews");
                    // Create a define overview parameters object.
                    IDefineOverviewsParameters defineOvArgs = new DefineOverviewsParametersClass();
                    // Use the overview tile parameters interface to specify the overview factor
                    // used to generate overviews.
                    ((IOverviewTileParameters)defineOvArgs).OverviewFactor = 3;
                    // Use the mosaic dataset operation interface to define overviews.
                    theMosaicDatasetOperation.DefineOverviews(defineOvArgs, null);
                    #endregion

                    #region Compute Pixel Size Ranges
                    Console.WriteLine("Computing Pixel Size Ranges");
                    // Calculate cell size ranges to update the Min/Max pixel sizes.
                    theMosaicDatasetOperation.CalculateCellSizeRanges(computeArgs, null);
                    #endregion

                    #region Generating Overviews
                    Console.WriteLine("Generating Overviews");
                    // Create a generate overviews parameters object.
                    IGenerateOverviewsParameters genPars = new GenerateOverviewsParametersClass();
                    // Set properties to control overview generation.
                    IQueryFilter genQuery = new QueryFilterClass();
                    ((ISelectionParameters)genPars).QueryFilter = genQuery;
                    genPars.GenerateMissingImages = true;
                    genPars.GenerateStaleImages = true;
                    // Use the mosaic dataset operation interface to generate overviews.
                    theMosaicDatasetOperation.GenerateOverviews(genPars, null);
                    #endregion
                }

                #region Report
                Console.WriteLine("Success.");
                #endregion
            }
            catch (Exception exc)
            {
                #region Report
                Console.WriteLine("Exception Caught in CreateMD: " + exc.Message);
                Console.WriteLine("Shutting down.");
                #endregion
            }
        }

        /// <summary>
        /// Create an array with the right BandName and Wavelength values for the corresponding key. 
        /// </summary>
        /// <param name="key">Key to use.</param>
        /// <returns>Array with the correct BandName and Wavelength values.</returns>
        private static IArray SetBandProperties(string key)
        {
            IArray productDefProps = new ArrayClass();
            IPropertySet band1Def = new PropertySetClass();
            IPropertySet band2Def = new PropertySetClass();
            IPropertySet band3Def = new PropertySetClass();
            if (key == "NATURAL_COLOR_RGB" || key == "NATURAL_COLOR_RGBI")
            {
                band1Def.SetProperty("BandName", "Red");
                band1Def.SetProperty("WavelengthMin", 630);
                band1Def.SetProperty("WavelengthMax", 690);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 530);
                band2Def.SetProperty("WavelengthMax", 570);

                band3Def.SetProperty("BandName", "Blue");
                band3Def.SetProperty("WavelengthMin", 440);
                band3Def.SetProperty("WavelengthMax", 480);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);

                if (key == "NATURAL_COLOR_RGBI")
                {
                    IPropertySet band4Def = new PropertySetClass();
                    band4Def.SetProperty("BandName", "NearInfrared");
                    band4Def.SetProperty("WavelengthMin", 770);
                    band4Def.SetProperty("WavelengthMax", 830);
                    productDefProps.Add(band4Def);
                }
            }
            else if (key == "FALSE_COLOR_IRG")
            {
                band1Def.SetProperty("BandName", "Infrared");
                band1Def.SetProperty("WavelengthMin", 770);
                band1Def.SetProperty("WavelengthMax", 830);

                band2Def.SetProperty("BandName", "Red");
                band2Def.SetProperty("WavelengthMin", 630);
                band2Def.SetProperty("WavelengthMax", 690);

                band3Def.SetProperty("BandName", "Green");
                band3Def.SetProperty("WavelengthMin", 530);
                band3Def.SetProperty("WavelengthMax", 570);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
            }
            else if (key == "FORMOSAT-2_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 450);
                band1Def.SetProperty("WavelengthMax", 520);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 520);
                band2Def.SetProperty("WavelengthMax", 600);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 630);
                band3Def.SetProperty("WavelengthMax", 690);

                band4Def.SetProperty("BandName", "NearInfrared");
                band4Def.SetProperty("WavelengthMin", 760);
                band4Def.SetProperty("WavelengthMax", 900);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "GEOEYE-1_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 450);
                band1Def.SetProperty("WavelengthMax", 510);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 510);
                band2Def.SetProperty("WavelengthMax", 580);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 655);
                band3Def.SetProperty("WavelengthMax", 690);

                band4Def.SetProperty("BandName", "NearInfrared");
                band4Def.SetProperty("WavelengthMin", 780);
                band4Def.SetProperty("WavelengthMax", 920);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "IKONOS_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 445);
                band1Def.SetProperty("WavelengthMax", 516);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 506);
                band2Def.SetProperty("WavelengthMax", 595);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 632);
                band3Def.SetProperty("WavelengthMax", 698);

                band4Def.SetProperty("BandName", "NearInfrared");
                band4Def.SetProperty("WavelengthMin", 757);
                band4Def.SetProperty("WavelengthMax", 863);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "KOMPSAT-2_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 450);
                band1Def.SetProperty("WavelengthMax", 520);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 520);
                band2Def.SetProperty("WavelengthMax", 600);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 630);
                band3Def.SetProperty("WavelengthMax", 690);

                band4Def.SetProperty("BandName", "NearInfrared");
                band4Def.SetProperty("WavelengthMin", 760);
                band4Def.SetProperty("WavelengthMax", 900);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "LANDSAT_6BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();
                IPropertySet band5Def = new PropertySetClass();
                IPropertySet band6Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 450);
                band1Def.SetProperty("WavelengthMax", 520);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 520);
                band2Def.SetProperty("WavelengthMax", 600);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 630);
                band3Def.SetProperty("WavelengthMax", 690);

                band4Def.SetProperty("BandName", "NearInfrared_1");
                band4Def.SetProperty("WavelengthMin", 760);
                band4Def.SetProperty("WavelengthMax", 900);

                band5Def.SetProperty("BandName", "NearInfrared_2");
                band5Def.SetProperty("WavelengthMin", 1550);
                band5Def.SetProperty("WavelengthMax", 1750);

                band6Def.SetProperty("BandName", "MidInfrared");
                band6Def.SetProperty("WavelengthMin", 2080);
                band6Def.SetProperty("WavelengthMax", 2350);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
                productDefProps.Add(band5Def);
                productDefProps.Add(band6Def);
            }
            else if (key == "QUICKBIRD_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 450);
                band1Def.SetProperty("WavelengthMax", 520);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 520);
                band2Def.SetProperty("WavelengthMax", 600);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 630);
                band3Def.SetProperty("WavelengthMax", 690);

                band4Def.SetProperty("BandName", "NearInfrared");
                band4Def.SetProperty("WavelengthMin", 760);
                band4Def.SetProperty("WavelengthMax", 900);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "RAPIDEYE_5BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();
                IPropertySet band5Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Blue");
                band1Def.SetProperty("WavelengthMin", 440);
                band1Def.SetProperty("WavelengthMax", 510);

                band2Def.SetProperty("BandName", "Green");
                band2Def.SetProperty("WavelengthMin", 520);
                band2Def.SetProperty("WavelengthMax", 590);

                band3Def.SetProperty("BandName", "Red");
                band3Def.SetProperty("WavelengthMin", 630);
                band3Def.SetProperty("WavelengthMax", 685);

                band4Def.SetProperty("BandName", "RedEdge");
                band4Def.SetProperty("WavelengthMin", 690);
                band4Def.SetProperty("WavelengthMax", 730);

                band5Def.SetProperty("BandName", "NearInfrared");
                band5Def.SetProperty("WavelengthMin", 760);
                band5Def.SetProperty("WavelengthMax", 850);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
                productDefProps.Add(band5Def);
            }
            else if (key == "SPOT-5_4BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "Green");
                band1Def.SetProperty("WavelengthMin", 500);
                band1Def.SetProperty("WavelengthMax", 590);

                band2Def.SetProperty("BandName", "Red");
                band2Def.SetProperty("WavelengthMin", 610);
                band2Def.SetProperty("WavelengthMax", 680);

                band3Def.SetProperty("BandName", "NearInfrared");
                band3Def.SetProperty("WavelengthMin", 780);
                band3Def.SetProperty("WavelengthMax", 890);

                band4Def.SetProperty("BandName", "ShortWaveInfrared");
                band4Def.SetProperty("WavelengthMin", 1580);
                band4Def.SetProperty("WavelengthMax", 1750);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
            }
            else if (key == "WORLDVIEW-2_8BANDS")
            {
                IPropertySet band4Def = new PropertySetClass();
                IPropertySet band5Def = new PropertySetClass();
                IPropertySet band6Def = new PropertySetClass();
                IPropertySet band7Def = new PropertySetClass();
                IPropertySet band8Def = new PropertySetClass();

                band1Def.SetProperty("BandName", "CoastalBlue");
                band1Def.SetProperty("WavelengthMin", 400);
                band1Def.SetProperty("WavelengthMax", 450);

                band2Def.SetProperty("BandName", "Blue");
                band2Def.SetProperty("WavelengthMin", 450);
                band2Def.SetProperty("WavelengthMax", 510);

                band3Def.SetProperty("BandName", "Green");
                band3Def.SetProperty("WavelengthMin", 510);
                band3Def.SetProperty("WavelengthMax", 580);

                band4Def.SetProperty("BandName", "Yellow");
                band4Def.SetProperty("WavelengthMin", 585);
                band4Def.SetProperty("WavelengthMax", 625);

                band5Def.SetProperty("BandName", "Red");
                band5Def.SetProperty("WavelengthMin", 630);
                band5Def.SetProperty("WavelengthMax", 690);

                band6Def.SetProperty("BandName", "RedEdge");
                band6Def.SetProperty("WavelengthMin", 705);
                band6Def.SetProperty("WavelengthMax", 745);

                band7Def.SetProperty("BandName", "NearInfrared_1");
                band7Def.SetProperty("WavelengthMin", 770);
                band7Def.SetProperty("WavelengthMax", 895);

                band8Def.SetProperty("BandName", "NearInfrared_2");
                band8Def.SetProperty("WavelengthMin", 860);
                band8Def.SetProperty("WavelengthMax", 1040);

                productDefProps.Add(band1Def);
                productDefProps.Add(band2Def);
                productDefProps.Add(band3Def);
                productDefProps.Add(band4Def);
                productDefProps.Add(band5Def);
                productDefProps.Add(band6Def);
                productDefProps.Add(band7Def);
                productDefProps.Add(band8Def);
            }
            return productDefProps;
        }
    }
}

[Visual Basic .NET]

CreateMosaicDataset.vb

Imports System.IO
Imports ESRI.ArcGIS
Imports ESRI.ArcGIS.DataSourcesGDB
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry

Namespace RasterSamples
	Class Program
		<STAThread> _
		Friend Shared Sub Main(args As String())
			'#Region "Initialize License"
			Dim aoInit As ESRI.ArcGIS.esriSystem.AoInitialize = Nothing
			Try
				Console.WriteLine("Obtaining license")
				ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)
				aoInit = New AoInitializeClass()
                Dim licStatus As esriLicenseStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced)
				Console.WriteLine("Ready with license.")
			Catch exc As Exception
				' If it fails at this point, shutdown the test and ignore any subsequent errors.
				Console.WriteLine(exc.Message)
			End Try
			'#End Region

			Try
				'#Region "Setup MD Parameters"
                MDParameters.gdbParentFolder = "c:\temp\CreateMD"
                ' Choose which type of gdb to create/open.
                ' 0 - Create FileGDB
                ' 1 - Create PGdb
                ' 2 - Open SDE
                Dim gdbOption As Integer = 0
                ' Provide the proper extension based on the gdb you want to create. 
                ' e.g. MDParameters.gdbName = "samplePGdb.mdb" to create a personal gdb.
                ' To use an SDE, set SDE connection properties below.
                MDParameters.gdbName = "sampleFGdb.gdb"
                MDParameters.mosaicDatasetName = "sampleMD"

                ' Specify the srs of the mosaic dataset
				Dim spatialrefFactory As ISpatialReferenceFactory = New SpatialReferenceEnvironmentClass()
				MDParameters.mosaicDatasetSrs = spatialrefFactory.CreateProjectedCoordinateSystem(CInt(esriSRProjCSType.esriSRProjCS_World_Mercator))
				'ISpatialReference mosaicSrs = spatialrefFactory.CreateGeographicCoordinateSystem(
				'(int)(esriSRGeoCSType.esriSRGeoCS_WGS1984));

				' 0 and PT_UNKNOWN for bits and bands = use defaults.
				MDParameters.mosaicDatasetBands = 0
				MDParameters.mosaicDatasetBits = rstPixelType.PT_UNKNOWN
				MDParameters.configKeyword = ""

				' Product Definition key choices:
				' None
				' NATURAL_COLOR_RGB
				' NATURAL_COLOR_RGBI
				' FALSE_COLOR_IRG
				' FORMOSAT-2_4BANDS
				' GEOEYE-1_4BANDS
				' IKONOS_4BANDS
				' KOMPSAT-2_4BANDS
				' LANDSAT_6BANDS
				' LANDSAT_MSS_4BANDS
				' QUICKBIRD_4BANDS
				' RAPIDEYE_5BANDS
				' SPOT-5_4BANDS
				' WORLDVIEW-2_8BANDS

				' Setting this property ensures any data added to the MD with its
				' metadata defined gets added with the correct band combination.
				MDParameters.productDefinitionKey = "FALSE_COLOR_IRG"

				MDParameters.rasterTypeName = "QuickBird"
                ' The next two properties can be left blank for defaults
                ' The product filter defines which specific product of the raster 
                ' type to add, e.g. To specfiy Quickbird Basic use value "Basic"
                MDParameters.rasterTypeProductFilter = ""

                ' The product name specifies which template to use when adding data.
                ' e.g. "Pansharpen and Multispectral" means both multispectral and 
                ' pansharpened rasters are added to the mosaic dataset.
                MDParameters.rasterTypeProductName = "Multispectral"

				' Data source from which to read the data.
                MDParameters.dataSource = "\\qalab_server\data\Raster\DOTNET\Input\DatasourcesRaster\MosaicDataset\QB\Torino"
                MDParameters.dataSourceFilter = ""
				' No need to set if data source has an srs or if you want to use the MD srs as data source srs.
                MDParameters.dataSourceSrs = Nothing
                MDParameters.buildOverviews = True
				'#End Region

                MDParameters.emptyFgdbFolder = True
                MDParameters.createFgdbParentFolder = False
				'#Region "Empty/Create Output Directory"
				If MDParameters.emptyFgdbFolder Then
					Try
						Console.WriteLine("Emptying Output Directory")
						Directory.Delete(MDParameters.gdbParentFolder, True)
						Directory.CreateDirectory(MDParameters.gdbParentFolder)
					Catch generatedExceptionName As Exception
					End Try
				End If
				If MDParameters.createFgdbParentFolder AndAlso Not System.IO.Directory.Exists(MDParameters.gdbParentFolder) Then
					Console.WriteLine("Creating Output Directory")
					Directory.CreateDirectory(MDParameters.gdbParentFolder)
				End If
				'#End Region

				Dim createMD As New CreateMD()

                If gdbOption = 0 Then
                    '#Region "Create MD in File GDB"
                    Console.WriteLine("Creating File GDB: " & MDParameters.gdbName)
                    Dim fgdbWorkspace As IWorkspace = CreateFileGdbWorkspace(MDParameters.gdbParentFolder, MDParameters.gdbName)
                    '#End Region
                    createMD.CreateMosaicDataset(fgdbWorkspace)
                ElseIf gdbOption = 1 Then
                    '#Region "Create MD in Personal GDB"
                    Console.WriteLine("Creating Personal GDB: " & MDParameters.gdbName)
                    Dim pGdbWorkspace As IWorkspace = CreateAccessWorkspace(MDParameters.gdbParentFolder, MDParameters.gdbName)
                    '#End Region
                    createMD.CreateMosaicDataset(pGdbWorkspace)
                ElseIf gdbOption = 2 Then
                    '#Region "Open SDE GDB"
                    ' Set SDE connection properties.
                    Dim sdeProperties As IPropertySet = New PropertySetClass()
                    sdeProperties.SetProperty("SERVER", "")
                    sdeProperties.SetProperty("INSTANCE", "")
                    sdeProperties.SetProperty("VERSION", "")
                    sdeProperties.SetProperty("USER", "")
                    sdeProperties.SetProperty("PASSWORD", "")
                    sdeProperties.SetProperty("DATABASE", "")
                    Dim sdeWorkspace As IWorkspace = CreateSdeWorkspace(sdeProperties)
                    If sdeWorkspace Is Nothing Then
                        Console.WriteLine("Could not open SDE workspace: ")
                        Return
                    End If

                    '#End Region

                    '#Region "Create MD in SDE"
                    MDParameters.mosaicDatasetName = "sampleMD"
                    '#End Region
                    createMD.CreateMosaicDataset(sdeWorkspace)
                End If

				'#Region "Shutdown"
				Console.WriteLine("Press any key...")
				Console.ReadKey()
				' Shutdown License
					'#End Region
				aoInit.Shutdown()
			Catch exc As Exception
				'#Region "Report"
				Console.WriteLine("Exception Caught in Main: " & exc.Message)
				Console.WriteLine("Shutting down.")
				'#End Region

				'#Region "Shutdown"
				Console.WriteLine("Press any key...")
				Console.ReadKey()
				' Shutdown License
					'#End Region
				aoInit.Shutdown()
			End Try
		End Sub

		''' <summary>
		''' Create a File Geodatabase given the name and parent folder.
		''' </summary>
		''' <param name="gdbParentFolder">Folder to create the new gdb in.</param>
		''' <param name="gdbName">Name of the gdb to be created.</param>
		''' <returns>Workspace reference to the new geodatabase.</returns>
		Public Shared Function CreateFileGdbWorkspace(gdbParentFolder As String, gdbName As String) As IWorkspace
			' Instantiate a file geodatabase workspace factory and create a file geodatabase.
			' The Create method returns a workspace object.
			Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
			Dim workspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
			Dim workspaceName As IWorkspaceName = workspaceFactory.Create(gdbParentFolder, gdbName, Nothing, 0)

			' Cast the workspace name object to the IName interface and open the workspace.
			Dim name As IName = DirectCast(workspaceName, IName)
			Dim workspace As IWorkspace = DirectCast(name.Open(), IWorkspace)
			Return workspace
		End Function

		''' <summary>
		''' Create a Personal Geodatabase given the name and parent folder.
		''' </summary>
		''' <param name="gdbParentFolder">Folder to create the new gdb in.</param>
		''' <param name="gdbName">Name of the gdb to be created.</param>
		''' <returns>Workspace reference to the new geodatabase.</returns>
		Public Shared Function CreateAccessWorkspace(gdbParentFolder As String, gdbName As String) As IWorkspace
			' Instantiate an Access workspace factory and create a personal geodatabase.
			' The Create method returns a workspace object.
			Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
			Dim workspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
			Dim workspaceName As IWorkspaceName = workspaceFactory.Create(gdbParentFolder, gdbName, Nothing, 0)

			' Cast the workspace name object to the IName interface and open the workspace.
			Dim name As IName = DirectCast(workspaceName, IName)
			Dim workspace As IWorkspace = DirectCast(name.Open(), IWorkspace)
			Return workspace
		End Function

		''' <summary>
		''' Retrieves an SDE workspace using the specified property set.
		''' </summary>
		''' <param name="propertySet">The connection parameters.</param>
		''' <returns>An IWorkspace reference to an SDE workspace.</returns>
		Public Shared Function CreateSdeWorkspace(propertySet As IPropertySet) As IWorkspace
			' Create the workspace factory and connect to the workspace.
			Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
			Dim workspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
			Dim workspace As IWorkspace = workspaceFactory.Open(propertySet, 0)
			Return workspace
		End Function

	End Class

	Public NotInheritable Class MDParameters
		Private Sub New()
		End Sub
		' Define the folder in which the GDB resides or is to be created
		Public Shared gdbParentFolder As String
		' Name of the GDB
		Public Shared gdbName As String
		' Configuration keyword for the Gdb (optional)
		Public Shared configKeyword As String

		' Mosaic Dataset Properties
		Public Shared mosaicDatasetName As String
		' Name of the Mosaic Dataset to create.
		Public Shared mosaicDatasetSrs As ISpatialReference
		' Srs for the Mosaic Dataset
		Public Shared mosaicDatasetBands As Integer
		' Number of bands of the Mosaic Dataset
		Public Shared mosaicDatasetBits As rstPixelType
		' Pixel Type of the Mosaic Dataset
		Public Shared productDefinitionKey As String
		' The product definition key.
		Public Shared productDefinitionProps As IArray
		' Properties of the product definition (Bands names and wavelengths).
		' Raster Type Properties
		Public Shared rasterTypeName As String
		' Name of the Raster type to use (or path to the .art file)
		Public Shared rasterTypeProductFilter As String
		' The product filter to set on the Raster Type
		Public Shared rasterTypeProductName As String
		' The name of the product to create from the added data
		Public Shared rasterTypeAddDEM As Boolean
		' Flag to specify whether to add a DEM to a Raster Type.
		Public Shared rasterTypeDemPath As String
		' Path to the DEM if previous property is true.
		' Crawler Properties
		Public Shared dataSource As String
		' Path to the data.
		Public Shared dataSourceFilter As String
		' File filter to use to crawl data.
		' Srs of the input data if the input does not contain an srs 
		' and it is different from the srs of the mosaic dataset(optional)
		Public Shared dataSourceSrs As ISpatialReference

		' Operational flags
		Public Shared buildOverviews As Boolean
		' Generate overviews for the Mosaic Dataset
		Public Shared emptyFgdbFolder As Boolean
		' Delete the parent folder for the GDB
		Public Shared createFgdbParentFolder As Boolean
		' Create the Parent folder for the GDB
	End Class

    Public Class CreateMD
        ''' <summary>
        ''' Create a Mosaic Dataset in the geodatabase provided using the parameters defined by MDParamaters.
        ''' </summary>
        ''' <param name="gdbWorkspace">Geodatabase to create the Mosaic dataser in.</param>
        Public Sub CreateMosaicDataset(ByVal gdbWorkspace As IWorkspace)
            Try
                '#Region "Global Declarations"
                Dim theMosaicDataset As IMosaicDataset = Nothing
                Dim theMosaicDatasetOperation As IMosaicDatasetOperation = Nothing
                Dim mosaicExtHelper As IMosaicWorkspaceExtensionHelper = Nothing
                Dim mosaicExt As IMosaicWorkspaceExtension = Nothing
                '#End Region

                '#Region "CreateMosaicDataset"
                Try
                    Console.WriteLine("Create Mosaic Dataset: " & MDParameters.mosaicDatasetName & ".amd")
                    ' Create the mosaic dataset creation parameters object.
                    Dim creationPars As ICreateMosaicDatasetParameters = New CreateMosaicDatasetParametersClass()
                    ' Set the number of bands for the mosaic dataset.
                    ' If defined as zero leave defaults
                    If MDParameters.mosaicDatasetBands <> 0 Then
                        creationPars.BandCount = MDParameters.mosaicDatasetBands
                    End If
                    ' Set the pixel type of the mosaic dataset.
                    ' If defined as unknown leave defaults
                    If MDParameters.mosaicDatasetBits <> rstPixelType.PT_UNKNOWN Then
                        creationPars.PixelType = MDParameters.mosaicDatasetBits
                    End If
                    ' Create the mosaic workspace extension helper class.
                    mosaicExtHelper = New MosaicWorkspaceExtensionHelperClass()
                    ' Find the right extension from the workspace.
                    mosaicExt = mosaicExtHelper.FindExtension(gdbWorkspace)

                    ' Default is none.
                    If MDParameters.productDefinitionKey.ToLower() <> "none" Then
                        ' Set the product definition keyword and properties.
                        ' (The property is called band definition keyword and band properties in the object).
                        DirectCast(creationPars, ICreateMosaicDatasetParameters2).BandDefinitionKeyword = MDParameters.productDefinitionKey
                        MDParameters.productDefinitionProps = SetBandProperties(MDParameters.productDefinitionKey)
                        If MDParameters.productDefinitionProps.Count = 0 Then
                            Console.WriteLine("Setting production definition properties failed.")
                            Return
                        End If
                        DirectCast(creationPars, ICreateMosaicDatasetParameters2).BandProperties = MDParameters.productDefinitionProps
                    End If

                    ' Use the extension to create a new mosaic dataset, supplying the 
                    ' spatial reference and the creation parameters object created above.
                    theMosaicDataset = mosaicExt.CreateMosaicDataset(MDParameters.mosaicDatasetName, MDParameters.mosaicDatasetSrs, creationPars, MDParameters.configKeyword)
                Catch exc As Exception
                    Console.WriteLine("Exception Caught while creating Mosaic Dataset: " & exc.Message)
                    Return
                End Try
                '#End Region

                '#Region "OpenMosaicDataset"
                Console.WriteLine("Opening Mosaic Dataset")
                theMosaicDataset = Nothing
                ' Use the extension to open the mosaic dataset.
                theMosaicDataset = mosaicExt.OpenMosaicDataset(MDParameters.mosaicDatasetName)
                ' The mosaic dataset operation interface is used to perform operations on 
                ' a mosaic dataset.
                theMosaicDatasetOperation = DirectCast(theMosaicDataset, IMosaicDatasetOperation)
                '#End Region

                '#Region "Preparing Raster Type"
                Console.WriteLine("Preparing Raster Type")
                ' Create a Raster Type Name object.
                Dim theRasterTypeName As IRasterTypeName = New RasterTypeNameClass()
                ' Assign the name of the Raster Type to the name object.
                ' The Name field accepts a path to an .art file as well 
                ' the name for a built in Raster Type.
                theRasterTypeName.Name = MDParameters.rasterTypeName
                ' Use the Open function from the IName interface to get the Raster Type object.
                Dim theRasterType As IRasterType = DirectCast(DirectCast(theRasterTypeName, IName).Open(), IRasterType)
                If theRasterType Is Nothing Then
                    Console.WriteLine("Raster Type not found " & MDParameters.rasterTypeName)
                End If

                ' Set the URI Filter on the loaded raster type.
                If MDParameters.rasterTypeProductFilter <> "" Then
                    ' Get the supported URI filters from the raster type object using the 
                    ' raster type properties interface.
                    Dim mySuppFilters As IArray = DirectCast(theRasterType, IRasterTypeProperties).SupportedURIFilters
                    Dim productFilter As IItemURIFilter = Nothing
                    For i As Integer = 0 To mySuppFilters.Count - 1
                        ' Set the desired filter from the supported filters.
                        productFilter = DirectCast(mySuppFilters.Element(i), IItemURIFilter)
                        If productFilter.Name = MDParameters.rasterTypeProductFilter Then
                            theRasterType.URIFilter = productFilter
                        End If
                    Next
                End If
                ' Enable the correct templates in the raster type.
                Dim rasterProductNames As String() = MDParameters.rasterTypeProductName.Split(";"c)
                Dim enableTemplate As Boolean = False
                If rasterProductNames.Length >= 1 AndAlso (rasterProductNames(0) <> "") Then
                    ' Get the supported item templates from the raster type.
                    Dim templateArray As IItemTemplateArray = theRasterType.ItemTemplates
                    For i As Integer = 0 To templateArray.Count - 1
                        ' Go through the supported item templates and enable the ones needed.
                        Dim template As IItemTemplate = templateArray.Element(i)
                        enableTemplate = False
                        For j As Integer = 0 To rasterProductNames.Length - 1

                            If template.Name = rasterProductNames(j) Then
                                enableTemplate = True
                            End If
                        Next
                        If enableTemplate Then
                            template.Enabled = True
                        Else
                            template.Enabled = False
                        End If
                    Next
                End If

                If MDParameters.dataSourceSrs IsNot Nothing Then
                    DirectCast(theRasterType, IRasterTypeProperties).SynchronizeParameters.DefaultSpatialReference = MDParameters.dataSourceSrs
                End If
                '#End Region

                '#Region "Add DEM To Raster Type"
                If MDParameters.rasterTypeAddDEM AndAlso DirectCast(theRasterType, IRasterTypeProperties).SupportsOrthorectification Then
                    ' Open the Raster Dataset
                    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory")
                    Dim workspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
                    Dim rasterWorkspace As IRasterWorkspace = DirectCast(workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(MDParameters.rasterTypeDemPath), 0), IRasterWorkspace)


                    Dim myRasterDataset As IRasterDataset = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(MDParameters.rasterTypeDemPath))

                    Dim geometricFunctionArguments As IGeometricFunctionArguments = New GeometricFunctionArgumentsClass()
                    geometricFunctionArguments.DEM = myRasterDataset
                    DirectCast(theRasterType, IRasterTypeProperties).OrthorectificationParameters = geometricFunctionArguments
                End If
                '#End Region

                '#Region "Preparing Data Source Crawler"
                Console.WriteLine("Preparing Data Source Crawler")
                ' Create a new property set to specify crawler properties.
                Dim crawlerProps As IPropertySet = New PropertySetClass()
                ' Specify a file filter
                crawlerProps.SetProperty("Filter", MDParameters.dataSourceFilter)
                ' Specify whether to search subdirectories.
                crawlerProps.SetProperty("Recurse", True)
                ' Specify the source path.
                crawlerProps.SetProperty("Source", MDParameters.dataSource)
                ' Get the recommended crawler from the raster type based on the specified 
                ' properties using the IRasterBuilder interface.
                Dim theCrawler As IDataSourceCrawler = DirectCast(theRasterType, IRasterBuilder).GetRecommendedCrawler(crawlerProps)
                '#End Region

                '#Region "Add Rasters"
                Console.WriteLine("Adding Rasters")
                ' Create a AddRaster parameters object.
                Dim AddRastersArgs As IAddRastersParameters = New AddRastersParametersClass()
                ' Specify the data crawler to be used to crawl the data.
                AddRastersArgs.Crawler = theCrawler
                ' Specify the raster type to be used to add the data.
                AddRastersArgs.RasterType = theRasterType
                ' Use the mosaic dataset operation interface to add 
                ' rasters to the mosaic dataset.
                theMosaicDatasetOperation.AddRasters(AddRastersArgs, Nothing)
                '#End Region

                '#Region "Compute Pixel Size Ranges"
                Console.WriteLine("Computing Pixel Size Ranges")
                ' Create a calculate cellsize ranges parameters object.
                Dim computeArgs As ICalculateCellSizeRangesParameters = New CalculateCellSizeRangesParametersClass()
                ' Use the mosaic dataset operation interface to calculate cellsize ranges.
                theMosaicDatasetOperation.CalculateCellSizeRanges(computeArgs, Nothing)
                '#End Region

                '#Region "Building Boundary"
                Console.WriteLine("Building Boundary")
                ' Create a build boundary parameters object.
                Dim boundaryArgs As IBuildBoundaryParameters = New BuildBoundaryParametersClass()
                ' Set flags that control boundary generation.
                boundaryArgs.AppendToExistingBoundary = True
                ' Use the mosaic dataset operation interface to build boundary.
                theMosaicDatasetOperation.BuildBoundary(boundaryArgs, Nothing)
                '#End Region

                If MDParameters.buildOverviews Then
                    '#Region "Defining Overviews"
                    Console.WriteLine("Defining Overviews")
                    ' Create a define overview parameters object.
                    Dim defineOvArgs As IDefineOverviewsParameters = New DefineOverviewsParametersClass()
                    ' Use the overview tile parameters interface to specify the overview factor
                    ' used to generate overviews.
                    DirectCast(defineOvArgs, IOverviewTileParameters).OverviewFactor = 3
                    ' Use the mosaic dataset operation interface to define overviews.
                    theMosaicDatasetOperation.DefineOverviews(defineOvArgs, Nothing)
                    '#End Region

                    '#Region "Compute Pixel Size Ranges"
                    Console.WriteLine("Computing Pixel Size Ranges")
                    ' Calculate cell size ranges to update the Min/Max pixel sizes.
                    theMosaicDatasetOperation.CalculateCellSizeRanges(computeArgs, Nothing)
                    '#End Region

                    '#Region "Generating Overviews"
                    Console.WriteLine("Generating Overviews")
                    ' Create a generate overviews parameters object.
                    Dim genPars As IGenerateOverviewsParameters = New GenerateOverviewsParametersClass()
                    ' Set properties to control overview generation.
                    Dim genQuery As IQueryFilter = New QueryFilterClass()
                    DirectCast(genPars, ISelectionParameters).QueryFilter = genQuery
                    genPars.GenerateMissingImages = True
                    genPars.GenerateStaleImages = True
                    ' Use the mosaic dataset operation interface to generate overviews.
                    '#End Region
                    theMosaicDatasetOperation.GenerateOverviews(genPars, Nothing)
                End If

                '#Region "Report"
                '#End Region
                Console.WriteLine("Success.")
            Catch exc As Exception
                '#Region "Report"
                Console.WriteLine("Exception Caught in CreateMD: " & exc.Message)
                '#End Region
                Console.WriteLine("Shutting down.")
            End Try
        End Sub

        ''' <summary>
        ''' Create an array with the right BandName and Wavelength values for the corresponding key.
        ''' </summary>
        ''' <param name="key">Key to use.</param>
        ''' <returns>Array with the correct BandName and Wavelength values.</returns>
        Private Shared Function SetBandProperties(ByVal key As String) As IArray
            Dim productDefProps As IArray = New ArrayClass()
            Dim band1Def As IPropertySet = New PropertySetClass()
            Dim band2Def As IPropertySet = New PropertySetClass()
            Dim band3Def As IPropertySet = New PropertySetClass()
            If key = "NATURAL_COLOR_RGB" OrElse key = "NATURAL_COLOR_RGBI" Then
                band1Def.SetProperty("BandName", "Red")
                band1Def.SetProperty("WavelengthMin", 630)
                band1Def.SetProperty("WavelengthMax", 690)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 530)
                band2Def.SetProperty("WavelengthMax", 570)

                band3Def.SetProperty("BandName", "Blue")
                band3Def.SetProperty("WavelengthMin", 440)
                band3Def.SetProperty("WavelengthMax", 480)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)

                If key = "NATURAL_COLOR_RGBI" Then
                    Dim band4Def As IPropertySet = New PropertySetClass()
                    band4Def.SetProperty("BandName", "NearInfrared")
                    band4Def.SetProperty("WavelengthMin", 770)
                    band4Def.SetProperty("WavelengthMax", 830)
                    productDefProps.Add(band4Def)
                End If
            ElseIf key = "FALSE_COLOR_IRG" Then
                band1Def.SetProperty("BandName", "Infrared")
                band1Def.SetProperty("WavelengthMin", 770)
                band1Def.SetProperty("WavelengthMax", 830)

                band2Def.SetProperty("BandName", "Red")
                band2Def.SetProperty("WavelengthMin", 630)
                band2Def.SetProperty("WavelengthMax", 690)

                band3Def.SetProperty("BandName", "Green")
                band3Def.SetProperty("WavelengthMin", 530)
                band3Def.SetProperty("WavelengthMax", 570)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
            ElseIf key = "FORMOSAT-2_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 450)
                band1Def.SetProperty("WavelengthMax", 520)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 520)
                band2Def.SetProperty("WavelengthMax", 600)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 630)
                band3Def.SetProperty("WavelengthMax", 690)

                band4Def.SetProperty("BandName", "NearInfrared")
                band4Def.SetProperty("WavelengthMin", 760)
                band4Def.SetProperty("WavelengthMax", 900)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "GEOEYE-1_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 450)
                band1Def.SetProperty("WavelengthMax", 510)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 510)
                band2Def.SetProperty("WavelengthMax", 580)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 655)
                band3Def.SetProperty("WavelengthMax", 690)

                band4Def.SetProperty("BandName", "NearInfrared")
                band4Def.SetProperty("WavelengthMin", 780)
                band4Def.SetProperty("WavelengthMax", 920)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "IKONOS_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 445)
                band1Def.SetProperty("WavelengthMax", 516)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 506)
                band2Def.SetProperty("WavelengthMax", 595)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 632)
                band3Def.SetProperty("WavelengthMax", 698)

                band4Def.SetProperty("BandName", "NearInfrared")
                band4Def.SetProperty("WavelengthMin", 757)
                band4Def.SetProperty("WavelengthMax", 863)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "KOMPSAT-2_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 450)
                band1Def.SetProperty("WavelengthMax", 520)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 520)
                band2Def.SetProperty("WavelengthMax", 600)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 630)
                band3Def.SetProperty("WavelengthMax", 690)

                band4Def.SetProperty("BandName", "NearInfrared")
                band4Def.SetProperty("WavelengthMin", 760)
                band4Def.SetProperty("WavelengthMax", 900)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "LANDSAT_6BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()
                Dim band5Def As IPropertySet = New PropertySetClass()
                Dim band6Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 450)
                band1Def.SetProperty("WavelengthMax", 520)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 520)
                band2Def.SetProperty("WavelengthMax", 600)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 630)
                band3Def.SetProperty("WavelengthMax", 690)

                band4Def.SetProperty("BandName", "NearInfrared_1")
                band4Def.SetProperty("WavelengthMin", 760)
                band4Def.SetProperty("WavelengthMax", 900)

                band5Def.SetProperty("BandName", "NearInfrared_2")
                band5Def.SetProperty("WavelengthMin", 1550)
                band5Def.SetProperty("WavelengthMax", 1750)

                band6Def.SetProperty("BandName", "MidInfrared")
                band6Def.SetProperty("WavelengthMin", 2080)
                band6Def.SetProperty("WavelengthMax", 2350)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
                productDefProps.Add(band5Def)
                productDefProps.Add(band6Def)
            ElseIf key = "QUICKBIRD_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 450)
                band1Def.SetProperty("WavelengthMax", 520)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 520)
                band2Def.SetProperty("WavelengthMax", 600)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 630)
                band3Def.SetProperty("WavelengthMax", 690)

                band4Def.SetProperty("BandName", "NearInfrared")
                band4Def.SetProperty("WavelengthMin", 760)
                band4Def.SetProperty("WavelengthMax", 900)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "RAPIDEYE_5BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()
                Dim band5Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Blue")
                band1Def.SetProperty("WavelengthMin", 440)
                band1Def.SetProperty("WavelengthMax", 510)

                band2Def.SetProperty("BandName", "Green")
                band2Def.SetProperty("WavelengthMin", 520)
                band2Def.SetProperty("WavelengthMax", 590)

                band3Def.SetProperty("BandName", "Red")
                band3Def.SetProperty("WavelengthMin", 630)
                band3Def.SetProperty("WavelengthMax", 685)

                band4Def.SetProperty("BandName", "RedEdge")
                band4Def.SetProperty("WavelengthMin", 690)
                band4Def.SetProperty("WavelengthMax", 730)

                band5Def.SetProperty("BandName", "NearInfrared")
                band5Def.SetProperty("WavelengthMin", 760)
                band5Def.SetProperty("WavelengthMax", 850)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
                productDefProps.Add(band5Def)
            ElseIf key = "SPOT-5_4BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "Green")
                band1Def.SetProperty("WavelengthMin", 500)
                band1Def.SetProperty("WavelengthMax", 590)

                band2Def.SetProperty("BandName", "Red")
                band2Def.SetProperty("WavelengthMin", 610)
                band2Def.SetProperty("WavelengthMax", 680)

                band3Def.SetProperty("BandName", "NearInfrared")
                band3Def.SetProperty("WavelengthMin", 780)
                band3Def.SetProperty("WavelengthMax", 890)

                band4Def.SetProperty("BandName", "ShortWaveInfrared")
                band4Def.SetProperty("WavelengthMin", 1580)
                band4Def.SetProperty("WavelengthMax", 1750)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
            ElseIf key = "WORLDVIEW-2_8BANDS" Then
                Dim band4Def As IPropertySet = New PropertySetClass()
                Dim band5Def As IPropertySet = New PropertySetClass()
                Dim band6Def As IPropertySet = New PropertySetClass()
                Dim band7Def As IPropertySet = New PropertySetClass()
                Dim band8Def As IPropertySet = New PropertySetClass()

                band1Def.SetProperty("BandName", "CoastalBlue")
                band1Def.SetProperty("WavelengthMin", 400)
                band1Def.SetProperty("WavelengthMax", 450)

                band2Def.SetProperty("BandName", "Blue")
                band2Def.SetProperty("WavelengthMin", 450)
                band2Def.SetProperty("WavelengthMax", 510)

                band3Def.SetProperty("BandName", "Green")
                band3Def.SetProperty("WavelengthMin", 510)
                band3Def.SetProperty("WavelengthMax", 580)

                band4Def.SetProperty("BandName", "Yellow")
                band4Def.SetProperty("WavelengthMin", 585)
                band4Def.SetProperty("WavelengthMax", 625)

                band5Def.SetProperty("BandName", "Red")
                band5Def.SetProperty("WavelengthMin", 630)
                band5Def.SetProperty("WavelengthMax", 690)

                band6Def.SetProperty("BandName", "RedEdge")
                band6Def.SetProperty("WavelengthMin", 705)
                band6Def.SetProperty("WavelengthMax", 745)

                band7Def.SetProperty("BandName", "NearInfrared_1")
                band7Def.SetProperty("WavelengthMin", 770)
                band7Def.SetProperty("WavelengthMax", 895)

                band8Def.SetProperty("BandName", "NearInfrared_2")
                band8Def.SetProperty("WavelengthMin", 860)
                band8Def.SetProperty("WavelengthMax", 1040)

                productDefProps.Add(band1Def)
                productDefProps.Add(band2Def)
                productDefProps.Add(band3Def)
                productDefProps.Add(band4Def)
                productDefProps.Add(band5Def)
                productDefProps.Add(band6Def)
                productDefProps.Add(band7Def)
                productDefProps.Add(band8Def)
            End If
            Return productDefProps
        End Function
    End Class
End Namespace