/// <summary> /// 2.水流方向计算 /// 水流方向计算就可以使用上一步所生成的DEM为源数据了 ///(如果使用未经洼地填平处理的数据,可能会造成精度下降) /// 算法说明:计算某个单元格附近八个(不足的不考虑)的最小高程(最大高程差) /// 结果说明:若有一个最小的,流向指向最小的;若有多个最小的继续分别计算几个最小值附近的最小值 /// 水流方向计算之后返回的是1-128表示流向的值 ///一直递归找到最小值的那一项 </summary> /// <param name="src"></param> /// <returns></returns> public static double[,] FlowDirection(this double[,] src) { int rowCount = src.GetLength(0); int colCount = src.GetLength(1); double[,] result = new double[rowCount, colCount]; for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { result[row, col] = CalFlowDirection(rowCount, colCount, row, col, src); FormCalView.SetColor(row, col, (int)result[row, col]); } } return(result); }
/// <summary> /// 3.水流积聚计算 /// 可以看到,生成的水流积聚栅格已经可以看到所产生的河网了。 /// 现在所需要做的就是把这些河网栅格提取出来。可以把产生的河网的支流的象素值作为阀值来提取河网栅格。 /// </summary> /// <param name="src"></param> /// <returns></returns> public static double[,] Accumulation(this double[,] src) { int rowCount = src.GetLength(0); int colCount = src.GetLength(1); double[,] result = new double[rowCount, colCount]; //定义结果 Dictionary <Grid, List <Grid> > dicCaledAll = new Dictionary <Grid, List <Grid> >(); Queue <Grid> queueGrid = new Queue <Grid>(); Dictionary <Grid, int> unFindOutGrid = new Dictionary <Grid, int>(); for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { //将数据添加到方程组中 Grid tempGrid = new Grid(row, col); queueGrid.Enqueue(tempGrid); unFindOutGrid.Add(tempGrid, 0); //添加默认为零 dicCaledAll.Add(tempGrid, GetFlowAround(src, tempGrid)); } } Dictionary <Grid, int> findOutGrid = new Dictionary <Grid, int>(); FormCalView.SetAllSize(rowCount, colCount); FormOutput.AppendProress(true); //开始解方程 while (queueGrid.Count > 0) { FormOutput.AppendProress(findOutGrid.Count * 100 / dicCaledAll.Count); Grid tGrid = queueGrid.Dequeue(); List <Grid> lstGrid = dicCaledAll[tGrid]; //找到数据 if (lstGrid.Count == 0) { result[tGrid.i, tGrid.j] = unFindOutGrid[tGrid]; findOutGrid.Add(tGrid, unFindOutGrid[tGrid]); dicCaledAll.Remove(tGrid); //unFindOutGrid.Remove(tGrid); FormCalView.SetColor(tGrid.i, tGrid.j, unFindOutGrid[tGrid]); } else { List <Grid> lstTemp = new List <Grid>(); lstTemp.AddRange(dicCaledAll[tGrid]); //循环寻找当前找出的Grid for (int i = 0; i < lstGrid.Count; i++) { Grid temp = lstGrid[i]; if (ContainsGrid(findOutGrid, temp)) { Grid temp1 = FindGrid(unFindOutGrid, temp); Grid temp2 = FindGrid(findOutGrid, temp); unFindOutGrid[temp1] = unFindOutGrid[temp1] + findOutGrid[temp2] + 1; lstTemp.Remove(temp); } } if (lstTemp.Count == 0) { result[tGrid.i, tGrid.j] = unFindOutGrid[tGrid]; findOutGrid.Add(tGrid, unFindOutGrid[tGrid]); dicCaledAll.Remove(tGrid); // unFindOutGrid.Remove(tGrid); FormCalView.SetColor(tGrid.i, tGrid.j, unFindOutGrid[tGrid]); } else { dicCaledAll[tGrid] = lstTemp; queueGrid.Enqueue(tGrid); } } } return(result); }