/// <summary> /// Finds the average slope in the given polygons with more user preferences. /// </summary> /// <param name="ras">The dem Raster(Grid file).</param> /// <param name="inZFactor">The scaler factor</param> /// <param name="slopeInPercent">The slope in percentage.</param> /// <param name="poly">The flow poly shapefile path.</param> /// <param name="fldInPolyToStoreSlope">The field name to store average slope in the attribute.</param> /// <param name="outerShpFile">The Featureset where we have the area of interest</param> /// <param name="outerShpIndex">The index of featureset which give paticular area of interest.</param> /// <param name="output">The path to save created slope Feature set.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns></returns> public bool Execute(IRaster ras, double inZFactor, bool slopeInPercent, IFeatureSet poly, string fldInPolyToStoreSlope, IFeatureSet outerShpFile, int outerShpIndex, IFeatureSet output, ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (ras == null || poly == null || outerShpFile == null || output == null) { return false; } if (poly.FeatureType != FeatureTypes.Polygon || outerShpFile.FeatureType != FeatureTypes.Polygon) return false; int previous = 0; IRaster slopegrid = new Raster(); int[] areaCount = new int[poly.Features.Count]; double[] areaTotal = new double[poly.Features.Count]; double[] areaAve = new double[poly.Features.Count]; Slope(ras, inZFactor, slopeInPercent, slopegrid, cancelProgressHandler); if (slopegrid == null) throw new SystemException(TextStrings.Slopegridfileisnull); foreach (IFeature f in poly.Features) output.Features.Add(f); for (int i = 0; i < slopegrid.NumRows; i++) { int current = Convert.ToInt32(Math.Round(i * 100D / slopegrid.NumRows)); //only update when increment in percentage if (current > previous+5) { cancelProgressHandler.Progress("", current, current + TextStrings.progresscompleted); previous = current; } for (int j = 0; j < slopegrid.NumColumns; j++) { Coordinate coordin = slopegrid.CellToProj(i, j); IPoint pt = new Point(coordin); IFeature point = new Feature(pt); if (!outerShpFile.Features[outerShpIndex].Covers(point)) continue;//not found the point inside. for (int c = 0; c < poly.Features.Count; c++) { if (output.Features[c].Covers(point)) { areaCount[c]++; areaTotal[c] += slopegrid.Value[i, j] / 100; } if (cancelProgressHandler.Cancel) return false; } } } //Add the column output.DataTable.Columns.Add("FID", typeof(int)); output.DataTable.Columns.Add(fldInPolyToStoreSlope, typeof(Double)); for (int c = 0; c < output.Features.Count; c++) { if (areaCount[c] == 0) areaAve[c] = 0.0; else areaAve[c] = areaTotal[c] / areaCount[c]; //Add the field values output.Features[c].DataRow["FID"] = c; output.Features[c].DataRow[fldInPolyToStoreSlope] = areaAve[c]; } output.SaveAs(output.Filename,true); slopegrid.Close(); ras.Close(); return true; }
/// <summary> /// Finds the average slope in the given polygons. /// </summary> /// <param name="gridIn">The Polygon Raster(Grid file).</param> /// <param name="polyOut">The Polygon shapefile path.</param> /// <param name="progress">The progress handler.</param> public bool Execute(IRaster gridIn, IFeatureSet polyOut, ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (gridIn == null || polyOut == null) { return false; } int maxX, maxY; int current = 0; int previous = 0; double noData, currVal, currTrack; string strTrackPath; IRaster gridTrack = new Raster(); maxX = gridIn.NumRows - 1; maxY = gridIn.NumColumns - 1; noData = gridIn.NoDataValue; //strTrackPath = System.IO.Path.GetDirectoryName(strInRast) + "\\" + System.IO.Path.GetFileNameWithoutExtension(strInRast) + "_track.bgd"; gridTrack = Raster.Create("gridTrack.bgd", "", gridIn.NumColumns, gridIn.NumRows, 1, gridIn.DataType, new string[] { "" }); //gridTrack.CreateNew("gridTrack", "", gridIn.NumColumns, gridIn.NumRows, 1, gridIn.DataType, new string[] { "" }); gridTrack.Bounds = gridIn.Bounds; gridTrack.NoDataValue = gridIn.NoDataValue; polyOut.DataTable.Columns.Add("Value", typeof(int)); polyOut.DataTable.Columns.Add("Zone", typeof(string)); polyOut.DataTable.Columns.Add("Area", typeof(double)); polyOut.DataTable.Columns.Add("COMID", typeof(string)); polyOut.DataTable.Columns.Add("AveSlope", typeof(double)); for (int i = 0; i <= maxX; i++) { current = Convert.ToInt32(i * 100 / maxX); //only update when increment in percentage if (current > previous+5) { cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed"); previous = current; } for (int j = 0; j <= maxY; j++) { if (i > 0 && j > 0) { currVal = Convert.ToInt16(gridIn.Value[i, j]); currTrack = Convert.ToInt16(gridTrack.Value[i, j]); if (currVal == gridIn.NoDataValue) { gridTrack.Value[i, j] = 1; if (cancelProgressHandler.Cancel == true) return false; } else { if (currTrack == 1) { } else { formPolyFromCell(gridIn, gridTrack, i, j, polyOut, cancelProgressHandler); if (cancelProgressHandler.Cancel == true) return false; } } } else { gridTrack.Value[i, j] = gridIn.NoDataValue; } } } gridIn.Close(); gridTrack.Close(); polyOut.SaveAs(polyOut.Filename, true); polyOut.Close(); return true; }
/// <summary> /// Finds the average slope in the given polygons. /// </summary> /// <param name="ras">The dem Raster(Grid file).</param> /// <param name="zFactor">The scaler factor</param> /// <param name="poly">The flow poly shapefile path.</param> /// <param name="output">The resulting DEM of slopes</param> /// <param name="cancelProgressHandler">The progress handler.</param> public bool Execute(IRaster ras, double zFactor, IFeatureSet poly, IFeatureSet output, ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (ras == null || poly == null || output == null) { return false; } output.FeatureType = poly.FeatureType; foreach (IFeature f in poly.Features) output.Features.Add(f); output.DataTable.Columns.Add("FID", typeof(int)); output.DataTable.Columns.Add(TextStrings.AveSlope, typeof(Double)); IRaster slopeGrid = new Raster(); slopeGrid.DataType = ras.DataType; slopeGrid.Bounds = ras.Bounds; //FeatureSet polyShape = new FeatureSet(); int previous = 0; if (Slope(ref ras, zFactor, false, ref slopeGrid, cancelProgressHandler) == false) return false; int shapeCount = output.Features.Count; int[] areaCount = new int[shapeCount]; double[] areaTotal = new double[shapeCount]; double[] areaAve = new double[shapeCount]; double dxHalf = slopeGrid.CellWidth / 2; double dyHalf = slopeGrid.CellHeight / 2; //check whether those two envelope are intersect if (ras.Bounds.Envelope.Intersects(output.Envelope) == false) return false; RcIndex start = slopeGrid.ProjToCell(output.Envelope.Minimum.X, output.Envelope.Maximum.Y); RcIndex stop = slopeGrid.ProjToCell(output.Envelope.Maximum.X, output.Envelope.Minimum.Y); int rowStart = start.Row; int colStart = start.Column; int rowStop = stop.Row; int colStop = stop.Column; for (int row = rowStart - 1; row < rowStop + 1; row++) { int current = Convert.ToInt32((row - rowStart + 1) * 100.0 / (rowStop + 1 - rowStart + 1)); //only update when increment in percentage if (current > previous + 5) { cancelProgressHandler.Progress("", current, current + TextStrings.progresscompleted); previous = current; } for (int col = colStart - 1; col < colStop + 1; col++) { Coordinate cent = slopeGrid.CellToProj(row, col); double xCent = cent.X; double yCent = cent.Y; for (int shpindx = 0; shpindx < output.Features.Count; shpindx++) { IFeature tempFeat = output.Features[shpindx]; Point pt1 = new Point(xCent, yCent); Point pt2 = new Point(xCent - dxHalf, yCent - dyHalf); Point pt3 = new Point(xCent + dxHalf, yCent - dyHalf); Point pt4 = new Point(xCent + dxHalf, yCent + dyHalf); Point pt5 = new Point(xCent - dxHalf, yCent + dyHalf); if ((((!tempFeat.Covers(pt1) && !tempFeat.Covers(pt2)) && !tempFeat.Covers(pt3)) && !tempFeat.Covers(pt4)) && !tempFeat.Covers(pt5)) continue; areaCount[shpindx]++; areaTotal[shpindx] += slopeGrid.Value[row, col] / 100; if (cancelProgressHandler.Cancel) return false; } } } for (int shpindx = 0; shpindx < output.Features.Count; shpindx++) { if (areaCount[shpindx] == 0) { areaAve[shpindx] = 0; } else { areaAve[shpindx] = areaTotal[shpindx] / areaCount[shpindx]; } output.Features[shpindx].DataRow["FID"] = shpindx; output.Features[shpindx].DataRow[TextStrings.AveSlope] = areaAve[shpindx]; } poly.Close(); slopeGrid.Close(); output.SaveAs(output.Filename,true); return true; }