/// <summary>
        /// Executes the Erase Opaeration tool programaticaly.
        /// </summary>
        /// <param name="input1">The input raster.</param>
        /// <param name="input2">The input raster.</param>
        /// <param name="output">The output raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns></returns>
        public bool Execute(IRaster input1, IFeatureSet input2, IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (input1 == null || input2 == null || output == null)
            {
                return false;
            }

            int noOfCol = input1.NumColumns;
            int noOfRow = input1.NumRows;

            //Calculate the Intersect Envelope
            IEnvelope envelope1 = new Envelope() as IEnvelope;
            IEnvelope envelope2 = new Envelope() as IEnvelope;
            envelope1 = input1.Bounds.Envelope;
            envelope2 = input2.Envelope;
            envelope1 = envelope1.Intersection(envelope2);

            //Create the new raster with the appropriate dimensions
            IRaster Temp = Raster.Create(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { });
            //Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType);
            Temp.CellWidth = input1.CellWidth;
            Temp.CellHeight = input1.CellHeight;
            Temp.Xllcenter = envelope1.Minimum.X + (Temp.CellWidth / 2);
            Temp.Yllcenter = envelope1.Minimum.Y + (Temp.CellHeight / 2);

            MapWindow.Geometries.ICoordinate I1 = new MapWindow.Geometries.Coordinate() as MapWindow.Geometries.ICoordinate;
            RcIndex v1 = new RcIndex();
         
           // MapWindow.Analysis.Topology.Algorithm.PointLocator pointLocator1 = new MapWindow.Analysis.Topology.Algorithm.PointLocator();

            double val1;
            int previous = 0;
            int current = 0;

            
           
            int max = (Temp.Bounds.NumRows + 1);
            for (int i = 0; i < Temp.Bounds.NumRows; i++)
            {
                for (int j = 0; j < Temp.Bounds.NumColumns; j++)
                {
                    I1 = Temp.CellToProj(i, j);
                   
                    v1 = input1.ProjToCell(I1);

                    if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1)
                        val1 = input1.Value[v1.Row, v1.Column];
                    else
                        val1 = Temp.NoDataValue;

                    //test if the coordinate is inside of the polygon
                    bool isInside = false;
                    IFeature pointFeat = new Feature(new Point(I1)) as IFeature;
                        foreach (IFeature f in input2.Features)
                        {
                            if (f.Contains(pointFeat))
                            {
                                Temp.Value[i, j] = val1;
                                isInside = true;
                                break;
                            }
                        }
                 
                    if (isInside == false)
                    {
                        Temp.Value[i, j] = Temp.NoDataValue;
                    }

                    
                    
                    if (cancelProgressHandler.Cancel == true)
                        return false;
                }
                current = Convert.ToInt32(Math.Round(i * 100D / max));
                //only update when increment in percentage
                if (current > previous)
                    cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed");
                previous = current;
            }

            output = Temp;
            output.Save();
            return true;
        }
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="ras">The input altitude raster.</param>
        /// <param name="output">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns></returns>
        public bool Execute(IRaster ras,double inZFactor, bool slopeInPercent,IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (ras == null || output == null)
            {
                return false;
            }
            double z1, z2, z3, z4, z5, z6, z7, z8, dZ_dx, dZ_dy, slope;
            int current, previous;
            try
            {
                int noOfCol = ras.NumColumns;
                int noOfRow = ras.NumRows;
                IEnvelope envelope1 = new Envelope() as IEnvelope;
                envelope1 = ras.Bounds.Envelope;


                output = Raster.Create(output.Filename, "", noOfCol, noOfRow, 1, typeof(double), new string[] { "" });
                output.NoDataValue = ras.NoDataValue;

                //output.Bounds = ras.Bounds.Copy();
                output.Bounds = ras.Bounds;

                previous = 0;
                for (int i = 0; i < output.NumRows; i++)
                {
                    current = Convert.ToInt32(Math.Round(i * 100D / output.NumRows));
                    //only update when increment in percentage
                    if (current > previous)
                        cancelProgressHandler.Progress("", current, current.ToString() + TextStrings.progresscompleted);
                    previous = current;

                    for (int j = 0; j < output.NumColumns; j++)
                    {
                        if (i > 0 && i < output.NumRows - 1 && j > 0 && j < output.NumColumns - 1)
                        {
                            z1 = ras.Value[i - 1, j - 1];
                            z2 = ras.Value[i - 1, j];
                            z3 = ras.Value[i - 1, j + 1];
                            z4 = ras.Value[i, j - 1];
                            z5 = ras.Value[i, j + 1];
                            z6 = ras.Value[i + 1, j - 1];
                            z7 = ras.Value[i + 1, j];
                            z8 = ras.Value[i + 1, j + 1];

                            //3rd Order Finite Difference slope algorithm
                            dZ_dx = inZFactor * ((z3 - z1) + 2 * (z5 - z4) + (z8 - z6)) / (8 * ras.CellWidth);
                            dZ_dy = inZFactor * ((z1 - z6) + 2 * (z2 - z7) + (z3 - z8)) / (8 * ras.CellHeight);

                            slope = Math.Atan(Math.Sqrt((dZ_dx * dZ_dx) + (dZ_dy * dZ_dy))) * (180 / Math.PI);
                            //change to radious and in persentage
                            if (slopeInPercent)
                                slope = (Math.Tan(slope * Math.PI / 180)) * 100;
                            output.Value[i, j] = slope;

                            if (cancelProgressHandler.Cancel == true)
                                return false;
                        }
                        else
                            output.Value[i, j] = output.NoDataValue;

                        if (cancelProgressHandler.Cancel == true)
                            return false;
                    }

                }

                //output = Temp;
                if (output.IsFullyWindowed())
                {
                    output.Save();
                    return true;
                }
                else
                    return false;
                
            }
            catch (Exception ex)
            {
                //throw new SystemException("Error in Slope: ", ex);
                throw new SystemException(ex.ToString());
            }
            
        }
        /// <summary>
        /// Executes the Erase Opaeration tool programaticaly
        /// </summary>
        /// <param name="input1">The input raster</param>
        /// <param name="input2">The input raster</param>
        /// <param name="output">The output raster</param>
        /// <param name="cancelProgressHandler">The progress handler</param>
        /// <returns></returns>
        public bool Execute(IRaster input1, IRaster input2, IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (input1 == null || input2 == null || output == null)
            {
                return false;
            }

            IEnvelope envelope = new Envelope() as IEnvelope;

            envelope = UnionEnvelope(input1, input2);

            int noOfCol;
            int noOfRow;

            //Figures out which raster has smaller cells
            IRaster smallestCellRaster;
            if (input1.CellWidth < input2.CellWidth) smallestCellRaster = input1;
            else smallestCellRaster = input2;

            //Given the envelope of the two rasters we calculate the number of columns / rows
            noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / smallestCellRaster.CellWidth));
            noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / smallestCellRaster.CellHeight));

            //Create the new raster with the appropriate dimensions
            Raster Temp = new Raster();
            //Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType);
            Temp.CreateNew(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { "" });

            Temp.CellWidth = smallestCellRaster.CellWidth;
            Temp.CellHeight = smallestCellRaster.CellHeight;
            Temp.Xllcenter = envelope.Minimum.X + (Temp.CellWidth / 2);
            Temp.Yllcenter = envelope.Minimum.Y + (Temp.CellHeight / 2);

            MapWindow.Geometries.ICoordinate I1 = new MapWindow.Geometries.Coordinate() as MapWindow.Geometries.ICoordinate;
            RcIndex v1 = new RcIndex();
            RcIndex v2 = new RcIndex();

            double val1;
            double val2;
            int previous=0;
            int current=0;
            int max = (Temp.Bounds.NumRows + 1);
            for (int i = 0; i < Temp.Bounds.NumRows; i++)
            {
                for (int j = 0; j < Temp.Bounds.NumColumns; j++)
                {
                    I1 = Temp.CellToProj(i, j);
                    v1 = input1.ProjToCell(I1);
                    if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1)
                        val1 = input1.Value[v1.Row, v1.Column];
                    else
                        val1 = input1.NoDataValue;

                    v2 = input2.ProjToCell(I1);
                    if (v2.Row <= input2.EndRow && v2.Column <= input2.EndColumn && v2.Row > -1 && v2.Column > -1)
                        val2 = input2.Value[v2.Row, v2.Column];
                    else
                        val2 = input2.NoDataValue;

                    if (val1 == input1.NoDataValue && val2 == input2.NoDataValue)
                    {
                        Temp.Value[i, j] = Temp.NoDataValue;
                    }
                    else if (val1 != input1.NoDataValue && val2 == input2.NoDataValue)
                    {
                        Temp.Value[i, j] = val1;
                    }
                    else if (val1 == input1.NoDataValue && val2 != input2.NoDataValue)
                    {
                        Temp.Value[i, j] = val2;
                    }
                    else
                    {
                        Temp.Value[i, j] = val1;
                    }

                    if (cancelProgressHandler.Cancel == true)
                        return false;
                }
                current = Convert.ToInt32(Math.Round(i * 100D / max));
                //only update when increment in persentage
                if(current>previous)
                    cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed");
                previous = current;
            }

            output = Temp;
            output.Save();
            return true;
        }
        /// <summary>
        /// Executes the threshold operation
        /// </summary>
        /// <param name="input1">The input raster</param>
        /// <param name="threshold">The threshold value</param>
        /// <param name="output">The output raster</param>
        /// <param name="cancelProgressHandler">The progress handler</param>
        /// <returns></returns>
        public bool Execute(IRaster input, double threshold, IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (input == null || output == null)
            {
                return false;
            }

            int noOfCol=input.NumColumns;
            int noOfRow=input.NumRows;
            int previous = 0;
            int current = 0;

            //Create the new raster with the appropriate dimensions
            output = Raster.Create(output.Filename, "", noOfCol, noOfRow,1 , typeof(int), new string[] { "" });
            output.CellWidth = input.CellWidth;
            output.CellHeight = input.CellHeight;
            output.Xllcenter = input.Xllcenter;
            output.Yllcenter = input.Yllcenter;
            output.NoDataValue = -1;

            //Loop throug every cell
            int max = (output.Bounds.NumRows + 1);
            for (int i = 0; i < output.NumRows; i++)
            {
                for (int j = 0; j < output.NumColumns; j++)
                {
                    if (input.Value[i, j] == input.NoDataValue)
                        output.Value[i,j] = -1;
                    else if (input.Value[i, j] >= threshold)
                        output.Value[i,j] = 1;
                    else
                        output.Value[i,j] = 0;
                    
                    if (cancelProgressHandler.Cancel == true)
                        return false;
                }

                //only update when increment in persentage
                current = Convert.ToInt32(Math.Round(i * 100D / max));
                if (current > previous)
                    cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed");
                previous = current;
            }

            output.Save();
            return true;
        }