示例#1
0
        /// <summary>
        /// Clips a raster with a polygon feature
        /// </summary>
        /// <param name="polygon">The clipping polygon feature</param>
        /// <param name="input">The input raster object</param>
        /// <param name="outputFileName">the output raster file name</param>
        /// <param name="cancelProgressHandler">Progress handler for reporting progress status and cancelling the operation</param>
        /// <remarks>We assume there is only one part in the polygon. 
        /// Traverses the raster with a vertical scan line from left to right, bottom to top</remarks>
        /// <returns></returns>
        public static IRaster ClipRasterWithPolygon(IFeature polygon, IRaster input, string outputFileName,
                                                    ICancelProgressHandler cancelProgressHandler = null)
        {
            //if the polygon is completely outside the raster
            if (!input.ContainsFeature(polygon))
                return input;

            if (cancelProgressHandler != null)
                cancelProgressHandler.Progress(null, 16, "Retrieving the borders.");

            List<Border> borders = GetBorders(polygon);

            if (cancelProgressHandler != null)
                cancelProgressHandler.Progress(null, 33, "Copying raster.");

            //create output raster
            IRaster output = Raster.CreateRaster(outputFileName, input.DriverCode, input.NumColumns, input.NumRows, 1,
                                                 input.DataType, new[] { string.Empty });
            output.Bounds = input.Bounds.Copy();
            output.NoDataValue = input.NoDataValue;
            if (input.CanReproject)
            {
                output.Projection = input.Projection;
            }

            // set all initial values of Output to NoData
            for (int i = 0; i < output.NumRows; i++)
            {
                for (int j = 0; j < output.NumColumns; j++)
                {
                    output.Value[i, j] = output.NoDataValue;
                }
            }

            double xStart = GetXStart(polygon, output);
            int columnStart = GetStartColumn(polygon, output); //get the index of first column
            double xCurrent = xStart;

            ProgressMeter pm = new ProgressMeter(cancelProgressHandler, "Clipping Raster", output.NumColumns);
            pm.StepPercent = 5;
            pm.StartValue = 33;

            int col = 0;
            for (int columnCurrent = columnStart; columnCurrent < output.NumColumns; columnCurrent++)
            {
                xCurrent = xStart + col * output.CellWidth;

                var intersections = GetYIntersections(borders, xCurrent);
                intersections.Sort();
                ParseIntersections(intersections, xCurrent, columnCurrent, output, input);

                // update progess meter
                pm.CurrentValue = xCurrent;

                //update counter
                col++;

                // cancel if requested
                if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                    return null;
            }

            output.Save();

            return output;
        }
示例#2
0
        /// <summary>
        /// Clips a raster with a polygon feature.
        /// </summary>
        /// <param name="polygon">The clipping polygon feature.</param>
        /// <param name="input">The input raster object.</param>
        /// <param name="outputFileName">the output raster file name.</param>
        /// <param name="cancelProgressHandler">Progress handler for reporting progress status and cancelling the operation.</param>
        /// <remarks>We assume there is only one part in the polygon.
        /// Traverses the raster with a vertical scan line from left to right, bottom to top.</remarks>
        /// <returns>The raster resulting from the clip operation.</returns>
        public static IRaster ClipRasterWithPolygon(IFeature polygon, IRaster input, string outputFileName, ICancelProgressHandler cancelProgressHandler = null)
        {
            // if the polygon is completely outside the raster
            if (!input.ContainsFeature(polygon))
            {
                return(input);
            }

            cancelProgressHandler?.Progress(16, "Retrieving the borders.");

            List <Border> borders = GetBorders(polygon);

            cancelProgressHandler?.Progress(33, "Copying raster.");

            // create output raster
            IRaster output = Raster.CreateRaster(outputFileName, input.DriverCode, input.NumColumns, input.NumRows, 1, input.DataType, new[] { string.Empty });

            output.Bounds      = input.Bounds.Copy();
            output.NoDataValue = input.NoDataValue;
            if (input.CanReproject)
            {
                output.Projection = input.Projection;
            }

            // set all initial values of Output to NoData
            for (int i = 0; i < output.NumRows; i++)
            {
                for (int j = 0; j < output.NumColumns; j++)
                {
                    output.Value[i, j] = output.NoDataValue;
                }
            }

            double xStart      = GetXStart(polygon, output);
            int    columnStart = GetStartColumn(polygon, output); // get the index of first column

            ProgressMeter pm = new ProgressMeter(cancelProgressHandler, "Clipping Raster", output.NumColumns)
            {
                StepPercent = 5, StartValue = 33
            };

            int col = 0;

            for (int columnCurrent = columnStart; columnCurrent < output.NumColumns; columnCurrent++)
            {
                var xCurrent = xStart + (col * output.CellWidth);

                var intersections = GetYIntersections(borders, xCurrent);
                intersections.Sort();
                ParseIntersections(intersections, xCurrent, columnCurrent, output, input);

                // update progess meter
                pm.CurrentValue = xCurrent;

                // update counter
                col++;

                // cancel if requested
                if (cancelProgressHandler != null && cancelProgressHandler.Cancel)
                {
                    return(null);
                }
            }

            output.Save();

            return(output);
        }