Beispiel #1
0
        private static IDoubleArray GetDoubleArray([NotNull] IEnumerable <double> values)
        {
            Assert.ArgumentNotNull(values, nameof(values));

            IDoubleArray result = new DoubleArrayClass();

            foreach (double distance in values)
            {
                result.Add(distance);
            }

            return(result);
        }
        /// <summary>
        /// Prepare the text string for breaks
        /// </summary>
        private IDoubleArray ParseBreaks(string p)
        {
            String[]     breaks     = p.Split(' ');
            IDoubleArray pBrks      = new DoubleArrayClass();
            int          firstIndex = breaks.GetLowerBound(0);
            int          lastIndex  = breaks.GetUpperBound(0);

            for (int splitIndex = firstIndex; splitIndex <= lastIndex; splitIndex++)
            {
                try
                {
                    pBrks.Add(Convert.ToDouble(breaks[splitIndex]));
                }
                catch (FormatException)
                {
                    MessageBox.Show("Breaks are not properly formatted.  Use only digits separated by spaces");
                    pBrks.RemoveAll();
                    return(pBrks);
                }
            }

            return(pBrks);
        }
        /// <summary>
        /// Prepare the text string for breaks
        /// </summary>
        private IDoubleArray ParseBreaks(string p)
		{
			String[] breaks = p.Split(' ');
			IDoubleArray pBrks = new DoubleArrayClass();
			int firstIndex = breaks.GetLowerBound(0);
			int lastIndex = breaks.GetUpperBound(0);
			for (int splitIndex = firstIndex; splitIndex <= lastIndex; splitIndex++)
			{
				try
				{
					pBrks.Add(Convert.ToDouble(breaks[splitIndex]));
				}
				catch (FormatException)
				{
					MessageBox.Show("Breaks are not properly formatted.  Use only digits separated by spaces");
					pBrks.RemoveAll();
					return pBrks;
				}
			}

			return pBrks;
		}
