private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { //读取高程矩阵 RasterReader read = new RasterReader(e.Argument as string); double[,] src = DEMReader.GetElevation(read); DEMReader.SaveDem(read, src, null, RiverPath); FormOutput.AppendLog("1.计算洼地"); List <MyGrid> SourceGrid = new List <MyGrid>(); MyGrid.Src = src; SourceGrid = MyGrid.GetSourceGrids(); FormOutput.AppendLog($"栅格点有{SourceGrid.Count},其中洼地点有{SourceGrid.Where(t => t.IsFill == false).Count()}"); FormOutput.AppendLog("2.开始填充洼地"); #region 指定填洼 int times = 1; while (SourceGrid.Where(t => t.IsFill == false).Count() > 0) { double[,] tempSrc = new double[src.GetLongLength(0), src.GetLongLength(1)]; foreach (var item in SourceGrid) { if (item.IsFill) { tempSrc[item.Row, item.Col] = item.ALT; } else { tempSrc[item.Row, item.Col] = item.FilledALT; } } MyGrid.Src = tempSrc; SourceGrid = MyGrid.GetSourceGrids(); FormOutput.AppendLog($"第{times}次填充洼地,栅格点有{SourceGrid.Count},其中洼地点有{SourceGrid.Where(t => t.IsFill == false).Count()}"); src = tempSrc; times++; } #endregion fillGrid = src; var ss = SourceGrid.OrderByDescending(t => t.ALT).ToList(); if (!string.IsNullOrEmpty(FillPath)) { DEMReader.SaveDem(read, fillGrid, null, FillPath); } FormOutput.AppendLog("填充洼地完成"); //计算流向 FormOutput.AppendLog("2.开始计算流向.."); FormOutput.AppendLog($"未确定流向的点有{SourceGrid.Where(t => t.IsFlat).Count()}"); directionDev = new int[src.GetLength(0), src.GetLength(1)]; int index = 0; FormOutput.AppendProress(true); foreach (var item in SourceGrid) { directionDev[item.Row, item.Col] = item.Direction; index++; FormOutput.AppendProress(index * 100 / SourceGrid.Count); } if (!string.IsNullOrEmpty(DirectionPath)) { DEMReader.SaveDem(read, directionDev, null, DirectionPath); } FormOutput.AppendLog("流向计算完成.."); FormOutput.AppendLog("3.开始计算汇流量.."); Accumulation = new int[src.GetLength(0), src.GetLength(1)]; for (int i1 = 0; i1 < src.GetLength(0); i1++) { FormOutput.AppendProress(i1 * 100 / src.GetLength(0)); for (int j1 = 0; j1 < src.GetLength(1); j1++) { int i = i1; int j = j1; bool flag = true; List <Point> caledPoint = new List <Point>(); while (flag) { //计算之后不计算,防止死循环 if (caledPoint.Where(t => t.X == i && t.Y == j).Any()) { break; } int direction = directionDev[i, j]; caledPoint.Add(new Point(i, j)); switch (direction) { case 1: Accumulation[i, j + 1]++; j = j + 1; break; case 2: Accumulation[i = 1, j + 1]++; i = i + 1; j = j + 1; break; case 4: Accumulation[i + 1, j]++; i = i + 1; break; case 8: Accumulation[i + 1, j - 1]++; i = i + 1; j = j - 1; break; case 16: Accumulation[i, j - 1]++; j = j - 1; break; case 32: Accumulation[i - 1, j - 1]++; i = i - 1; j = j - 1; break; case 64: Accumulation[i - 1, j]++; i = i - 1; break; case 128: Accumulation[i - 1, j + 1]++; i = i - 1; j = j + 1; break; default: break; } flag = direction != 0 && i < src.GetLength(0) && j < src.GetLength(1); } } } if (!string.IsNullOrEmpty(AccumulationPath)) { DEMReader.SaveDem(read, Accumulation, null, AccumulationPath); } FormOutput.AppendLog("计算汇流量完成.."); FormOutput.AppendLog("4.开始根据回流阀值提取河网.."); FormOutput.AppendLog($"当前阀值为{RiverThreshold}.."); RiverGrid = new int[src.GetLength(0), src.GetLength(1)]; for (int i = 0; i < Accumulation.GetLength(0); i++) { for (int j = 0; j < Accumulation.GetLength(1); j++) { if (Accumulation[i, j] < RiverThreshold) { RiverGrid[i, j] = -1; } else { RiverGrid[i, j] = 1; } } } DEMReader.SaveDem(read, RiverGrid, null, RiverPath); FormOutput.AppendLog("河网提取完成.."); e.Result = RiverPath; }
/// <summary> /// 河网提取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGetHeWang_Click(object sender, EventArgs e) { if (!backgroundWorker1.IsBusy) { //获取河网参数 if (formRiver == null) { formRiver = new FormRiverArg(); } if (formRiver.ShowDialog() == DialogResult.OK) { RiverPath = formRiver.RiverPath; RiverThreshold = formRiver.RiverThreshold; FillPath = formRiver.FillPath; DirectionPath = formRiver.DirectionPath; AccumulationPath = formRiver.AccumulationPath; FormOutput.AppendLog("开始计算..."); _currentTime = DateTime.Now; SaveCaculateArg(); string srcPath = fileChooseControl1.FilePath; //已经计算过,直接从缓存读取 if (currentDem == srcPath) { RasterReader read = new RasterReader(srcPath); if (!string.IsNullOrEmpty(FillPath)) { DEMReader.SaveDem(read, fillGrid, null, FillPath); } if (!string.IsNullOrEmpty(DirectionPath)) { DEMReader.SaveDem(read, directionDev, null, DirectionPath); } if (!string.IsNullOrEmpty(AccumulationPath)) { DEMReader.SaveDem(read, Accumulation, null, AccumulationPath); } FormOutput.AppendLog("开始根据回流阀值提取河网.."); FormOutput.AppendLog($"当前阀值为{RiverThreshold}.."); RiverGrid = new int[Accumulation.GetLength(0), Accumulation.GetLength(1)]; for (int i = 0; i < Accumulation.GetLength(0); i++) { for (int j = 0; j < Accumulation.GetLength(1); j++) { if (Accumulation[i, j] < RiverThreshold) { RiverGrid[i, j] = -1; } else { RiverGrid[i, j] = 1; } } } DEMReader.SaveDem(read, RiverGrid, null, RiverPath); FormOutput.AppendLog("河网提取完成.."); FormOutput.AppendLog(string.Format("提取结束,共耗时{0}秒..", (DateTime.Now - _currentTime).TotalSeconds)); System.Diagnostics.Process.Start("Explorer.exe", Path.GetDirectoryName(RiverPath)); } else { backgroundWorker2.RunWorkerAsync(srcPath); } } } else { FormOutput.AppendLog("当前后台正在计算..."); } }