コード例 #1
0
ファイル: Slope.cs プロジェクト: yangkf1985/DotSpatial
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="raster">The input altitude raster.</param>
        /// <param name="inZFactor">The double precision multiplicative scaling factor for elevation values.</param>
        /// <param name="slopeInPercent">A boolean parameter that clarifies the nature of the slope values.  If this is true, the values represent percent slope.</param>
        /// <param name="result">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>A boolean value, true if the process was successful.</returns>
        public static bool GetSlope(IRaster raster, double inZFactor, bool slopeInPercent, ref IRaster result,
                                    ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (raster == null || result == null)
            {
                return(false);
            }
            int noOfCol = raster.NumColumns;
            int noOfRow = raster.NumRows;

            //Create the new raster with the appropriate dimensions
            IRaster temp = Raster.CreateRaster("SlopeRaster.bgd", string.Empty, noOfCol, noOfRow, 1, typeof(double),
                                               new[] { string.Empty });

            temp.NoDataValue = raster.NoDataValue;
            temp.Bounds      = raster.Bounds;

            ProgressMeter progMeter = null;

            try
            {
                if (cancelProgressHandler != null)
                {
                    progMeter = new ProgressMeter(cancelProgressHandler, "Calculating Slope", temp.NumRows);
                }

                for (int i = 0; i < temp.NumRows; i++)
                {
                    if (cancelProgressHandler != null)
                    {
                        progMeter.Next();
                        if ((i % 100) == 0)
                        {
                            progMeter.SendProgress();

                            // HACK: DoEvents messes up the normal flow of your application.
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }
                    for (int j = 0; j < temp.NumColumns; j++)
                    {
                        if (i > 0 && i < temp.NumRows - 1 && j > 0 && j < temp.NumColumns - 1)
                        {
                            double z1 = raster.Value[i - 1, j - 1];
                            double z2 = raster.Value[i - 1, j];
                            double z3 = raster.Value[i - 1, j + 1];
                            double z4 = raster.Value[i, j - 1];
                            double z5 = raster.Value[i, j + 1];
                            double z6 = raster.Value[i + 1, j - 1];
                            double z7 = raster.Value[i + 1, j];
                            double z8 = raster.Value[i + 1, j + 1];

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (180 / Math.PI);

                            //change to radius and in percentage
                            if (slopeInPercent)
                            {
                                slope = (Math.Tan(slope * Math.PI / 180)) * 100;
                            }

                            temp.Value[i, j] = slope;

                            if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            temp.Value[i, j] = temp.NoDataValue;
                        }

                        if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                        {
                            return(false);
                        }
                    }
                }

                result = temp;
                if (result.IsFullyWindowed())
                {
                    result.Save();
                    return(true);
                }
                return(false);
            }
            finally
            {
                if (progMeter != null)
                {
                    progMeter.Reset();
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="ras">The input altitude raster.</param>
        /// <param name="inZFactor">The multiplicitive scaling factor for elveation.</param>
        /// <param name="slopeInPercent">Boolean that is true if the slope values should be returned as percentages.</param>
        /// <param name="result">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>Boolean, true if the method was successful.</returns>
        private static bool Slope(
            ref IRaster ras,
            double inZFactor,
            bool slopeInPercent,
            ref IRaster result,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (ras == null || result == null)
            {
                return(false);
            }

            try
            {
                int noOfCol = ras.NumColumns;
                int noOfRow = ras.NumRows;

                // Create the new raster with the appropriate dimensions
                IRaster temp = Raster.CreateRaster(
                    "SlopeRaster.bgd", string.Empty, noOfCol, noOfRow, 1, typeof(double), new[] { string.Empty });
                temp.NoDataValue = ras.NoDataValue;
                temp.Bounds      = ras.Bounds;

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

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (180 / Math.PI);

                            // change to radious and in persentage
                            if (slopeInPercent)
                            {
                                slope = Math.Tan(slope * Math.PI / 180) * 100;
                            }

                            temp.Value[i, j] = slope;

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

                        if (cancelProgressHandler.Cancel)
                        {
                            return(false);
                        }
                    }
                }

                result = temp;
                if (result.IsFullyWindowed())
                {
                    result.Save();
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                // throw new SystemException("Error in Slope: ", ex);
                throw new SystemException(ex.ToString());
            }
        }
コード例 #3
0
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="ras">The input altitude raster.</param>
        /// <param name="inZFactor">The multiplicitive scaling factor for elveation.</param>
        /// <param name="slopeInPercent">Boolean that is true if the slope values should be returned as percentages.</param>
        /// <param name="result">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>Boolean, true if the method was successful.</returns>
        private static bool Slope(
            ref IRaster ras,
            double inZFactor,
            bool slopeInPercent,
            ref IRaster result,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (ras == null || result == null)
            {
                return false;
            }

            try
            {
                int noOfCol = ras.NumColumns;
                int noOfRow = ras.NumRows;

                // Create the new raster with the appropriate dimensions
                IRaster temp = Raster.CreateRaster(
                    "SlopeRaster.bgd", string.Empty, noOfCol, noOfRow, 1, typeof(double), new[] { string.Empty });
                temp.NoDataValue = ras.NoDataValue;
                temp.Bounds = ras.Bounds;

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

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (180 / Math.PI);

                            // change to radious and in persentage
                            if (slopeInPercent)
                            {
                                slope = Math.Tan(slope * Math.PI / 180) * 100;
                            }

                            temp.Value[i, j] = slope;

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

                        if (cancelProgressHandler.Cancel)
                        {
                            return false;
                        }
                    }
                }

                result = temp;
                if (result.IsFullyWindowed())
                {
                    result.Save();
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                // throw new SystemException("Error in Slope: ", ex);
                throw new SystemException(ex.ToString());
            }
        }
コード例 #4
0
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="ras">The input altitude raster.</param>
        /// <param name="inZFactor">A multiplicative scaling factor to be applied to the elevation values before calculating the slope.</param>
        /// <param name="slopeInPercent">If this is true, the resulting slopes are returned as percentages.</param>
        /// <param name="output">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>True if the method worked.</returns>
        public bool Execute(
            IRaster ras,
            double inZFactor,
            bool slopeInPercent,
            IRaster output,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (ras == null || output == null)
            {
                return(false);
            }

            try
            {
                int noOfCol = ras.NumColumns;
                int noOfRow = ras.NumRows;
                output = Raster.CreateRaster(
                    output.Filename, string.Empty, noOfCol, noOfRow, 1, typeof(double), new[] { string.Empty });
                output.NoDataValue = ras.NoDataValue;

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

                int previous = 0;
                for (int i = 0; i < output.NumRows; i++)
                {
                    int current = Convert.ToInt32(Math.Round(i * 100D / output.NumRows));

                    // only update when increment in percentage
                    if (current > previous)
                    {
                        cancelProgressHandler.Progress(string.Empty, current, current + 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)
                        {
                            double z1 = ras.Value[i - 1, j - 1];
                            double z2 = ras.Value[i - 1, j];
                            double z3 = ras.Value[i - 1, j + 1];
                            double z4 = ras.Value[i, j - 1];
                            double z5 = ras.Value[i, j + 1];
                            double z6 = ras.Value[i + 1, j - 1];
                            double z7 = ras.Value[i + 1, j];
                            double z8 = ras.Value[i + 1, j + 1];

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (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)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            output.Value[i, j] = output.NoDataValue;
                        }

                        if (cancelProgressHandler.Cancel)
                        {
                            return(false);
                        }
                    }
                }

                // output = Temp;
                if (output.IsFullyWindowed())
                {
                    output.Save();
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                // throw new SystemException("Error in Slope: ", ex);
                throw new SystemException(ex.ToString());
            }
        }
コード例 #5
0
ファイル: RasterSlope.cs プロジェクト: ExRam/DotSpatial-PCL
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="ras">The input altitude raster.</param>
        /// <param name="inZFactor">A multiplicative scaling factor to be applied to the elevation values before calculating the slope.</param>
        /// <param name="slopeInPercent">If this is true, the resulting slopes are returned as percentages.</param>
        /// <param name="output">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>True if the method worked.</returns>
        public bool Execute(
            IRaster ras,
            double inZFactor,
            bool slopeInPercent,
            IRaster output,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (ras == null || output == null)
            {
                return false;
            }

            try
            {
                int noOfCol = ras.NumColumns;
                int noOfRow = ras.NumRows;
                output = Raster.CreateRaster(
                    output.Filename, string.Empty, noOfCol, noOfRow, 1, typeof(double), new[] { string.Empty });
                output.NoDataValue = ras.NoDataValue;

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

                int previous = 0;
                for (int i = 0; i < output.NumRows; i++)
                {
                    int current = Convert.ToInt32(Math.Round(i * 100D / output.NumRows));

                    // only update when increment in percentage
                    if (current > previous)
                    {
                        cancelProgressHandler.Progress(string.Empty, current, current + 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)
                        {
                            double z1 = ras.Value[i - 1, j - 1];
                            double z2 = ras.Value[i - 1, j];
                            double z3 = ras.Value[i - 1, j + 1];
                            double z4 = ras.Value[i, j - 1];
                            double z5 = ras.Value[i, j + 1];
                            double z6 = ras.Value[i + 1, j - 1];
                            double z7 = ras.Value[i + 1, j];
                            double z8 = ras.Value[i + 1, j + 1];

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (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)
                            {
                                return false;
                            }
                        }
                        else
                        {
                            output.Value[i, j] = output.NoDataValue;
                        }

                        if (cancelProgressHandler.Cancel)
                        {
                            return false;
                        }
                    }
                }

                // output = Temp;
                if (output.IsFullyWindowed())
                {
                    output.Save();
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                // throw new SystemException("Error in Slope: ", ex);
                throw new SystemException(ex.ToString());
            }
        }
コード例 #6
0
ファイル: Slope.cs プロジェクト: nikson898/dot-spatial
        /// <summary>
        /// Executes the slope generation raster.
        /// </summary>
        /// <param name="raster">The input altitude raster.</param>
        /// <param name="inZFactor">The double precision multiplicative scaling factor for elevation values.</param>
        /// <param name="slopeInPercent">A boolean parameter that clarifies the nature of the slope values.  If this is true, the values represent percent slope.</param>
        /// <param name="result">The output slope raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>A boolean value, true if the process was successful.</returns>
        public static bool GetSlope(IRaster raster, double inZFactor, bool slopeInPercent, ref IRaster result,
                                    ICancelProgressHandler cancelProgressHandler)
        {
            //Validates the input and output data
            if (raster == null || result == null)
            {
                return false;
            }
            int noOfCol = raster.NumColumns;
            int noOfRow = raster.NumRows;

            //Create the new raster with the appropriate dimensions
            IRaster temp = Raster.CreateRaster("SlopeRaster.bgd", string.Empty, noOfCol, noOfRow, 1, typeof(double),
                                               new[] { string.Empty });
            temp.NoDataValue = raster.NoDataValue;
            temp.Bounds = raster.Bounds;
            temp.Projection = raster.Projection;

            ProgressMeter progMeter = null;
            try
            {
                if (cancelProgressHandler != null)
                    progMeter = new ProgressMeter(cancelProgressHandler, "Calculating Slope", temp.NumRows);

                for (int i = 0; i < temp.NumRows; i++)
                {

                    if (cancelProgressHandler != null)
                    {
                        progMeter.Next();
                        if ((i % 100) == 0)
                        {
                            progMeter.SendProgress();

                            // HACK: DoEvents messes up the normal flow of your application. 
                            System.Windows.Forms.Application.DoEvents();
                        }

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

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

                            double slope = Math.Atan(Math.Sqrt((dZdX * dZdX) + (dZdY * dZdY))) * (180 / Math.PI);

                            //change to radius and in percentage
                            if (slopeInPercent)
                            {
                                slope = (Math.Tan(slope * Math.PI / 180)) * 100;
                            }

                            temp.Value[i, j] = slope;

                            if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                            {
                                return false;
                            }
                        }
                        else
                        {
                            temp.Value[i, j] = temp.NoDataValue;
                        }

                        if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                        {
                            return false;
                        }
                    }
                }

                result = temp;
                if (result.IsFullyWindowed())
                {
                    result.Save();
                    return true;
                }
                return false;
            }
            finally
            {
                if (progMeter != null)
                {
                    progMeter.Reset();
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        }
コード例 #7
0
        /// <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());
            }
            
        }