Esempio n. 1
0
 /// <summary>
 /// The default indexing of a raster specifies the column and row
 /// values for accessing a particular value.
 /// </summary>
 /// <param name="Column"></param>
 /// <param name="Row"></param>
 /// <returns></returns>
 public object this[int Column, int Row]
 {
     get
     {
         return(m_Grid.get_Value(Column, Row));
     }
     set
     {
         m_Grid.set_Value(Column, Row, value);
     }
 }
 public void set_Value(int col, int row, object pVal)
 {
     grid.set_Value(col, row, pVal);
 }
        // This is not a full fledged map algebra function!  This cannot deal with
        // images of different sizes or cell spacings.  This is only a quick function
        // that can tell if, for instance, a pitfilled image is different from the
        // version created in Arcview.  It simply takes each cell and finds the difference
        // in values.
        #region "Difference"

        /// <summary>
        /// Calculates the difference values and stores them in the Dest grid.  This only works if the grids
        /// have the same number of rows and columns.
        /// </summary>
        /// <param name="Source1">MapWinGIS.Grid representing one source grid</param>
        /// <param name="Source2">MapWinGIS.Grid to compare Source1 against</param>
        /// <param name="Dest">MapWinGIS.Grid where the output is to be saved</param>
        /// <param name="ICallBack">A MapWinGIS.ICallBack</param>
        /// <remarks>Uses ArgumentExceptions if the grids are different sizes</remarks>
        public static void Difference(MapWinGIS.Grid Source1, MapWinGIS.Grid Source2, MapWinGIS.Grid Dest, MapWinGIS.ICallback ICallBack)
        {
            // Log entrance as best as possible while preventing errors from a null reference
            string Source1File = "null";
            string Source2File = "null";
            string DestFile    = "null";

            if (Source1 != null)
            {
                Source1File = Source1.Filename;
                if (Source1File == null)
                {
                    Source1File = "Unnamed";
                }
            }
            if (Source2 != null)
            {
                Source2File = Source2.Filename;
                if (Source2File == null)
                {
                    Source2File = "Unnamed";
                }
            }
            if (Dest != null)
            {
                DestFile = Dest.Filename;
                if (DestFile == null)
                {
                    DestFile = "Unnamed";
                }
            }
            MapWinUtility.Logger.Dbg("Difference(Source1: " + Source1File + ",\n" +
                                     "           Source2: " + Source2File + ",\n" +
                                     "           Dest: " + DestFile + ",\n" +
                                     "           ICallback");

            if (Source1 == null)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: Source1 cannot be null.");
                throw new ArgumentException("Source1 cannot be null.");
            }
            if (Source2 == null)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: Source2 cannot be null.");
                throw new ArgumentException("Source2 cannot be null.");
            }
            if (Dest == null)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: Dest cannot be null.");
                throw new ArgumentException("Dest cannot be null.");
            }
            int nX, nY;

            nX = Source1.Header.NumberCols;
            nY = Source1.Header.NumberRows;
            if (Source2.Header.NumberRows != nY)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: The grids are not the same height.");
                throw new ArgumentException("The grids are not the same height.");
            }
            if (Source2.Header.NumberCols != nX)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: The grids are not the same width!");
                throw new ArgumentException("The grids are not the same width!");
            }
            if (Dest.Header.NumberRows != nY)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: The output grid is not the same height!");
                throw new ArgumentException("The output grid is not the same height!");
            }
            if (Dest.Header.NumberCols != nX)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: The output grid is not the same width!");
                throw new ArgumentException("The output grid is not the same width!");
            }

            int OldProg = 0;
            int Prog    = 0;

            if (ICallBack != null)
            {
                ICallBack.Progress("Status", 0, "Difference...0% Complete");
            }
            MapWinUtility.Logger.Progress("Difference...0% Complete", Prog, OldProg);
            if (Source1.DataType != MapWinGIS.GridDataType.FloatDataType)
            {
                switch (Source1.DataType)
                {
                case MapWinGIS.GridDataType.DoubleDataType:
                    for (int Y = 0; Y < nY; Y++)
                    {
                        for (int X = 0; X < nX; X++)
                        {
                            double val = (double)Source1.get_Value(X, Y) - (double)Source2.get_Value(X, Y);
                            Dest.set_Value(X, Y, val);
                        }
                        if (ICallBack != null)
                        {
                            Prog = (int)(Y * 100 / nY);
                            if (Prog > OldProg)
                            {
                                ICallBack.Progress("Status", Prog, "Difference..." + Prog.ToString() + "% Complete");
                                MapWinUtility.Logger.Progress("Difference..." + Prog.ToString() + "% Complete", Prog, OldProg);
                                OldProg = Prog;
                            }
                        }
                    }
                    break;

                case MapWinGIS.GridDataType.UnknownDataType:
                    for (int Y = 0; Y < nY; Y++)
                    {
                        for (int X = 0; X < nX; X++)
                        {
                            double val = (double)Source1.get_Value(X, Y) - (double)Source2.get_Value(X, Y);
                            Dest.set_Value(X, Y, val);
                        }
                        if (ICallBack != null)
                        {
                            Prog = (int)(Y * 100 / nY);
                            if (Prog > OldProg)
                            {
                                ICallBack.Progress("Status", Prog, "Difference..." + Prog.ToString() + "% Complete");
                                MapWinUtility.Logger.Progress("Difference..." + Prog.ToString() + "% Complete", Prog, OldProg);
                                OldProg = Prog;
                            }
                        }
                    }
                    break;

                case MapWinGIS.GridDataType.LongDataType:
                    for (int Y = 0; Y < nY; Y++)
                    {
                        for (int X = 0; X < nX; X++)
                        {
                            long val = (long)Source1.get_Value(X, Y) - (long)Source2.get_Value(X, Y);
                            Dest.set_Value(X, Y, val);
                        }
                        if (ICallBack != null)
                        {
                            Prog = (int)(Y * 100 / nY);
                            if (Prog > OldProg)
                            {
                                MapWinUtility.Logger.Progress("Difference..." + Prog.ToString() + "% Complete", Prog, OldProg);
                                ICallBack.Progress("Status", Prog, "Difference..." + Prog.ToString() + "% Complete");
                                OldProg = Prog;
                            }
                        }
                    }
                    break;

                case MapWinGIS.GridDataType.ShortDataType:
                    for (int Y = 0; Y < nY; Y++)
                    {
                        for (int X = 0; X < nX; X++)
                        {
                            int val = (int)Source1.get_Value(X, Y) - (int)Source2.get_Value(X, Y);
                            Dest.set_Value(X, Y, val);
                        }
                        if (ICallBack != null)
                        {
                            Prog = (int)(Y * 100 / nY);
                            if (Prog > OldProg)
                            {
                                MapWinUtility.Logger.Progress("Difference..." + Prog.ToString() + "% Complete", Prog, OldProg);
                                ICallBack.Progress("Status", Prog, "Difference..." + Prog.ToString() + "% Complete");
                                OldProg = Prog;
                            }
                        }
                    }
                    break;

                default:
                    MapWinUtility.Logger.Progress("The Datatype was not a valid numeric type.", Prog, OldProg);
                    throw new ArgumentException("The Datatype was not a valid numeric type.");
                }
            }
            else
            {
                for (int Y = 0; Y < nY; Y++)
                {
                    float[] Vals1 = new float[nX];
                    float[] Vals2 = new float[nX];
                    float[] Diff  = new float[nX];
                    Source1.GetRow(Y, ref Vals1[0]);
                    Source2.GetRow(Y, ref Vals2[0]);
                    for (int X = 0; X < nX; X++)
                    {
                        Diff[X] = Vals1[X] - Vals2[X];
                    }
                    Dest.PutRow(Y, ref Diff[0]);
                    if (ICallBack != null)
                    {
                        Prog = (int)(Y * 100 / nY);
                        if (Prog > OldProg)
                        {
                            MapWinUtility.Logger.Progress("Difference..." + Prog.ToString() + "% Complete", Prog, OldProg);
                            ICallBack.Progress("Status", Prog, "Difference..." + Prog.ToString() + "% Complete");
                            OldProg = Prog;
                        }
                    }
                }
            }
            MapWinUtility.Logger.Dbg("Finished Difference");
            if (ICallBack != null)
            {
                ICallBack.Progress("Status", 0, "Done.");
            }
        }