private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string[] args = e.Argument as string[]; double avgLength = 0; double avgSlope = 0; if (File.Exists(args[0]) && File.Exists(args[1])) { FormOutput.AppendLog("开始获取线的交点..."); RasterReader reader = new RasterReader(args[1]); List <Point3d> pts = CalPoints(args[0], reader); FormOutput.AppendLog("开始计算平均坡度和坡长..."); CalAvgSlopeLength(pts, reader, ref avgLength, ref avgSlope); reader.Dispose(); FormOutput.AppendLog("平均坡长:" + avgLength.ToString("f3")); FormOutput.AppendLog("平均坡度:" + avgSlope.ToString("f3")); } double?r = null; if (File.Exists(args[1]) && File.Exists(args[2])) { FormOutput.AppendLog("开始计算坡面流速系数..."); r = RasterCoefficientReader.ReadCoeficient(args[2], args[1]); if (r.HasValue) { FormOutput.AppendLog("结果--坡面流速系数为:" + r.Value.ToString()); } } e.Result = new double[] { avgLength, avgSlope, r.GetValueOrDefault(0) }; }
private double CaculateRiver(List <Dictionary <Point2d, Geometry> > dic, string tifPath, ref Dictionary <Point2d, Geometry> mainRiver) { RasterReader reader = new RasterReader(tifPath); //Dictionary<Point2d, Geometry> maxGeoLength = null; double maxLength = 0; //主沟道长度 List <Point3d> gradientPoints = new List <Point3d>(); // List<Point3d> gradientPoints1 = new List<Point3d>(); int percent = 0; foreach (var geoLength in dic) { double tempLength = 0; List <Point2d> pointListLength = new List <Point2d>(); gradientPoints = new List <Point3d>(); foreach (var length in geoLength) { int pcount = length.Value.GetPointCount(); for (int i = 0; i < pcount; i++) { Point2d temp = new Point2d(length.Value.GetX(i), length.Value.GetY(i)); if (!pointListLength.Contains(temp)) { pointListLength.Add(temp); gradientPoints.Add(new Point3d(length.Value.GetX(i), length.Value.GetY(i), GetElevationByPointInDEM(temp, reader))); } } } for (int i = 1; i < gradientPoints.Count; i++) { tempLength += GetLength(gradientPoints[i - 1], gradientPoints[i]); } if (maxLength < tempLength) { maxLength = tempLength; mainRiver = geoLength; //gradientPoints1 = gradientPoints; } percent++; FormOutput.AppendProress(percent * 50 / dic.Count); } reader.Dispose(); return(maxLength); }
/// <summary> /// 用于计算shp文件中的系数平均值 /// </summary> /// <param name="shppath">shp路径</param> /// <param name="inputDemPath">tif路径</param> /// <returns></returns> public static double?ReadCoeficient(string shppath, string inputDemPath) { FormOutput.AppendLog("按规定范围裁剪栅格.."); //创建一个临时目录 string outDemPath = Path.Combine(Path.GetTempPath(), "WDEM.tif"); RasterReader raster = null; Band band = null; try { ImgCut.CutTiff(shppath, inputDemPath, outDemPath); if (File.Exists(outDemPath) == false) { FormOutput.AppendLog("裁剪失败!"); return(null); } FormOutput.AppendLog("开始读取裁剪后的栅格数据.."); raster = new RasterReader(outDemPath); int row = raster.RowCount; int col = raster.ColumnCount; // int count = raster.RasterCount; int xsize = raster.DataSet.RasterXSize; int ysize = raster.DataSet.RasterYSize; band = raster.DataSet.GetRasterBand(1); //无效值 //FormOutput.AppendLog("获取栅格数据无效值.."); double nodatavalue; int hasval; band.GetNoDataValue(out nodatavalue, out hasval); // FormOutput.AppendLog("栅格数据无效值为" + nodatavalue); double[] readData = new double[row * col]; band.ReadRaster(0, 0, xsize, ysize, readData, row, col, 0, 0); //FormOutput.AppendLog("开始整理损失指数数据.."); var res = readData.GroupBy(t => t).Select(t => new { count = t.Count(), Key = t.Key }).ToArray(); double total = 0; double totalcount = 0; foreach (var s in res) { if (s.Key != nodatavalue) { total += s.Key * s.count; totalcount += s.count; } } double R = total / totalcount; R = R / 1000; //转换?? return(R); } catch (Exception ex) { FormOutput.AppendLog("获取栅格数据异常:" + ex.Message); return(null); } finally { //销毁 if (raster != null) { band.Dispose(); raster.Dispose(); } } }