Beispiel #4
0
        public static bool ConvertPolygon(double[] vtx, double height, int flag, out IDoubleArray VArray, out IUInt16Array IndexArray, out IFloatArray TextureArrayU1V1, out IDoubleArray Norms)
        {
            bool flag2;

            VArray           = new DoubleArrayClass();
            IndexArray       = new UInt16ArrayClass();
            TextureArrayU1V1 = new FloatArrayClass();
            IFloatArray array = null;

            Norms = new DoubleArrayClass();
            IPolygon polygon = null;
            ITriMesh o       = null;

            try
            {
                if (((vtx == null) || ((vtx.Length % 2) != 0)) || (vtx.Length < 8))
                {
                    return(false);
                }
                polygon = geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZ) as IPolygon;
                if (polygon == null)
                {
                    return(false);
                }
                int num = vtx.Length / 2;
                for (int i = 0; i < num; i++)
                {
                    IPoint pointValue = geoFactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ);
                    if (flag == 1)
                    {
                        pointValue.X = vtx[i * 2];
                        pointValue.Y = vtx[(i * 2) + 1];
                        pointValue.Z = height;
                    }
                    else
                    {
                        pointValue.X = vtx[((num - i) - 1) * 2];
                        pointValue.Y = vtx[(((num - i) - 1) * 2) + 1];
                        pointValue.Z = height;
                    }
                    polygon.ExteriorRing.AppendPoint(pointValue);
                }
                if (!polygon.IsClosed)
                {
                    polygon.Close();
                }
                o = geoConvertor.PolygonToTriMesh(polygon);
                if (o == null)
                {
                    return(false);
                }
                if (!o.BatchExport(ref VArray, ref IndexArray, ref TextureArrayU1V1, ref array, ref Norms))
                {
                    return(false);
                }
                flag2 = true;
            }
            catch (Exception)
            {
                flag2 = false;
            }
            finally
            {
                if (polygon != null)
                {
                    Marshal.ReleaseComObject(polygon);
                }
                if (o != null)
                {
                    Marshal.ReleaseComObject(o);
                }
            }
            return(flag2);
        }
        public override bool Draw(out IModelPoint mp, out IModel fmodel, out IModel smodel)
        {
            List <ushort>  list2      = new List <ushort>();
            IPolygon       polygon    = null;
            IPolygon       polygon2   = null;
            IPolygon       polygon3   = null;
            IPoint         pointValue = null;
            ITriMesh       mesh       = null;
            IDoubleArray   vArray     = null;
            IUInt16Array   indexArray = null;
            IFloatArray    array3     = null;
            IFloatArray    array4     = null;
            IDoubleArray   norms      = null;
            IDrawGroup     group      = null;
            IDrawPrimitive primitive  = null;
            IDrawPrimitive primitive2 = null;
            IDrawPrimitive primitive3 = null;
            double         naN        = double.NaN;
            double         ty         = double.NaN;

            base.Draw(out mp, out fmodel, out smodel);
            try
            {
                int           num;
                int           num2;
                List <Vector> list;
                polygon  = DrawGeometry.geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZ) as IPolygon;
                polygon2 = DrawGeometry.geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZ) as IPolygon;
                polygon3 = DrawGeometry.geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZ) as IPolygon;
                if (this._route.Count > 2)
                {
                    list = Maths.DisperseLine(this._route, this._pillarSection.Diameter);
                }
                else
                {
                    list = this._route;
                }
                double[] vtxs = this._pillarSection.GetVtxs();
                SystemLog.Instance.Log("顶点个数:" + vtxs.Length);
                double[] numArray = new double[list.Count];
                for (num = 0; num < list.Count; num++)
                {
                    if (num == 0)
                    {
                        numArray[num] = 0.0;
                    }
                    else
                    {
                        numArray[num] = (list[num] - list[num - 1]).Length + numArray[num - 1];
                    }
                }
                object renderInfo = (this._renderType == RenderType.Texture) ? ((object)this._tcNames) : ((object)this._colors);
                int[]  index      = new int[3];
                index[1] = 1;
                index[2] = 2;
                if (!base.NewEmptyModel(index, this._renderType, renderInfo, out fmodel))
                {
                    return(false);
                }
                group = fmodel.GetGroup(0);
                SystemLog.Instance.Log("开始计算正交向量:" + DateTime.Now.ToLongTimeString());
                primitive = group.GetPrimitive(0);
                for (num = 0; num < list.Count; num++)
                {
                    Vector vector;
                    Vector vector2;
                    Vector vector3;
                    if (num == 0)
                    {
                        vector = list[num + 1] - list[num];
                    }
                    else if (num == (list.Count - 1))
                    {
                        vector = list[num] - list[num - 1];
                    }
                    else
                    {
                        vector = ((list[num] - list[num - 1])).UnitVector() + ((list[num + 1] - list[num])).UnitVector();
                    }
                    Maths.GenerateComplementBasis(vector, out vector2, out vector3);
                    vector2 = -vector2;
                    num2    = 0;
                    while (num2 <= this._pillarSection.SegCount)
                    {
                        Vector vector6;
                        Vector vector7;
                        (((vtxs[num2 * 2] - this._pillarSection.OffsetX) * vector2) + ((vtxs[(num2 * 2) + 1] - this._pillarSection.OffsetY) * vector3)).UnitVector();
                        Vector vector4 = list[num] + ((Vector)((vtxs[num2 * 2] * vector2) + (vtxs[(num2 * 2) + 1] * vector3)));
                        Vector vector5 = (Vector)(vector4 * 1.01);
                        if (num == 0)
                        {
                            pointValue = DrawGeometry.geoFactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ);
                            pointValue.SetCoords(vector4.X, vector4.Y, vector4.Z, 0.0, 0);
                            polygon.ExteriorRing.AppendPoint(pointValue);
                            pointValue = DrawGeometry.geoFactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ);
                            pointValue.SetCoords(vector5.X, vector5.Y, vector5.Z, 0.0, 0);
                            polygon3.ExteriorRing.AppendPoint(pointValue);
                        }
                        else if (num == (list.Count - 1))
                        {
                            pointValue = DrawGeometry.geoFactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ);
                            pointValue.SetCoords(vector4.X, vector4.Y, vector4.Z, 0.0, 0);
                            polygon2.ExteriorRing.AppendPoint(pointValue);
                        }
                        primitive.VertexArray.Append((float)vector4.X);
                        primitive.VertexArray.Append((float)vector4.Y);
                        primitive.VertexArray.Append((float)vector4.Z);
                        list2.Add((ushort)((primitive.VertexArray.Length / 3) - 1));
                        primitive.VertexArray.Append((float)vector4.X);
                        primitive.VertexArray.Append((float)vector4.Y);
                        primitive.VertexArray.Append((float)vector4.Z);
                        list2.Add((ushort)((primitive.VertexArray.Length / 3) - 1));
                        if (this._renderType == RenderType.Texture)
                        {
                            primitive.TexcoordArray.Append((float)(num2 * 1.0));
                            primitive.TexcoordArray.Append((float)(num * 1.0));
                            primitive.TexcoordArray.Append((float)(num2 * 1.0));
                            primitive.TexcoordArray.Append((float)(num * 1.0));
                        }
                        if (num2 == 0)
                        {
                            vector6 = new Vector(vtxs[num2 * 2] - vtxs[(this._pillarSection.SegCount - 1) * 2], vtxs[(num2 * 2) + 1] - vtxs[((this._pillarSection.SegCount - 1) * 2) + 1], 0.0).UnitVector();
                            vector7 = new Vector(vtxs[(num2 + 1) * 2] - vtxs[num2 * 2], vtxs[((num2 + 1) * 2) + 1] - vtxs[(num2 * 2) + 1], 0.0).UnitVector();
                        }
                        else if (num2 == this._pillarSection.SegCount)
                        {
                            vector6 = new Vector(vtxs[num2 * 2] - vtxs[(num2 - 1) * 2], vtxs[(num2 * 2) + 1] - vtxs[((num2 - 1) * 2) + 1], 0.0).UnitVector();
                            vector7 = new Vector(vtxs[2] - vtxs[num2 * 2], vtxs[3] - vtxs[(num2 * 2) + 1], 0.0).UnitVector();
                        }
                        else
                        {
                            vector6 = new Vector(vtxs[num2 * 2] - vtxs[(num2 - 1) * 2], vtxs[(num2 * 2) + 1] - vtxs[((num2 - 1) * 2) + 1], 0.0).UnitVector();
                            vector7 = new Vector(vtxs[(num2 + 1) * 2] - vtxs[num2 * 2], vtxs[((num2 + 1) * 2) + 1] - vtxs[(num2 * 2) + 1], 0.0).UnitVector();
                        }
                        Vector vector8 = new Vector(vector6.Y, -vector6.X, vector6.Z);
                        primitive.NormalArray.Append((float)vector8.X);
                        primitive.NormalArray.Append((float)vector8.Y);
                        primitive.NormalArray.Append((float)vector8.Z);
                        vector8 = new Vector(vector7.Y, -vector7.X, vector7.Z);
                        primitive.NormalArray.Append((float)vector8.X);
                        primitive.NormalArray.Append((float)vector8.Y);
                        primitive.NormalArray.Append((float)vector8.Z);
                        num2++;
                    }
                }
                SystemLog.Instance.Log("结束计算正交向量:" + DateTime.Now.ToLongTimeString());
                SystemLog.Instance.Log("开始添加索引数组:" + DateTime.Now.ToLongTimeString());
                for (num = 0; num < (list.Count - 1); num++)
                {
                    for (num2 = 0; num2 < this._pillarSection.SegCount; num2++)
                    {
                        primitive.IndexArray.Append(list2[((num * ((this._pillarSection.SegCount + 1) * 2)) + (num2 * 2)) + 1]);
                        primitive.IndexArray.Append(list2[((num + 1) * ((this._pillarSection.SegCount + 1) * 2)) + ((num2 + 1) * 2)]);
                        primitive.IndexArray.Append(list2[(((num + 1) * ((this._pillarSection.SegCount + 1) * 2)) + (num2 * 2)) + 1]);
                        primitive.IndexArray.Append(list2[((num * ((this._pillarSection.SegCount + 1) * 2)) + (num2 * 2)) + 1]);
                        primitive.IndexArray.Append(list2[(num * ((this._pillarSection.SegCount + 1) * 2)) + ((num2 + 1) * 2)]);
                        primitive.IndexArray.Append(list2[((num + 1) * ((this._pillarSection.SegCount + 1) * 2)) + ((num2 + 1) * 2)]);
                    }
                }
                SystemLog.Instance.Log("结束添加索引数组:" + DateTime.Now.ToLongTimeString());
                SystemLog.Instance.Log("开始画底面顶面和线框:" + DateTime.Now.ToLongTimeString());
                vArray     = new DoubleArrayClass();
                indexArray = new UInt16ArrayClass();
                array3     = new FloatArrayClass();
                norms      = new DoubleArrayClass();
                primitive2 = group.GetPrimitive(1);
                polygon.ExteriorRing.ReverseOrientation();
                mesh = DrawGeometry.geoConvertor.PolygonToTriMesh(polygon);
                if ((mesh != null) && mesh.BatchExport(ref vArray, ref indexArray, ref array3, ref array4, ref norms))
                {
                    for (num = 0; num < vArray.Length; num++)
                    {
                        primitive2.VertexArray.Append((float)vArray.Array[num]);
                    }
                    for (num = 0; num < indexArray.Length; num++)
                    {
                        primitive2.IndexArray.Append(indexArray.Array[num]);
                    }
                    for (num = 0; num < norms.Length; num++)
                    {
                        primitive2.NormalArray.Append((float)norms.Array[num]);
                    }
                    if (this._renderType == RenderType.Texture)
                    {
                        for (num = 0; num < (vArray.Length / 3); num++)
                        {
                            this.GetTexcoord(vArray.Array[num * 3], vArray.Array[(num * 3) + 1], out naN, out ty);
                            primitive2.TexcoordArray.Append((float)naN);
                            primitive2.TexcoordArray.Append((float)ty);
                        }
                    }
                }
                vArray     = new DoubleArrayClass();
                indexArray = new UInt16ArrayClass();
                array3     = new FloatArrayClass();
                norms      = new DoubleArrayClass();
                primitive3 = group.GetPrimitive(2);
                mesh       = DrawGeometry.geoConvertor.PolygonToTriMesh(polygon2);
                if ((mesh != null) && mesh.BatchExport(ref vArray, ref indexArray, ref array3, ref array4, ref norms))
                {
                    for (num = 0; num < vArray.Length; num++)
                    {
                        primitive3.VertexArray.Append((float)vArray.Array[num]);
                    }
                    for (num = 0; num < indexArray.Length; num++)
                    {
                        primitive3.IndexArray.Append(indexArray.Array[num]);
                    }
                    for (num = 0; num < norms.Length; num++)
                    {
                        primitive3.NormalArray.Append((float)norms.Array[num]);
                    }
                    if (this._renderType == RenderType.Texture)
                    {
                        for (num = 0; num < (vArray.Length / 3); num++)
                        {
                            this.GetTexcoord(vArray.Array[num * 3], vArray.Array[(num * 3) + 1], out naN, out ty);
                            primitive3.TexcoordArray.Append((float)naN);
                            primitive3.TexcoordArray.Append((float)ty);
                        }
                    }
                }
                SystemLog.Instance.Log("结束画底面顶面和线框:" + DateTime.Now.ToLongTimeString());
                SystemLog.Instance.Log("结束调用Draw:" + DateTime.Now.ToLongTimeString());
                return(true);
            }
            catch (Exception exception)
            {
                SystemLog.Instance.Log(exception);
                return(false);
            }
        }
        static void Main(string[] args)
        {
            ESRI.ArcGIS.esriSystem.AoInitialize aoInit;

            #region Initialize License
            try
            {
                Console.WriteLine("Obtaining license");
                ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
                aoInit = new AoInitializeClass();
                esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
                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);
                return;
            }
            #endregion

            try
            {
                #region Specify input directory and dataset name
                // The directory which contains the Panchromatic Image.
                string panDir = @"C:\Data\QB\Pan";
                // The directory which contains the Multispectral Image.
                string rgbDir = @"C:\Data\QB\MS";

                string panImageName = "05JAN27104436-P1BS-005533787010_01_P002.TIF";
                string rgbImageName = "05JAN27104436-M1BS-005533787010_01_P002.TIF";

                // Output filename.
                string outputDataset = @"c:\Temp\QBTemplateCS.afr";
                #endregion

                #region Initialize
                // Setup Workspaces.
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
                IWorkspaceFactory workspaceFactory   = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                IRasterWorkspace  panRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(panDir, 0);
                IRasterWorkspace  rgbRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(rgbDir, 0);

                // Open Datasets
                IRasterDataset panDataset = panRasterWorkspace.OpenRasterDataset(panImageName);
                IRasterDataset rgbDataset = rgbRasterWorkspace.OpenRasterDataset(rgbImageName);
                #endregion

                #region Create Variables
                // Create one variable of type IRasterFunctionVariable for each
                // Raster Dataset opened above

                // Create a new Raster Function Variable
                IRasterFunctionVariable panVar = new RasterFunctionVariableClass();
                // Set the name of the variable
                panVar.Name = "panImage";
                // Describe the variable
                panVar.Description = "Panchromatic Image to be used for pansharpening";
                // Specify whether it represents a dataset
                panVar.IsDataset = true;

                // Create a new Raster Function Variable
                IRasterFunctionVariable rgbVar = new RasterFunctionVariableClass();
                // Set the name of the variable
                rgbVar.Name = "rgbImage";
                // Describe the variable
                rgbVar.Description = "Multispectral Image to be used for pansharpening";
                // Specify whether it represents a dataset
                rgbVar.IsDataset = true;
                #endregion

                #region Prepare the Pan Image
                // Setup statistics for each band
                IArray            statsArrayPan = new ArrayClass();
                IRasterStatistics statsPanBand  = new RasterStatisticsClass();
                statsPanBand.Minimum = 1;
                statsPanBand.Maximum = 2047;
                statsArrayPan.Add(statsPanBand);
                // Create the arguments object for the stretching function
                IStretchFunctionArguments stretchingPanFunctionArguements = new StretchFunctionArgumentsClass();
                // Set the stretching type
                stretchingPanFunctionArguements.StretchType =
                    esriRasterStretchType.esriRasterStretchMinimumMaximum;
                // Set the statistics created above
                stretchingPanFunctionArguements.Statistics = statsArrayPan;
                // Set the input raster, in this case, the variable for the Pan Image
                stretchingPanFunctionArguements.Raster = panVar;

                // Create the function object to stretch the Pan Image.
                IRasterFunction stretchingPanFunction = new StretchFunction();

                // Create a Raster Function Template object for the stretch function
                IRasterFunctionTemplate stretchingPanFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                stretchingPanFunctionT.Function = (IRasterFunction)stretchingPanFunction;
                // Set the arguments for the function
                stretchingPanFunctionT.Arguments = stretchingPanFunctionArguements;
                #endregion

                #region Prepare the Multispectral (RGB) Image
                // Create an array which defines the order of bands
                ILongArray bandIDs = new LongArrayClass();
                bandIDs.Add(2);
                bandIDs.Add(1);
                bandIDs.Add(0);
                // Create an Extract Band Function Arguments object
                IExtractBandFunctionArguments extractRgbBandFunctionArgs = (IExtractBandFunctionArguments)
                                                                           new ExtractBandFunctionArguments();
                // Set the order of bands of the output
                extractRgbBandFunctionArgs.BandIDs = bandIDs;
                // Set the input raster, in this case the variable for the Multispectral Image
                extractRgbBandFunctionArgs.Raster = rgbVar;

                // Create the Extract Band Function object
                IRasterFunction extractRgbBandFunction = new ExtractBandFunction();

                // Create a Raster Function Template object for the function created above
                IRasterFunctionTemplate extractRgbBandFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                extractRgbBandFunctionT.Function = (IRasterFunction)extractRgbBandFunction;
                // Set the arguments for the function
                extractRgbBandFunctionT.Arguments = extractRgbBandFunctionArgs;

                // Setup statistics for each band
                IArray            statsArray    = new ArrayClass();
                IRasterStatistics statsMulBand1 = new RasterStatisticsClass();
                statsMulBand1.Minimum = 100;
                statsMulBand1.Maximum = 1721;
                statsArray.Add(statsMulBand1);
                IRasterStatistics statsMulBand2 = new RasterStatisticsClass();
                statsMulBand2.Minimum = 95;
                statsMulBand2.Maximum = 2047;
                statsArray.Add(statsMulBand2);
                IRasterStatistics statsMulBand3 = new RasterStatisticsClass();
                statsMulBand3.Minimum = 34;
                statsMulBand3.Maximum = 2006;
                statsArray.Add(statsMulBand3);

                // Create a stretching function for the multispectral image
                IRasterFunction stretchingRGBFunction = new StretchFunction();
                // Create an arguments object for the stretch function
                IStretchFunctionArguments stretchingRGBFunctionArguments =
                    new StretchFunctionArgumentsClass();
                // Set the type of stretchings to perform
                stretchingRGBFunctionArguments.StretchType =
                    esriRasterStretchType.esriRasterStretchMinimumMaximum;
                // Set the statistics created above
                stretchingRGBFunctionArguments.Statistics = statsArray;
                // Set the extract band function template created above as the input
                stretchingRGBFunctionArguments.Raster = extractRgbBandFunctionT;

                // Create a Raster Function Template object for the function created above
                IRasterFunctionTemplate stretchingRGBFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                stretchingRGBFunctionT.Function = (IRasterFunction)stretchingRGBFunction;
                // Set the arguments for the function
                stretchingRGBFunctionT.Arguments = stretchingRGBFunctionArguments;
                #endregion

                #region Pansharpen the Pan Image with the Multispectral
                // Create a Raster Function Arguments object for the pansharpen function
                IPansharpeningFunctionArguments pansharpFunctionArguements =
                    new PansharpeningFunctionArgumentsClass();
                // Set the Panchromatic image which has been prepared above
                pansharpFunctionArguements.PanImage = stretchingPanFunctionT;
                // Set the Multispectral image which has been prepared above
                pansharpFunctionArguements.MSImage = stretchingRGBFunctionT;

                // Create a new pansharpen raster function object
                IRasterFunction pansharpenFunction = new PansharpeningFunction();
                // Create a new pansharpen function arguments object
                IPansharpeningFunctionArguments pansharpenFunctionArguements =
                    new PansharpeningFunctionArgumentsClass();
                // Set the pansharpening type
                pansharpenFunctionArguements.PansharpeningType =
                    esriPansharpeningType.esriPansharpeningESRI;
                // Create an array of doubles that sets the weights for each band
                IDoubleArray weightsArray = new DoubleArrayClass();
                weightsArray.Add(0.167);
                weightsArray.Add(0.166);
                weightsArray.Add(0.166);
                // and set it on the arguments object
                pansharpenFunctionArguements.Weights = weightsArray;

                // Create a Raster Function Template object for the pansharpen function
                IRasterFunctionTemplate rasterFunction = new RasterFunctionTemplateClass();
                rasterFunction.Function  = (IRasterFunction)pansharpenFunction;
                rasterFunction.Arguments = pansharpFunctionArguements;
                ((IRasterFunction)rasterFunction).PixelType = rstPixelType.PT_UCHAR;
                #endregion

                #region Resolve variables
                // Create a PropertySet object to set the values for the
                // Raster Function Variables created above
                IPropertySet variables = new PropertySet();
                variables.SetProperty("panImage", panDataset);
                variables.SetProperty("rgbImage", rgbDataset);
                #endregion

                #region Create the Function Raster Dataset
                // Create the Function Raster Dataset Object for the Pansharpened Dataset
                IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset();
                // Create a name object for the Function Raster Dataset.
                IFunctionRasterDatasetName functionRasterDatasetName =
                    new FunctionRasterDatasetNameClass();
                // Specify the output filename for the new dataset (including
                // the .afr extension at the end).
                functionRasterDatasetName.FullName = outputDataset;
                functionRasterDataset.FullName     = (IName)functionRasterDatasetName;
                // Initialize the new Function Raster Dataset with the Raster Function
                // Template and the property set containing the variables and their values.
                functionRasterDataset.Init((IRasterFunction)rasterFunction, variables);

                ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset;
                myTempDset.MakePermanent();
                #endregion

                #region Shutdown
                Console.WriteLine("Success.");
                Console.WriteLine("Press any key...");
                Console.ReadKey();

                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }
            catch (Exception exc)
            {
                #region Shutdown
                Console.WriteLine("Exception Caught while creating Function Raster Dataset. " + exc.Message);
                Console.WriteLine("Failed.");
                Console.WriteLine("Press any key...");
                Console.ReadKey();

                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }
        }
        static void Main(string[] args)
        {
            ESRI.ArcGIS.esriSystem.AoInitialize aoInit;

            #region Initialize License
            try
            {
                Console.WriteLine("Obtaining license");
                ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
                aoInit = new AoInitializeClass();
                esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
                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);
                return;
            }
            #endregion

            try
            {
                #region Specify input directory and dataset name
                // The directory which contains the Panchromatic Image.
                string panDir = @"C:\Data\QB\Pan";
                // The directory which contains the Multispectral Image.
                string rgbDir = @"C:\Data\QB\MS";
                
                string panImageName = "05JAN27104436-P1BS-005533787010_01_P002.TIF";
                string rgbImageName = "05JAN27104436-M1BS-005533787010_01_P002.TIF";

                // Output filename.
                string outputDataset = @"c:\Temp\QBTemplateCS.afr";
                #endregion

                #region Initialize
                // Setup Workspaces.
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
                IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                IRasterWorkspace panRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(panDir, 0);
                IRasterWorkspace rgbRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(rgbDir, 0);

                // Open Datasets
                IRasterDataset panDataset = panRasterWorkspace.OpenRasterDataset(panImageName);
                IRasterDataset rgbDataset = rgbRasterWorkspace.OpenRasterDataset(rgbImageName);
                #endregion

                #region Create Variables
                // Create one variable of type IRasterFunctionVariable for each 
                // Raster Dataset opened above
                
                // Create a new Raster Function Variable
                IRasterFunctionVariable panVar = new RasterFunctionVariableClass();
                // Set the name of the variable
                panVar.Name = "panImage";
                // Describe the variable
                panVar.Description = "Panchromatic Image to be used for pansharpening";
                // Specify whether it represents a dataset
                panVar.IsDataset = true;

                // Create a new Raster Function Variable
                IRasterFunctionVariable rgbVar = new RasterFunctionVariableClass();
                // Set the name of the variable
                rgbVar.Name = "rgbImage";
                // Describe the variable
                rgbVar.Description = "Multispectral Image to be used for pansharpening";
                // Specify whether it represents a dataset
                rgbVar.IsDataset = true;
                #endregion

                #region Prepare the Pan Image
                // Setup statistics for each band
                IArray statsArrayPan = new ArrayClass();
                IRasterStatistics statsPanBand = new RasterStatisticsClass();
                statsPanBand.Minimum = 1;
                statsPanBand.Maximum = 2047;
                statsArrayPan.Add(statsPanBand);
                // Create the arguments object for the stretching function
                IStretchFunctionArguments stretchingPanFunctionArguements = new StretchFunctionArgumentsClass();
                // Set the stretching type
                stretchingPanFunctionArguements.StretchType =
                    esriRasterStretchType.esriRasterStretchMinimumMaximum;
                // Set the statistics created above
                stretchingPanFunctionArguements.Statistics = statsArrayPan;
                // Set the input raster, in this case, the variable for the Pan Image
                stretchingPanFunctionArguements.Raster = panVar;

                // Create the function object to stretch the Pan Image.
                IRasterFunction stretchingPanFunction = new StretchFunction();

                // Create a Raster Function Template object for the stretch function
                IRasterFunctionTemplate stretchingPanFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                stretchingPanFunctionT.Function = (IRasterFunction)stretchingPanFunction;
                // Set the arguments for the function
                stretchingPanFunctionT.Arguments = stretchingPanFunctionArguements;
                #endregion

                #region Prepare the Multispectral (RGB) Image
                // Create an array which defines the order of bands
                ILongArray bandIDs = new LongArrayClass();
                bandIDs.Add(2);
                bandIDs.Add(1);
                bandIDs.Add(0);
                // Create an Extract Band Function Arguments object
                IExtractBandFunctionArguments extractRgbBandFunctionArgs = (IExtractBandFunctionArguments)
                    new ExtractBandFunctionArguments();
                // Set the order of bands of the output
                extractRgbBandFunctionArgs.BandIDs = bandIDs;
                // Set the input raster, in this case the variable for the Multispectral Image
                extractRgbBandFunctionArgs.Raster = rgbVar;

                // Create the Extract Band Function object
                IRasterFunction extractRgbBandFunction = new ExtractBandFunction();

                // Create a Raster Function Template object for the function created above
                IRasterFunctionTemplate extractRgbBandFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                extractRgbBandFunctionT.Function = (IRasterFunction)extractRgbBandFunction;
                // Set the arguments for the function
                extractRgbBandFunctionT.Arguments = extractRgbBandFunctionArgs;

                // Setup statistics for each band
                IArray statsArray = new ArrayClass();
                IRasterStatistics statsMulBand1 = new RasterStatisticsClass();
                statsMulBand1.Minimum = 100;
                statsMulBand1.Maximum = 1721;
                statsArray.Add(statsMulBand1);
                IRasterStatistics statsMulBand2 = new RasterStatisticsClass();
                statsMulBand2.Minimum = 95;
                statsMulBand2.Maximum = 2047;
                statsArray.Add(statsMulBand2);
                IRasterStatistics statsMulBand3 = new RasterStatisticsClass();
                statsMulBand3.Minimum = 34;
                statsMulBand3.Maximum = 2006;
                statsArray.Add(statsMulBand3);

                // Create a stretching function for the multispectral image
                IRasterFunction stretchingRGBFunction = new StretchFunction();
                // Create an arguments object for the stretch function
                IStretchFunctionArguments stretchingRGBFunctionArguments = 
                    new StretchFunctionArgumentsClass();
                // Set the type of stretchings to perform
                stretchingRGBFunctionArguments.StretchType =
                    esriRasterStretchType.esriRasterStretchMinimumMaximum;
                // Set the statistics created above
                stretchingRGBFunctionArguments.Statistics = statsArray;
                // Set the extract band function template created above as the input
                stretchingRGBFunctionArguments.Raster = extractRgbBandFunctionT;

                // Create a Raster Function Template object for the function created above
                IRasterFunctionTemplate stretchingRGBFunctionT = new RasterFunctionTemplateClass();
                // Set the function on the template
                stretchingRGBFunctionT.Function = (IRasterFunction)stretchingRGBFunction;
                // Set the arguments for the function
                stretchingRGBFunctionT.Arguments = stretchingRGBFunctionArguments;
                #endregion

                #region Pansharpen the Pan Image with the Multispectral
                // Create a Raster Function Arguments object for the pansharpen function
                IPansharpeningFunctionArguments pansharpFunctionArguements = 
                    new PansharpeningFunctionArgumentsClass();
                // Set the Panchromatic image which has been prepared above
                pansharpFunctionArguements.PanImage = stretchingPanFunctionT;
                // Set the Multispectral image which has been prepared above
                pansharpFunctionArguements.MSImage = stretchingRGBFunctionT;

                // Create a new pansharpen raster function object
                IRasterFunction pansharpenFunction = new PansharpeningFunction();
                // Create a new pansharpen function arguments object
                IPansharpeningFunctionArguments pansharpenFunctionArguements = 
                    new PansharpeningFunctionArgumentsClass();
                // Set the pansharpening type
                pansharpenFunctionArguements.PansharpeningType = 
                    esriPansharpeningType.esriPansharpeningESRI;
                // Create an array of doubles that sets the weights for each band
                IDoubleArray weightsArray = new DoubleArrayClass();
                weightsArray.Add(0.167);
                weightsArray.Add(0.166);
                weightsArray.Add(0.166);
                // and set it on the arguments object
                pansharpenFunctionArguements.Weights = weightsArray;

                // Create a Raster Function Template object for the pansharpen function
                IRasterFunctionTemplate rasterFunction = new RasterFunctionTemplateClass();
                rasterFunction.Function = (IRasterFunction)pansharpenFunction;
                rasterFunction.Arguments = pansharpFunctionArguements;
                ((IRasterFunction)rasterFunction).PixelType = rstPixelType.PT_UCHAR;
                #endregion

                #region Resolve variables
                // Create a PropertySet object to set the values for the 
                // Raster Function Variables created above
                IPropertySet variables = new PropertySet();
                variables.SetProperty("panImage", panDataset);
                variables.SetProperty("rgbImage", rgbDataset);
                #endregion

                #region Create the Function Raster Dataset
                // Create the Function Raster Dataset Object for the Pansharpened Dataset
                IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset();
                // Create a name object for the Function Raster Dataset.
                IFunctionRasterDatasetName functionRasterDatasetName =
                    new FunctionRasterDatasetNameClass();
                // Specify the output filename for the new dataset (including 
                // the .afr extension at the end).
                functionRasterDatasetName.FullName = outputDataset;
                functionRasterDataset.FullName = (IName)functionRasterDatasetName;
                // Initialize the new Function Raster Dataset with the Raster Function 
                // Template and the property set containing the variables and their values.
                functionRasterDataset.Init((IRasterFunction)rasterFunction, variables);

                ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset;
                myTempDset.MakePermanent();
                #endregion

                #region Shutdown
                Console.WriteLine("Success.");
                Console.WriteLine("Press any key...");
                Console.ReadKey();

                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }
            catch (Exception exc)
            {
                #region Shutdown
                Console.WriteLine("Exception Caught while creating Function Raster Dataset. " + exc.Message);
                Console.WriteLine("Failed.");
                Console.WriteLine("Press any key...");
                Console.ReadKey();

                // Shutdown License
                aoInit.Shutdown();
                #endregion
            }        
        }
 /// <summary>
 /// creates a mask of valid values (greater than equal to min and less than equal to max)
 /// </summary>
 /// <param name="inRaster"></param>
 /// <param name="BandRanges"></param>
 /// <returns></returns>
 public IFunctionRasterDataset maskDataRange(object inRaster, double[][] BandRanges)
 {
     string tempAr = funcDir + "\\" + FuncCnt + ".afr";
     IFunctionRasterDataset frDset = new FunctionRasterDatasetClass();
     IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass();
     frDsetName.FullName = tempAr;
     frDset.FullName = (IName)frDsetName;
     IRasterFunction rsFunc = new MaskFunctionClass();
     IMaskFunctionArguments args = new MaskFunctionArgumentsClass();
     IDoubleArray dbArray = new DoubleArrayClass();
     foreach (double[] d in BandRanges)
     {
         dbArray.Add(d[0]);
         dbArray.Add(d[1]);
     }
     args.Raster = returnRaster(inRaster);
     args.IncludedRanges = dbArray;
     frDset.Init(rsFunc, args);
     return frDset;
 }
        /// <summary>
        /// performs a convolution analysis for a defined kernel
        /// </summary>
        /// <param name="inRaster"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="kn"></param>
        /// <returns></returns>
        public IFunctionRasterDataset convolutionRasterFunction(object inRaster, int width, int height, double[] kn)
        {
            string tempAr = funcDir + "\\" + FuncCnt + ".afr";
            IFunctionRasterDataset frDset = new FunctionRasterDatasetClass();
            IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass();
            frDsetName.FullName = tempAr;
            frDset.FullName = (IName)frDsetName;
            IRasterFunction rsFunc = new ConvolutionFunctionClass();
            IFunctionRasterDataset rs = createIdentityRaster(inRaster,rstPixelType.PT_FLOAT);
            IConvolutionFunctionArguments args = new ConvolutionFunctionArgumentsClass();
            args.Raster = rs;
            args.Rows = width;
            args.Columns = height;
            IDoubleArray dbArry = new DoubleArrayClass();
            foreach (double d in kn)
            {
                dbArry.Add(d);
            }
            args.Kernel = dbArry;
            args.Type = esriRasterFilterTypeEnum.esriRasterFilterUserDefined;
            frDset.Init(rsFunc, args);
            IFunctionRasterDataset outFrDset = frDset;
            if (width > 3 || height > 3)
            {
                double cSizeX = frDset.RasterInfo.CellSize.X;
                double cSizeY = frDset.RasterInfo.CellSize.Y;
                double addY = 0;
                double addX = 0;
                if (width > 3)
                {
                    addX = (((width + 1) / 2) - 2) * cSizeX;

                }

                if (height > 3)
                {
                    addY = -1 * (((height + 1) / 2) - 2) * cSizeY;

                }
                outFrDset = shiftRasterFunction(frDset, addX, addY);
            }
            return outFrDset;
        }
 /// <summary>
 /// Calculates a trend raster from double filter
 /// </summary>
 /// <param name="inRaster">string, IRasterDataset, or IRaster</param>
 /// <param name="doubleFilter">a double array of plan values</param>
 /// <returns>IRaster</returns>
 public IFunctionRasterDataset calcTrendFunction(object inRaster, double[] doubleFilter)
 {
     IDoubleArray dbArray = new DoubleArrayClass();
     string tempAr = funcDir + "\\" + FuncCnt + ".afr";
     IFunctionRasterDataset frDset = new FunctionRasterDatasetClass();
     IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass();
     frDsetName.FullName = tempAr;
     frDset.FullName = (IName)frDsetName;
     IRasterFunction rsFunc = new TrendFunctionClass();
     ITrendFunctionArguments args = new TrendFunctionArgumentsClass();
     args.Raster = createIdentityRaster(inRaster,rstPixelType.PT_FLOAT);
     for (int i = 0; i < doubleFilter.Length; i++)
     {
         dbArray.Add(doubleFilter[i]);
     }
     args.PlaneParameters = dbArray;
     frDset.Init(rsFunc, args);
     return frDset;
 }
 /// <summary>
 /// Remaps the values of a given raster to new set of values
 /// </summary>
 /// <param name="inRaster">input raster</param>
 /// <param name="filter">a remap filter</param>
 /// <returns>IRaster with remaped values</returns>
 public IFunctionRasterDataset calcRemapFunction(object inRaster, IRemapFilter filter)
 {
     IFunctionRasterDataset rRst = createIdentityRaster(inRaster);
     IDoubleArray rangeArray = new DoubleArrayClass();
     IDoubleArray valueArray = new DoubleArrayClass();
     double min,max,vl;
     string tempAr = funcDir + "\\" + FuncCnt + ".afr";
     IFunctionRasterDataset frDset = new FunctionRasterDatasetClass();
     IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass();
     frDsetName.FullName = tempAr;
     frDset.FullName = (IName)frDsetName;
     IRasterFunction rsFunc = new RemapFunctionClass();
     IRemapFunctionArguments args = new RemapFunctionArgumentsClass();
     args.AllowUnmatched = filter.AllowUnmatched;
     args.Raster = rRst;
     for (int i = 0; i < filter.ClassCount; i++)
     {
         filter.QueryClass(i, out min, out max, out vl);
         rangeArray.Add(min);
         rangeArray.Add(max);
         valueArray.Add(vl);
     }
     args.InputRanges = rangeArray;
     args.OutputValues = valueArray;
     frDset.Init(rsFunc, args);
     return frDset;
 }